01fd4cff1aae5bb028c2a8ed1b4b3b5043a0d9db
[folly.git] / folly / test / JsonOtherTest.cpp
1 /*
2  * Copyright 2016 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <folly/json.h>
18
19 #include <folly/Benchmark.h>
20 #include <folly/FileUtil.h>
21 #include <folly/portability/GFlags.h>
22
23 #include <gtest/gtest.h>
24
25 using folly::dynamic;
26 using folly::parseJson;
27 using folly::toJson;
28
29 TEST(Json, StripComments) {
30   const std::string kTestDir = "folly/test/";
31   const std::string kTestFile = "json_test_data/commented.json";
32   const std::string kTestExpected = "json_test_data/commented.json.exp";
33
34   std::string testStr;
35   std::string expectedStr;
36   if (!folly::readFile(kTestFile.data(), testStr) &&
37       !folly::readFile((kTestDir + kTestFile).data(), testStr)) {
38     FAIL() << "can not read test file " << kTestFile;
39   }
40   if (!folly::readFile(kTestExpected.data(), expectedStr) &&
41       !folly::readFile((kTestDir + kTestExpected).data(), expectedStr)) {
42     FAIL() << "can not read test file " << kTestExpected;
43   }
44   EXPECT_EQ(expectedStr, folly::json::stripComments(testStr));
45 }
46
47 BENCHMARK(jsonSerialize, iters) {
48   folly::json::serialization_opts opts;
49   for (size_t i = 0; i < iters; ++i) {
50     folly::json::serialize(
51       "qwerty \xc2\x80 \xef\xbf\xbf poiuy"
52       "qwerty \xc2\x80 \xef\xbf\xbf poiuy"
53       "qwerty \xc2\x80 \xef\xbf\xbf poiuy"
54       "qwerty \xc2\x80 \xef\xbf\xbf poiuy"
55       "qwerty \xc2\x80 \xef\xbf\xbf poiuy"
56       "qwerty \xc2\x80 \xef\xbf\xbf poiuy"
57       "qwerty \xc2\x80 \xef\xbf\xbf poiuy"
58       "qwerty \xc2\x80 \xef\xbf\xbf poiuy"
59       "qwerty \xc2\x80 \xef\xbf\xbf poiuy"
60       "qwerty \xc2\x80 \xef\xbf\xbf poiuy",
61       opts);
62   }
63 }
64
65 BENCHMARK(jsonSerializeWithNonAsciiEncoding, iters) {
66   folly::json::serialization_opts opts;
67   opts.encode_non_ascii = true;
68
69   for (size_t i = 0; i < iters; ++i) {
70     folly::json::serialize(
71       "qwerty \xc2\x80 \xef\xbf\xbf poiuy"
72       "qwerty \xc2\x80 \xef\xbf\xbf poiuy"
73       "qwerty \xc2\x80 \xef\xbf\xbf poiuy"
74       "qwerty \xc2\x80 \xef\xbf\xbf poiuy"
75       "qwerty \xc2\x80 \xef\xbf\xbf poiuy"
76       "qwerty \xc2\x80 \xef\xbf\xbf poiuy"
77       "qwerty \xc2\x80 \xef\xbf\xbf poiuy"
78       "qwerty \xc2\x80 \xef\xbf\xbf poiuy"
79       "qwerty \xc2\x80 \xef\xbf\xbf poiuy"
80       "qwerty \xc2\x80 \xef\xbf\xbf poiuy",
81       opts);
82   }
83 }
84
85 BENCHMARK(jsonSerializeWithUtf8Validation, iters) {
86   folly::json::serialization_opts opts;
87   opts.validate_utf8 = true;
88
89   for (size_t i = 0; i < iters; ++i) {
90     folly::json::serialize(
91       "qwerty \xc2\x80 \xef\xbf\xbf poiuy"
92       "qwerty \xc2\x80 \xef\xbf\xbf poiuy"
93       "qwerty \xc2\x80 \xef\xbf\xbf poiuy"
94       "qwerty \xc2\x80 \xef\xbf\xbf poiuy"
95       "qwerty \xc2\x80 \xef\xbf\xbf poiuy"
96       "qwerty \xc2\x80 \xef\xbf\xbf poiuy"
97       "qwerty \xc2\x80 \xef\xbf\xbf poiuy"
98       "qwerty \xc2\x80 \xef\xbf\xbf poiuy"
99       "qwerty \xc2\x80 \xef\xbf\xbf poiuy"
100       "qwerty \xc2\x80 \xef\xbf\xbf poiuy",
101       opts);
102   }
103 }
104
105 BENCHMARK(parseSmallStringWithUtf, iters) {
106   for (size_t i = 0; i < iters << 4; ++i) {
107     parseJson("\"I \\u2665 UTF-8 thjasdhkjh blah blah blah\"");
108   }
109 }
110
111 BENCHMARK(parseNormalString, iters) {
112   for (size_t i = 0; i < iters << 4; ++i) {
113     parseJson("\"akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk\"");
114   }
115 }
116
117 BENCHMARK(parseBigString, iters) {
118   for (size_t i = 0; i < iters; ++i) {
119     parseJson("\""
120       "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk"
121       "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk"
122       "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk"
123       "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk"
124       "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk"
125       "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk"
126       "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk"
127       "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk"
128       "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk"
129       "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk"
130       "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk"
131       "\"");
132   }
133 }
134
135 BENCHMARK(toJson, iters) {
136   dynamic something = parseJson(
137     "{\"old_value\":40,\"changed\":true,\"opened\":false,\"foo\":[1,2,3,4,5,6]}"
138   );
139
140   for (size_t i = 0; i < iters; i++) {
141     toJson(something);
142   }
143 }
144
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();
150   }
151   return RUN_ALL_TESTS();
152 }