Fix copyright lines
[folly.git] / folly / test / JsonOtherTest.cpp
1 /*
2  * Copyright 2015-present 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/Conv.h>
21 #include <folly/FileUtil.h>
22 #include <folly/Range.h>
23 #include <folly/portability/GFlags.h>
24 #include <folly/portability/GTest.h>
25
26 using folly::dynamic;
27 using folly::parseJson;
28 using folly::toJson;
29
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";
42
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";
54
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";
59
60   std::string testStr;
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;
65   }
66   if (!folly::readFile(kTestExpected.data(), expectedStr) &&
67       !folly::readFile((kTestDir + kTestExpected).data(), expectedStr)) {
68     FAIL() << "can not read test file " << kTestExpected;
69   }
70   EXPECT_EQ(expectedStr, folly::json::stripComments(testStr));
71 }
72
73 BENCHMARK(jsonSerialize, iters) {
74   const dynamic obj = kLargeNonAsciiString;
75
76   folly::json::serialization_opts opts;
77   for (size_t i = 0; i < iters; ++i) {
78     folly::json::serialize(obj, opts);
79   }
80 }
81
82 BENCHMARK(jsonSerializeWithNonAsciiEncoding, iters) {
83   const dynamic obj = kLargeNonAsciiString;
84
85   folly::json::serialization_opts opts;
86   opts.encode_non_ascii = true;
87
88   for (size_t i = 0; i < iters; ++i) {
89     folly::json::serialize(obj, opts);
90   }
91 }
92
93 BENCHMARK(jsonSerializeWithUtf8Validation, iters) {
94   const dynamic obj = kLargeNonAsciiString;
95
96   folly::json::serialization_opts opts;
97   opts.validate_utf8 = true;
98
99   for (size_t i = 0; i < iters; ++i) {
100     folly::json::serialize(obj, opts);
101   }
102 }
103
104 BENCHMARK(jsonSerializeAsciiWithUtf8Validation, iters) {
105   const dynamic obj = kLargeAsciiString;
106
107   folly::json::serialization_opts opts;
108   opts.validate_utf8 = true;
109
110   for (size_t i = 0; i < iters; ++i) {
111     folly::json::serialize(obj, opts);
112   }
113 }
114
115 BENCHMARK(parseSmallStringWithUtf, iters) {
116   for (size_t i = 0; i < iters << 4; ++i) {
117     parseJson("\"I \\u2665 UTF-8 thjasdhkjh blah blah blah\"");
118   }
119 }
120
121 BENCHMARK(parseNormalString, iters) {
122   for (size_t i = 0; i < iters << 4; ++i) {
123     parseJson("\"akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk\"");
124   }
125 }
126
127 BENCHMARK(parseBigString, iters) {
128   const auto json = folly::to<std::string>('"', kLargeAsciiString, '"');
129
130   for (size_t i = 0; i < iters; ++i) {
131     parseJson(json);
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 }