2 * Copyright 2017 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <folly/json.h>
19 #include <folly/Benchmark.h>
20 #include <folly/Conv.h>
21 #include <folly/FileUtil.h>
22 #include <folly/Range.h>
23 #include <folly/portability/GFlags.h>
24 #include <folly/portability/GTest.h>
27 using folly::parseJson;
30 constexpr folly::StringPiece kLargeAsciiString =
31 "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk"
32 "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk"
33 "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk"
34 "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk"
35 "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk"
36 "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk"
37 "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk"
38 "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk"
39 "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk"
40 "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk"
41 "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk";
43 constexpr folly::StringPiece kLargeNonAsciiString =
44 "qwerty \xc2\x80 \xef\xbf\xbf poiuy"
45 "qwerty \xc2\x80 \xef\xbf\xbf poiuy"
46 "qwerty \xc2\x80 \xef\xbf\xbf poiuy"
47 "qwerty \xc2\x80 \xef\xbf\xbf poiuy"
48 "qwerty \xc2\x80 \xef\xbf\xbf poiuy"
49 "qwerty \xc2\x80 \xef\xbf\xbf poiuy"
50 "qwerty \xc2\x80 \xef\xbf\xbf poiuy"
51 "qwerty \xc2\x80 \xef\xbf\xbf poiuy"
52 "qwerty \xc2\x80 \xef\xbf\xbf poiuy"
53 "qwerty \xc2\x80 \xef\xbf\xbf poiuy";
55 TEST(Json, StripComments) {
56 const std::string kTestDir = "folly/test/";
57 const std::string kTestFile = "json_test_data/commented.json";
58 const std::string kTestExpected = "json_test_data/commented.json.exp";
61 std::string expectedStr;
62 if (!folly::readFile(kTestFile.data(), testStr) &&
63 !folly::readFile((kTestDir + kTestFile).data(), testStr)) {
64 FAIL() << "can not read test file " << kTestFile;
66 if (!folly::readFile(kTestExpected.data(), expectedStr) &&
67 !folly::readFile((kTestDir + kTestExpected).data(), expectedStr)) {
68 FAIL() << "can not read test file " << kTestExpected;
70 EXPECT_EQ(expectedStr, folly::json::stripComments(testStr));
73 BENCHMARK(jsonSerialize, iters) {
74 const dynamic obj = kLargeNonAsciiString;
76 folly::json::serialization_opts opts;
77 for (size_t i = 0; i < iters; ++i) {
78 folly::json::serialize(obj, opts);
82 BENCHMARK(jsonSerializeWithNonAsciiEncoding, iters) {
83 const dynamic obj = kLargeNonAsciiString;
85 folly::json::serialization_opts opts;
86 opts.encode_non_ascii = true;
88 for (size_t i = 0; i < iters; ++i) {
89 folly::json::serialize(obj, opts);
93 BENCHMARK(jsonSerializeWithUtf8Validation, iters) {
94 const dynamic obj = kLargeNonAsciiString;
96 folly::json::serialization_opts opts;
97 opts.validate_utf8 = true;
99 for (size_t i = 0; i < iters; ++i) {
100 folly::json::serialize(obj, opts);
104 BENCHMARK(jsonSerializeAsciiWithUtf8Validation, iters) {
105 const dynamic obj = kLargeAsciiString;
107 folly::json::serialization_opts opts;
108 opts.validate_utf8 = true;
110 for (size_t i = 0; i < iters; ++i) {
111 folly::json::serialize(obj, opts);
115 BENCHMARK(parseSmallStringWithUtf, iters) {
116 for (size_t i = 0; i < iters << 4; ++i) {
117 parseJson("\"I \\u2665 UTF-8 thjasdhkjh blah blah blah\"");
121 BENCHMARK(parseNormalString, iters) {
122 for (size_t i = 0; i < iters << 4; ++i) {
123 parseJson("\"akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk\"");
127 BENCHMARK(parseBigString, iters) {
128 const auto json = folly::to<std::string>('"', kLargeAsciiString, '"');
130 for (size_t i = 0; i < iters; ++i) {
135 BENCHMARK(toJson, iters) {
136 dynamic something = parseJson(
137 "{\"old_value\":40,\"changed\":true,\"opened\":false,\"foo\":[1,2,3,4,5,6]}"
140 for (size_t i = 0; i < iters; i++) {
145 int main(int argc, char** argv) {
146 testing::InitGoogleTest(&argc, argv);
147 gflags::ParseCommandLineFlags(&argc, &argv, true);
148 if (FLAGS_benchmark) {
149 folly::runBenchmarks();
151 return RUN_ALL_TESTS();