Fix SimpleBarrier
[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 #include <folly/portability/GTest.h>
23
24 using folly::dynamic;
25 using folly::parseJson;
26 using folly::toJson;
27
28 TEST(Json, StripComments) {
29   const std::string kTestDir = "folly/test/";
30   const std::string kTestFile = "json_test_data/commented.json";
31   const std::string kTestExpected = "json_test_data/commented.json.exp";
32
33   std::string testStr;
34   std::string expectedStr;
35   if (!folly::readFile(kTestFile.data(), testStr) &&
36       !folly::readFile((kTestDir + kTestFile).data(), testStr)) {
37     FAIL() << "can not read test file " << kTestFile;
38   }
39   if (!folly::readFile(kTestExpected.data(), expectedStr) &&
40       !folly::readFile((kTestDir + kTestExpected).data(), expectedStr)) {
41     FAIL() << "can not read test file " << kTestExpected;
42   }
43   EXPECT_EQ(expectedStr, folly::json::stripComments(testStr));
44 }
45
46 BENCHMARK(jsonSerialize, iters) {
47   folly::json::serialization_opts opts;
48   for (size_t i = 0; i < iters; ++i) {
49     folly::json::serialize(
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"
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       opts);
61   }
62 }
63
64 BENCHMARK(jsonSerializeWithNonAsciiEncoding, iters) {
65   folly::json::serialization_opts opts;
66   opts.encode_non_ascii = true;
67
68   for (size_t i = 0; i < iters; ++i) {
69     folly::json::serialize(
70       "qwerty \xc2\x80 \xef\xbf\xbf poiuy"
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       opts);
81   }
82 }
83
84 BENCHMARK(jsonSerializeWithUtf8Validation, iters) {
85   folly::json::serialization_opts opts;
86   opts.validate_utf8 = true;
87
88   for (size_t i = 0; i < iters; ++i) {
89     folly::json::serialize(
90       "qwerty \xc2\x80 \xef\xbf\xbf poiuy"
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       opts);
101   }
102 }
103
104 BENCHMARK(parseSmallStringWithUtf, iters) {
105   for (size_t i = 0; i < iters << 4; ++i) {
106     parseJson("\"I \\u2665 UTF-8 thjasdhkjh blah blah blah\"");
107   }
108 }
109
110 BENCHMARK(parseNormalString, iters) {
111   for (size_t i = 0; i < iters << 4; ++i) {
112     parseJson("\"akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk\"");
113   }
114 }
115
116 BENCHMARK(parseBigString, iters) {
117   for (size_t i = 0; i < iters; ++i) {
118     parseJson("\""
119       "akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk"
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       "\"");
131   }
132 }
133
134 BENCHMARK(toJson, iters) {
135   dynamic something = parseJson(
136     "{\"old_value\":40,\"changed\":true,\"opened\":false,\"foo\":[1,2,3,4,5,6]}"
137   );
138
139   for (size_t i = 0; i < iters; i++) {
140     toJson(something);
141   }
142 }
143
144 int main(int argc, char** argv) {
145   testing::InitGoogleTest(&argc, argv);
146   gflags::ParseCommandLineFlags(&argc, &argv, true);
147   if (FLAGS_benchmark) {
148     folly::runBenchmarks();
149   }
150   return RUN_ALL_TESTS();
151 }