proxygen
JsonOtherTest.cpp File Reference
#include <folly/json.h>
#include <folly/Benchmark.h>
#include <folly/Conv.h>
#include <folly/FileUtil.h>
#include <folly/Range.h>
#include <folly/portability/GFlags.h>
#include <folly/portability/GTest.h>

Go to the source code of this file.

Functions

 TEST (Json, StripComments)
 
 BENCHMARK (jsonSerialize, iters)
 
 BENCHMARK (jsonSerializeWithNonAsciiEncoding, iters)
 
 BENCHMARK (jsonSerializeWithUtf8Validation, iters)
 
 BENCHMARK (jsonSerializeAsciiWithUtf8Validation, iters)
 
 BENCHMARK (jsonSerializeWithExtraUnicodeEscapes, iters)
 
 BENCHMARK (parseSmallStringWithUtf, iters)
 
 BENCHMARK (parseNormalString, iters)
 
 BENCHMARK (parseBigString, iters)
 
 BENCHMARK (toJson, iters)
 
int main (int argc, char **argv)
 

Variables

constexpr folly::StringPiece kLargeAsciiString
 
constexpr folly::StringPiece kLargeNonAsciiString
 
constexpr folly::StringPiece kLargeAsciiStringWithSpecialChars
 

Function Documentation

BENCHMARK ( jsonSerialize  ,
iters   
)

Definition at line 82 of file JsonOtherTest.cpp.

References i, kLargeNonAsciiString, and folly::json::serialize().

82  {
83  const dynamic obj = kLargeNonAsciiString;
84 
86  for (size_t i = 0; i < iters; ++i) {
87  folly::json::serialize(obj, opts);
88  }
89 }
constexpr folly::StringPiece kLargeNonAsciiString
std::string serialize(dynamic const &dyn, serialization_opts const &opts)
Definition: json.cpp:621
BENCHMARK ( jsonSerializeWithNonAsciiEncoding  ,
iters   
)

Definition at line 91 of file JsonOtherTest.cpp.

References folly::json::serialization_opts::encode_non_ascii, i, kLargeNonAsciiString, and folly::json::serialize().

91  {
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 }
constexpr folly::StringPiece kLargeNonAsciiString
std::string serialize(dynamic const &dyn, serialization_opts const &opts)
Definition: json.cpp:621
BENCHMARK ( jsonSerializeWithUtf8Validation  ,
iters   
)

Definition at line 102 of file JsonOtherTest.cpp.

References i, kLargeNonAsciiString, folly::json::serialize(), and folly::json::serialization_opts::validate_utf8.

102  {
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 }
constexpr folly::StringPiece kLargeNonAsciiString
std::string serialize(dynamic const &dyn, serialization_opts const &opts)
Definition: json.cpp:621
BENCHMARK ( jsonSerializeAsciiWithUtf8Validation  ,
iters   
)

Definition at line 113 of file JsonOtherTest.cpp.

References i, kLargeAsciiString, folly::json::serialize(), and folly::json::serialization_opts::validate_utf8.

113  {
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 }
std::string serialize(dynamic const &dyn, serialization_opts const &opts)
Definition: json.cpp:621
constexpr folly::StringPiece kLargeAsciiString
BENCHMARK ( jsonSerializeWithExtraUnicodeEscapes  ,
iters   
)

Definition at line 124 of file JsonOtherTest.cpp.

References folly::json::buildExtraAsciiToEscapeBitmap(), folly::json::serialization_opts::extra_ascii_to_escape_bitmap, i, kLargeAsciiStringWithSpecialChars, and folly::json::serialize().

124  {
126 
130 
131  for (size_t i = 0; i < iters; ++i) {
132  folly::json::serialize(obj, opts);
133  }
134 }
constexpr folly::StringPiece kLargeAsciiStringWithSpecialChars
std::array< uint64_t, 2 > buildExtraAsciiToEscapeBitmap(StringPiece chars)
Definition: json.cpp:611
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
BENCHMARK ( parseSmallStringWithUtf  ,
iters   
)

Definition at line 136 of file JsonOtherTest.cpp.

References i, and folly::parseJson().

