proxygen
JsonOtherTest.cpp
Go to the documentation of this file.
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>
25 
26 using folly::dynamic;
27 using folly::parseJson;
28 using folly::toJson;
29 
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 
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 
56  "<script>foo%@bar.com</script>"
57  "<script>foo%@bar.com</script>"
58  "<script>foo%@bar.com</script>"
59  "<script>foo%@bar.com</script>"
60  "<script>foo%@bar.com</script>"
61  "<script>foo%@bar.com</script>"
62  "<script>foo%@bar.com</script>";
63 
64 TEST(Json, StripComments) {
65  const std::string kTestDir = "folly/test/";
66  const std::string kTestFile = "json_test_data/commented.json";
67  const std::string kTestExpected = "json_test_data/commented.json.exp";
68 
69  std::string testStr;
70  std::string expectedStr;
71  if (!folly::readFile(kTestFile.data(), testStr) &&
72  !folly::readFile((kTestDir + kTestFile).data(), testStr)) {
73  FAIL() << "can not read test file " << kTestFile;
74  }
75  if (!folly::readFile(kTestExpected.data(), expectedStr) &&
76  !folly::readFile((kTestDir + kTestExpected).data(), expectedStr)) {
77  FAIL() << "can not read test file " << kTestExpected;
78  }
79  EXPECT_EQ(expectedStr, folly::json::stripComments(testStr));
80 }
81 
82 BENCHMARK(jsonSerialize, iters) {
83  const dynamic obj = kLargeNonAsciiString;
84 
86  for (size_t i = 0; i < iters; ++i) {
87  folly::json::serialize(obj, opts);
88  }
89 }
90 
91 BENCHMARK(jsonSerializeWithNonAsciiEncoding, iters) {
92  const dynamic obj = kLargeNonAsciiString;
93 
95  opts.encode_non_ascii = true;
96 
97  for (size_t i = 0; i < iters; ++i) {
98  folly::json::serialize(obj, opts);
99  }
100 }
101 
102 BENCHMARK(jsonSerializeWithUtf8Validation, iters) {
103  const dynamic obj = kLargeNonAsciiString;
104 
106  opts.validate_utf8 = true;
107 
108  for (size_t i = 0; i < iters; ++i) {
109  folly::json::serialize(obj, opts);
110  }
111 }
112 
113 BENCHMARK(jsonSerializeAsciiWithUtf8Validation, iters) {
114  const dynamic obj = kLargeAsciiString;
115 
117  opts.validate_utf8 = true;
118 
119  for (size_t i = 0; i < iters; ++i) {
120  folly::json::serialize(obj, opts);
121  }
122 }
123 
124 BENCHMARK(jsonSerializeWithExtraUnicodeEscapes, iters) {
126 
130 
131  for (size_t i = 0; i < iters; ++i) {
132  folly::json::serialize(obj, opts);
133  }
134 }
135 
136 BENCHMARK(parseSmallStringWithUtf, iters) {
137  for (size_t i = 0; i < iters << 4; ++i) {
138  parseJson("\"I \\u2665 UTF-8 thjasdhkjh blah blah blah\"");
139  }
140 }
141 
142 BENCHMARK(parseNormalString, iters) {
143  for (size_t i = 0; i < iters << 4; ++i) {
144  parseJson("\"akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk\"");
145  }
146 }
147 
148 BENCHMARK(parseBigString, iters) {
149  const auto json = folly::to<std::string>('"', kLargeAsciiString, '"');
150 
151  for (size_t i = 0; i < iters; ++i) {
152  parseJson(json);
153  }
154 }
155 
156 BENCHMARK(toJson, iters) {
157  dynamic something = parseJson(
158  "{\"old_value\":40,\"changed\":true,\"opened\":false,\"foo\":[1,2,3,4,5,6]}");
159 
160  for (size_t i = 0; i < iters; i++) {
161  toJson(something);
162  }
163 }
164 
165 int main(int argc, char** argv) {
166  testing::InitGoogleTest(&argc, argv);
167  gflags::ParseCommandLineFlags(&argc, &argv, true);
168  if (FLAGS_benchmark) {
170  }
171  return RUN_ALL_TESTS();
172 }
BENCHMARK(jsonSerialize, iters)
bool readFile(int fd, Container &out, size_t num_bytes=std::numeric_limits< size_t >::max())
Definition: FileUtil.h:125
#define FAIL()
Definition: gtest.h:1822
constexpr folly::StringPiece kLargeAsciiStringWithSpecialChars
dynamic parseJson(StringPiece range)
Definition: json.cpp:900
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: gtest.h:2232
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::string stripComments(StringPiece jsonC)
Definition: json.cpp:837
void runBenchmarks()
Definition: Benchmark.cpp:456
char ** argv
int main(int argc, char **argv)
constexpr folly::StringPiece kLargeNonAsciiString
std::array< uint64_t, 2 > buildExtraAsciiToEscapeBitmap(StringPiece chars)
Definition: json.cpp:611
const char * string
Definition: Conv.cpp:212
TEST(Json, StripComments)
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: gtest.cc:5370
std::array< uint64_t, 2 > extra_ascii_to_escape_bitmap
Definition: json.h:137
std::string serialize(dynamic const &dyn, serialization_opts const &opts)
Definition: json.cpp:621
std::string toJson(dynamic const &dyn)
Definition: json.cpp:915
constexpr folly::StringPiece kLargeAsciiString