2 * Copyright 2015 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/FileUtil.h>
22 #include <gtest/gtest.h>
23 #include <gflags/gflags.h>
26 using folly::parseJson;
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";
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;
40 if (!folly::readFile(kTestExpected.data(), expectedStr) &&
41 !folly::readFile((kTestDir + kTestExpected).data(), expectedStr)) {
42 FAIL() << "can not read test file " << kTestExpected;
44 EXPECT_EQ(expectedStr, folly::json::stripComments(testStr));
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",
65 BENCHMARK(jsonSerializeWithNonAsciiEncoding, iters) {
66 folly::json::serialization_opts opts;
67 opts.encode_non_ascii = true;
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",
85 BENCHMARK(jsonSerializeWithUtf8Validation, iters) {
86 folly::json::serialization_opts opts;
87 opts.validate_utf8 = true;
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",
105 BENCHMARK(parseSmallStringWithUtf, iters) {
106 for (size_t i = 0; i < iters << 4; ++i) {
107 parseJson("\"I \\u2665 UTF-8 thjasdhkjh blah blah blah\"");
111 BENCHMARK(parseNormalString, iters) {
112 for (size_t i = 0; i < iters << 4; ++i) {
113 parseJson("\"akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk\"");
117 BENCHMARK(parseBigString, iters) {
118 for (size_t i = 0; i < iters; ++i) {
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"
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();