136  {
137  for (size_t i = 0; i < iters << 4; ++i) {
138  parseJson("\"I \\u2665 UTF-8 thjasdhkjh blah blah blah\"");
139  }
140 }
dynamic parseJson(StringPiece range)
Definition: json.cpp:900
BENCHMARK ( parseNormalString  ,
iters   
)

Definition at line 142 of file JsonOtherTest.cpp.

References i, and folly::parseJson().

142  {
143  for (size_t i = 0; i < iters << 4; ++i) {
144  parseJson("\"akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk\"");
145  }
146 }
dynamic parseJson(StringPiece range)
Definition: json.cpp:900
BENCHMARK ( parseBigString  ,
iters   
)

Definition at line 148 of file JsonOtherTest.cpp.

References i, kLargeAsciiString, and folly::parseJson().

148  {
149  const auto json = folly::to<std::string>('"', kLargeAsciiString, '"');
150 
151  for (size_t i = 0; i < iters; ++i) {
152  parseJson(json);
153  }
154 }
dynamic parseJson(StringPiece range)
Definition: json.cpp:900
constexpr folly::StringPiece kLargeAsciiString
BENCHMARK ( toJson  ,
iters   
)

Definition at line 156 of file JsonOtherTest.cpp.

References i, folly::parseJson(), and folly::toJson().

156  {
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 }
dynamic parseJson(StringPiece range)
Definition: json.cpp:900
std::string toJson(dynamic const &dyn)
Definition: json.cpp:915
int main ( int  argc,
char **  argv 
)

Definition at line 165 of file JsonOtherTest.cpp.

References testing::InitGoogleTest(), RUN_ALL_TESTS(), and folly::runBenchmarks().

165  {
167  gflags::ParseCommandLineFlags(&argc, &argv, true);
168  if (FLAGS_benchmark) {
170  }
171  return RUN_ALL_TESTS();
172 }
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: gtest.h:2232
void runBenchmarks()
Definition: Benchmark.cpp:456
char ** argv
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: gtest.cc:5370
TEST ( Json  ,
StripComments   
)

Definition at line 64 of file JsonOtherTest.cpp.

References EXPECT_EQ, FAIL, folly::readFile(), string, and folly::json::stripComments().

64  {
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 }
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
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::string stripComments(StringPiece jsonC)
Definition: json.cpp:837
const char * string
Definition: Conv.cpp:212

Variable Documentation

constexpr folly::StringPiece kLargeAsciiString
Initial value:
=
"akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk"
"akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk"
"akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk"
"akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk"
"akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk"
"akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk"
"akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk"
"akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk"
"akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk"
"akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk"
"akjhfk jhkjlakjhfk jhkjlakjhfk jhkjl akjhfk"

Definition at line 30 of file JsonOtherTest.cpp.

Referenced by BENCHMARK().

constexpr folly::StringPiece kLargeAsciiStringWithSpecialChars
Initial value:
=
"<script>foo%@bar.com</script>"
"<script>foo%@bar.com</script>"
"<script>foo%@bar.com</script>"
"<script>foo%@bar.com</script>"
"<script>foo%@bar.com</script>"
"<script>foo%@bar.com</script>"
"<script>foo%@bar.com</script>"

Definition at line 55 of file JsonOtherTest.cpp.

Referenced by BENCHMARK().

constexpr folly::StringPiece kLargeNonAsciiString
Initial value:
=
"qwerty \xc2\x80 \xef\xbf\xbf poiuy"
"qwerty \xc2\x80 \xef\xbf\xbf poiuy"
"qwerty \xc2\x80 \xef\xbf\xbf poiuy"
"qwerty \xc2\x80 \xef\xbf\xbf poiuy"
"qwerty \xc2\x80 \xef\xbf\xbf poiuy"
"qwerty \xc2\x80 \xef\xbf\xbf poiuy"
"qwerty \xc2\x80 \xef\xbf\xbf poiuy"
"qwerty \xc2\x80 \xef\xbf\xbf poiuy"
"qwerty \xc2\x80 \xef\xbf\xbf poiuy"
"qwerty \xc2\x80 \xef\xbf\xbf poiuy"

Definition at line 43 of file JsonOtherTest.cpp.

Referenced by BENCHMARK().