X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2Ftest%2FJsonOtherTest.cpp;h=0d58c06fb8c1b9df778c5c62771140d729987e2c;hb=7acba7e1a0a75d22087647b3e9e830c9b0e8d41e;hp=dd1a5b83de109a37ff410ebdf848cb277bb5444d;hpb=6a6ac91e1fda65d7871390f03be8d19b3591ba45;p=folly.git diff --git a/folly/test/JsonOtherTest.cpp b/folly/test/JsonOtherTest.cpp index dd1a5b83..0d58c06f 100644 --- a/folly/test/JsonOtherTest.cpp +++ b/folly/test/JsonOtherTest.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2016 Facebook, Inc. + * Copyright 2017 Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,7 +17,9 @@ #include #include +#include #include +#include #include #include @@ -25,6 +27,31 @@ using folly::dynamic; using folly::parseJson; using folly::toJson; +constexpr folly::StringPiece kLargeAsciiString = + "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk" + "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk" + "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk" + "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk" + "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk" + "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk" + "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk" + "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk" + "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk" + "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk" + "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk"; + +constexpr folly::StringPiece kLargeNonAsciiString = + "qwerty \xc2\x80 \xef\xbf\xbf poiuy" + "qwerty \xc2\x80 \xef\xbf\xbf poiuy" + "qwerty \xc2\x80 \xef\xbf\xbf poiuy" + "qwerty \xc2\x80 \xef\xbf\xbf poiuy" + "qwerty \xc2\x80 \xef\xbf\xbf poiuy" + "qwerty \xc2\x80 \xef\xbf\xbf poiuy" + "qwerty \xc2\x80 \xef\xbf\xbf poiuy" + "qwerty \xc2\x80 \xef\xbf\xbf poiuy" + "qwerty \xc2\x80 \xef\xbf\xbf poiuy" + "qwerty \xc2\x80 \xef\xbf\xbf poiuy"; + TEST(Json, StripComments) { const std::string kTestDir = "folly/test/"; const std::string kTestFile = "json_test_data/commented.json"; @@ -44,60 +71,44 @@ TEST(Json, StripComments) { } BENCHMARK(jsonSerialize, iters) { + const dynamic obj = kLargeNonAsciiString; + folly::json::serialization_opts opts; for (size_t i = 0; i < iters; ++i) { - folly::json::serialize( - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy", - opts); + folly::json::serialize(obj, opts); } } BENCHMARK(jsonSerializeWithNonAsciiEncoding, iters) { + const dynamic obj = kLargeNonAsciiString; + folly::json::serialization_opts opts; opts.encode_non_ascii = true; for (size_t i = 0; i < iters; ++i) { - folly::json::serialize( - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy", - opts); + folly::json::serialize(obj, opts); } } BENCHMARK(jsonSerializeWithUtf8Validation, iters) { + const dynamic obj = kLargeNonAsciiString; + folly::json::serialization_opts opts; opts.validate_utf8 = true; for (size_t i = 0; i < iters; ++i) { - folly::json::serialize( - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy" - "qwerty \xc2\x80 \xef\xbf\xbf poiuy", - opts); + folly::json::serialize(obj, opts); + } +} + +BENCHMARK(jsonSerializeAsciiWithUtf8Validation, iters) { + const dynamic obj = kLargeAsciiString; + + folly::json::serialization_opts opts; + opts.validate_utf8 = true; + + for (size_t i = 0; i < iters; ++i) { + folly::json::serialize(obj, opts); } } @@ -114,20 +125,10 @@ BENCHMARK(parseNormalString, iters) { } BENCHMARK(parseBigString, iters) { + const auto json = folly::to('"', kLargeAsciiString, '"'); + for (size_t i = 0; i < iters; ++i) { - parseJson("\"" - "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk" - "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk" - "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk" - "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk" - "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk" - "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk" - "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk" - "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk" - "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk" - "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk" - "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk" - "\""); + parseJson(json); } }