Optimize JSON escaping of ASCII strings
[folly.git] / folly / test / JsonOtherTest.cpp
index 1d2954494cd9086b59dd445684e4cc157290c743..0d58c06fb8c1b9df778c5c62771140d729987e2c 100644 (file)
@@ -17,7 +17,9 @@
 #include <folly/json.h>
 
 #include <folly/Benchmark.h>
+#include <folly/Conv.h>
 #include <folly/FileUtil.h>
+#include <folly/Range.h>
 #include <folly/portability/GFlags.h>
 #include <folly/portability/GTest.h>
 
@@ -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<std::string>('"', 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);
   }
 }