Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
rapidjsontest.cpp
Go to the documentation of this file.
1// Tencent is pleased to support the open source community by making RapidJSON available.
2//
3// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
4//
5// Licensed under the MIT License (the "License"); you may not use this file except
6// in compliance with the License. You may obtain a copy of the License at
7//
8// http://opensource.org/licenses/MIT
9//
10// Unless required by applicable law or agreed to in writing, software distributed
11// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12// CONDITIONS OF ANY KIND, either express or implied. See the License for the
13// specific language governing permissions and limitations under the License.
14
15#include "perftest.h"
16
17#if TEST_RAPIDJSON
18
19#include "rapidjson/rapidjson.h"
20#include "rapidjson/document.h"
27
28#include <fstream>
29
30#ifdef RAPIDJSON_SSE2
31#define SIMD_SUFFIX(name) name##_SSE2
32#elif defined(RAPIDJSON_SSE42)
33#define SIMD_SUFFIX(name) name##_SSE42
34#elif defined(RAPIDJSON_NEON)
35#define SIMD_SUFFIX(name) name##_NEON
36#else
37#define SIMD_SUFFIX(name) name
38#endif
39
40using namespace rapidjson;
41
42class RapidJson : public PerfTest {
43public:
44 RapidJson() : temp_(), doc_() {}
45
46 virtual void SetUp() {
47 PerfTest::SetUp();
48
49 // temp buffer for insitu parsing.
50 temp_ = (char *)malloc(length_ + 1);
51
52 // Parse as a document
54
55 for (size_t i = 0; i < 7; i++)
56 EXPECT_FALSE(typesDoc_[i].Parse(types_[i]).HasParseError());
57 }
58
59 virtual void TearDown() {
60 PerfTest::TearDown();
61 free(temp_);
62 }
63
64private:
65 RapidJson(const RapidJson&);
66 RapidJson& operator=(const RapidJson&);
67
68protected:
69 char *temp_;
72};
73
74TEST_F(RapidJson, SIMD_SUFFIX(ReaderParseInsitu_DummyHandler)) {
75 for (size_t i = 0; i < kTrialCount; i++) {
76 memcpy(temp_, json_, length_ + 1);
77 InsituStringStream s(temp_);
79 Reader reader;
81 }
82}
83
84TEST_F(RapidJson, SIMD_SUFFIX(ReaderParseInsitu_DummyHandler_ValidateEncoding)) {
85 for (size_t i = 0; i < kTrialCount; i++) {
86 memcpy(temp_, json_, length_ + 1);
87 InsituStringStream s(temp_);
89 Reader reader;
91 }
92}
93
94TEST_F(RapidJson, SIMD_SUFFIX(ReaderParse_DummyHandler)) {
95 for (size_t i = 0; i < kTrialCount; i++) {
96 StringStream s(json_);
98 Reader reader;
99 EXPECT_TRUE(reader.Parse(s, h));
100 }
101}
102
103#define TEST_TYPED(index, Name)\
104TEST_F(RapidJson, SIMD_SUFFIX(ReaderParse_DummyHandler_##Name)) {\
105 for (size_t i = 0; i < kTrialCount * 10; i++) {\
106 StringStream s(types_[index]);\
107 BaseReaderHandler<> h;\
108 Reader reader;\
109 EXPECT_TRUE(reader.Parse(s, h));\
110 }\
111}\
112TEST_F(RapidJson, SIMD_SUFFIX(ReaderParseInsitu_DummyHandler_##Name)) {\
113 for (size_t i = 0; i < kTrialCount * 10; i++) {\
114 memcpy(temp_, types_[index], typesLength_[index] + 1);\
115 InsituStringStream s(temp_);\
116 BaseReaderHandler<> h;\
117 Reader reader;\
118 EXPECT_TRUE(reader.Parse<kParseInsituFlag>(s, h));\
119 }\
120}
121
122TEST_TYPED(0, Booleans)
124TEST_TYPED(2, Guids)
125TEST_TYPED(3, Integers)
126TEST_TYPED(4, Mixed)
127TEST_TYPED(5, Nulls)
128TEST_TYPED(6, Paragraphs)
129
130#undef TEST_TYPED
131
132TEST_F(RapidJson, SIMD_SUFFIX(ReaderParse_DummyHandler_FullPrecision)) {
133 for (size_t i = 0; i < kTrialCount; i++) {
134 StringStream s(json_);
136 Reader reader;
138 }
139}
140
141TEST_F(RapidJson, SIMD_SUFFIX(ReaderParseIterative_DummyHandler)) {
142 for (size_t i = 0; i < kTrialCount; i++) {
143 StringStream s(json_);
145 Reader reader;
147 }
148}
149
150TEST_F(RapidJson, SIMD_SUFFIX(ReaderParseIterativeInsitu_DummyHandler)) {
151 for (size_t i = 0; i < kTrialCount; i++) {
152 memcpy(temp_, json_, length_ + 1);
153 InsituStringStream s(temp_);
155 Reader reader;
157 }
158}
159
160TEST_F(RapidJson, SIMD_SUFFIX(ReaderParseIterativePull_DummyHandler)) {
161 for (size_t i = 0; i < kTrialCount; i++) {
162 StringStream s(json_);
164 Reader reader;
165 reader.IterativeParseInit();
166 while (!reader.IterativeParseComplete()) {
167 if (!reader.IterativeParseNext<kParseDefaultFlags>(s, h))
168 break;
169 }
170 EXPECT_FALSE(reader.HasParseError());
171 }
172}
173
174TEST_F(RapidJson, SIMD_SUFFIX(ReaderParseIterativePullInsitu_DummyHandler)) {
175 for (size_t i = 0; i < kTrialCount; i++) {
176 memcpy(temp_, json_, length_ + 1);
177 InsituStringStream s(temp_);
179 Reader reader;
180 reader.IterativeParseInit();
181 while (!reader.IterativeParseComplete()) {
183 break;
184 }
185 EXPECT_FALSE(reader.HasParseError());
186 }
187}
188
189TEST_F(RapidJson, SIMD_SUFFIX(ReaderParse_DummyHandler_ValidateEncoding)) {
190 for (size_t i = 0; i < kTrialCount; i++) {
191 StringStream s(json_);
193 Reader reader;
195 }
196}
197
198TEST_F(RapidJson, SIMD_SUFFIX(DocumentParseInsitu_MemoryPoolAllocator)) {
199 for (size_t i = 0; i < kTrialCount; i++) {
200 memcpy(temp_, json_, length_ + 1);
201 Document doc;
202 doc.ParseInsitu(temp_);
203 ASSERT_TRUE(doc.IsObject());
204 }
205}
206
207TEST_F(RapidJson, SIMD_SUFFIX(DocumentParseIterativeInsitu_MemoryPoolAllocator)) {
208 for (size_t i = 0; i < kTrialCount; i++) {
209 memcpy(temp_, json_, length_ + 1);
210 Document doc;
212 ASSERT_TRUE(doc.IsObject());
213 }
214}
215
216TEST_F(RapidJson, SIMD_SUFFIX(DocumentParse_MemoryPoolAllocator)) {
217 for (size_t i = 0; i < kTrialCount; i++) {
218 Document doc;
219 doc.Parse(json_);
220 ASSERT_TRUE(doc.IsObject());
221 }
222}
223
224TEST_F(RapidJson, SIMD_SUFFIX(DocumentParseLength_MemoryPoolAllocator)) {
225 for (size_t i = 0; i < kTrialCount; i++) {
226 Document doc;
227 doc.Parse(json_, length_);
228 ASSERT_TRUE(doc.IsObject());
229 }
230}
231
232#if RAPIDJSON_HAS_STDSTRING
233TEST_F(RapidJson, SIMD_SUFFIX(DocumentParseStdString_MemoryPoolAllocator)) {
234 const std::string s(json_, length_);
235 for (size_t i = 0; i < kTrialCount; i++) {
236 Document doc;
237 doc.Parse(s);
238 ASSERT_TRUE(doc.IsObject());
239 }
240}
241#endif
242
243TEST_F(RapidJson, SIMD_SUFFIX(DocumentParseIterative_MemoryPoolAllocator)) {
244 for (size_t i = 0; i < kTrialCount; i++) {
245 Document doc;
246 doc.Parse<kParseIterativeFlag>(json_);
247 ASSERT_TRUE(doc.IsObject());
248 }
249}
250
251TEST_F(RapidJson, SIMD_SUFFIX(DocumentParse_CrtAllocator)) {
252 for (size_t i = 0; i < kTrialCount; i++) {
253 memcpy(temp_, json_, length_ + 1);
255 doc.Parse(temp_);
256 ASSERT_TRUE(doc.IsObject());
257 }
258}
259
260TEST_F(RapidJson, SIMD_SUFFIX(DocumentParseEncodedInputStream_MemoryStream)) {
261 for (size_t i = 0; i < kTrialCount; i++) {
262 MemoryStream ms(json_, length_);
264 Document doc;
265 doc.ParseStream<0, UTF8<> >(is);
266 ASSERT_TRUE(doc.IsObject());
267 }
268}
269
270TEST_F(RapidJson, SIMD_SUFFIX(DocumentParseAutoUTFInputStream_MemoryStream)) {
271 for (size_t i = 0; i < kTrialCount; i++) {
272 MemoryStream ms(json_, length_);
274 Document doc;
275 doc.ParseStream<0, AutoUTF<unsigned> >(is);
276 ASSERT_TRUE(doc.IsObject());
277 }
278}
279
280template<typename T>
281size_t Traverse(const T& value) {
282 size_t count = 1;
283 switch(value.GetType()) {
284 case kObjectType:
285 for (typename T::ConstMemberIterator itr = value.MemberBegin(); itr != value.MemberEnd(); ++itr) {
286 count++; // name
287 count += Traverse(itr->value);
288 }
289 break;
290
291 case kArrayType:
292 for (typename T::ConstValueIterator itr = value.Begin(); itr != value.End(); ++itr)
293 count += Traverse(*itr);
294 break;
295
296 default:
297 // Do nothing.
298 break;
299 }
300 return count;
301}
302
303TEST_F(RapidJson, DocumentTraverse) {
304 for (size_t i = 0; i < kTrialCount; i++) {
305 size_t count = Traverse(doc_);
306 EXPECT_EQ(4339u, count);
307 //if (i == 0)
308 // std::cout << count << std::endl;
309 }
310}
311
312#ifdef __GNUC__
313RAPIDJSON_DIAG_PUSH
314RAPIDJSON_DIAG_OFF(effc++)
315#endif
316
318 ValueCounter() : count_(1) {} // root
319
320 bool EndObject(SizeType memberCount) { count_ += memberCount * 2; return true; }
321 bool EndArray(SizeType elementCount) { count_ += elementCount; return true; }
322
324};
325
326#ifdef __GNUC__
327RAPIDJSON_DIAG_POP
328#endif
329
330TEST_F(RapidJson, DocumentAccept) {
331 for (size_t i = 0; i < kTrialCount; i++) {
332 ValueCounter counter;
333 doc_.Accept(counter);
334 EXPECT_EQ(4339u, counter.count_);
335 }
336}
337
339 typedef char Ch;
340
341 NullStream() /*: length_(0)*/ {}
342 void Put(Ch) { /*++length_;*/ }
343 void Flush() {}
344 //size_t length_;
345};
346
347TEST_F(RapidJson, Writer_NullStream) {
348 for (size_t i = 0; i < kTrialCount; i++) {
350 Writer<NullStream> writer(s);
351 doc_.Accept(writer);
352 //if (i == 0)
353 // std::cout << s.length_ << std::endl;
354 }
355}
356
357TEST_F(RapidJson, SIMD_SUFFIX(Writer_StringBuffer)) {
358 for (size_t i = 0; i < kTrialCount; i++) {
359 StringBuffer s(0, 1024 * 1024);
360 Writer<StringBuffer> writer(s);
361 doc_.Accept(writer);
362 const char* str = s.GetString();
363 (void)str;
364 //if (i == 0)
365 // std::cout << strlen(str) << std::endl;
366 }
367}
368
369#define TEST_TYPED(index, Name)\
370TEST_F(RapidJson, SIMD_SUFFIX(Writer_StringBuffer_##Name)) {\
371 for (size_t i = 0; i < kTrialCount * 10; i++) {\
372 StringBuffer s(0, 1024 * 1024);\
373 Writer<StringBuffer> writer(s);\
374 typesDoc_[index].Accept(writer);\
375 const char* str = s.GetString();\
376 (void)str;\
377 }\
378}
379
380TEST_TYPED(0, Booleans)
382TEST_TYPED(2, Guids)
383TEST_TYPED(3, Integers)
384TEST_TYPED(4, Mixed)
385TEST_TYPED(5, Nulls)
386TEST_TYPED(6, Paragraphs)
387
388#undef TEST_TYPED
389
390TEST_F(RapidJson, SIMD_SUFFIX(PrettyWriter_StringBuffer)) {
391 for (size_t i = 0; i < kTrialCount; i++) {
392 StringBuffer s(0, 2048 * 1024);
394 writer.SetIndent(' ', 1);
395 doc_.Accept(writer);
396 const char* str = s.GetString();
397 (void)str;
398 //if (i == 0)
399 // std::cout << strlen(str) << std::endl;
400 }
401}
402
403TEST_F(RapidJson, internal_Pow10) {
404 double sum = 0;
405 for (size_t i = 0; i < kTrialCount * kTrialCount; i++)
406 sum += internal::Pow10(int(i & 255));
407 EXPECT_GT(sum, 0.0);
408}
409
410TEST_F(RapidJson, SkipWhitespace_Basic) {
411 for (size_t i = 0; i < kTrialCount; i++) {
412 rapidjson::StringStream s(whitespace_);
413 while (s.Peek() == ' ' || s.Peek() == '\n' || s.Peek() == '\r' || s.Peek() == '\t')
414 s.Take();
415 ASSERT_EQ('[', s.Peek());
416 }
417}
418
420 for (size_t i = 0; i < kTrialCount; i++) {
421 rapidjson::StringStream s(whitespace_);
422 rapidjson::SkipWhitespace(s);
423 ASSERT_EQ('[', s.Peek());
424 }
425}
426
427TEST_F(RapidJson, SkipWhitespace_strspn) {
428 for (size_t i = 0; i < kTrialCount; i++) {
429 const char* s = whitespace_ + std::strspn(whitespace_, " \t\r\n");
430 ASSERT_EQ('[', *s);
431 }
432}
433
434TEST_F(RapidJson, UTF8_Validate) {
436
437 for (size_t i = 0; i < kTrialCount; i++) {
438 StringStream is(json_);
439 bool result = true;
440 while (is.Peek() != '\0')
441 result &= UTF8<>::Validate(is, os);
442 EXPECT_TRUE(result);
443 }
444}
445
447 for (size_t i = 0; i < kTrialCount; i++) {
448 FILE *fp = fopen(filename_, "rb");
449 char buffer[65536];
450 FileReadStream s(fp, buffer, sizeof(buffer));
451 while (s.Take() != '\0')
452 ;
453 fclose(fp);
454 }
455}
456
457TEST_F(RapidJson, SIMD_SUFFIX(ReaderParse_DummyHandler_FileReadStream)) {
458 for (size_t i = 0; i < kTrialCount; i++) {
459 FILE *fp = fopen(filename_, "rb");
460 char buffer[65536];
461 FileReadStream s(fp, buffer, sizeof(buffer));
463 Reader reader;
464 reader.Parse(s, h);
465 fclose(fp);
466 }
467}
468
470 for (size_t i = 0; i < kTrialCount; i++) {
471 std::ifstream is(filename_, std::ios::in | std::ios::binary);
472 char buffer[65536];
473 IStreamWrapper isw(is, buffer, sizeof(buffer));
474 while (isw.Take() != '\0')
475 ;
476 is.close();
477 }
478}
479
480TEST_F(RapidJson, IStreamWrapper_Unbuffered) {
481 for (size_t i = 0; i < kTrialCount; i++) {
482 std::ifstream is(filename_, std::ios::in | std::ios::binary);
483 IStreamWrapper isw(is);
484 while (isw.Take() != '\0')
485 ;
486 is.close();
487 }
488}
489
490TEST_F(RapidJson, IStreamWrapper_Setbuffered) {
491 for (size_t i = 0; i < kTrialCount; i++) {
492 std::ifstream is;
493 char buffer[65536];
494 is.rdbuf()->pubsetbuf(buffer, sizeof(buffer));
495 is.open(filename_, std::ios::in | std::ios::binary);
496 IStreamWrapper isw(is);
497 while (isw.Take() != '\0')
498 ;
499 is.close();
500 }
501}
502
503TEST_F(RapidJson, SIMD_SUFFIX(ReaderParse_DummyHandler_IStreamWrapper)) {
504 for (size_t i = 0; i < kTrialCount; i++) {
505 std::ifstream is(filename_, std::ios::in | std::ios::binary);
506 char buffer[65536];
507 IStreamWrapper isw(is, buffer, sizeof(buffer));
509 Reader reader;
510 reader.Parse(isw, h);
511 is.close();
512 }
513}
514
515TEST_F(RapidJson, SIMD_SUFFIX(ReaderParse_DummyHandler_IStreamWrapper_Unbuffered)) {
516 for (size_t i = 0; i < kTrialCount; i++) {
517 std::ifstream is(filename_, std::ios::in | std::ios::binary);
518 IStreamWrapper isw(is);
520 Reader reader;
521 reader.Parse(isw, h);
522 is.close();
523 }
524}
525
526TEST_F(RapidJson, SIMD_SUFFIX(ReaderParse_DummyHandler_IStreamWrapper_Setbuffered)) {
527 for (size_t i = 0; i < kTrialCount; i++) {
528 std::ifstream is;
529 char buffer[65536];
530 is.rdbuf()->pubsetbuf(buffer, sizeof(buffer));
531 is.open(filename_, std::ios::in | std::ios::binary);
532 IStreamWrapper isw(is);
534 Reader reader;
535 reader.Parse(isw, h);
536 is.close();
537 }
538}
539
541 StringBuffer sb;
542 for (int i = 0; i < 32 * 1024 * 1024; i++)
543 sb.Put(i & 0x7f);
544}
545
546#endif // TEST_RAPIDJSON
Input stream wrapper with dynamically bound encoding and automatic encoding detection.
C-runtime library allocator.
Definition allocators.h:75
Input byte stream wrapper with a statically bound encoding.
File byte stream for input using fread().
GenericDocument & ParseInsitu(Ch *str)
Parse JSON text from a mutable string.
Definition document.h:2302
bool HasParseError() const
Whether a parse error has occurred in the last parsing.
Definition document.h:2388
GenericDocument & ParseStream(InputStream &is)
Parse JSON text from an input stream (with Encoding conversion)
Definition document.h:2259
GenericDocument & Parse(const typename SourceEncoding::Ch *str)
Parse JSON text from a read-only string (with Encoding conversion)
Definition document.h:2325
ParseResult Parse(InputStream &is, Handler &handler)
Parse JSON text.
Definition reader.h:557
bool IterativeParseNext(InputStream &is, Handler &handler)
Parse one token from JSON text.
Definition reader.h:618
void IterativeParseInit()
Initialize JSON text token-by-token parsing.
Definition reader.h:605
RAPIDJSON_FORCEINLINE bool IterativeParseComplete() const
Check if token-by-token parsing JSON text is complete.
Definition reader.h:675
bool HasParseError() const
Whether a parse error has occurred in the last parsing.
Definition reader.h:680
Writer with indentation and spacing.
PrettyWriter & SetIndent(Ch indentChar, unsigned indentCharCount)
Set custom indentation.
Document doc_
virtual void TearDown()
virtual void SetUp()
Document typesDoc_[7]
JSON writer.
Definition writer.h:89
os_t os
int * count
#define TEST_F(test_fixture, test_name)
Definition gtest.h:2304
#define ASSERT_EQ(val1, val2)
Definition gtest.h:1988
#define EXPECT_EQ(val1, val2)
Definition gtest.h:1954
#define EXPECT_GT(val1, val2)
Definition gtest.h:1966
#define EXPECT_TRUE(condition)
Definition gtest.h:1895
#define ASSERT_TRUE(condition)
Definition gtest.h:1901
#define EXPECT_FALSE(condition)
Definition gtest.h:1898
double Pow10(int n)
Computes integer powers of 10 in double (10.0^n).
Definition pow10.h:28
main RapidJSON namespace
#define value
Definition pkcs11.h:157
#define T(meth, val, expected)
common definitions and configuration
@ kObjectType
object
Definition rapidjson.h:648
@ kArrayType
array
Definition rapidjson.h:649
RAPIDJSON_NAMESPACE_BEGIN typedef unsigned SizeType
Size type (for string lengths, array sizes, etc.)
Definition rapidjson.h:384
#define SIMD_SUFFIX(name)
#define TEST_TYPED(index, Name)
size_t Traverse(const T &value)
void SkipWhitespace(InputStream &is)
Skip the JSON white spaces in a stream.
Definition reader.h:264
@ kParseFullPrecisionFlag
Parse number in full precision (but slower).
Definition reader.h:151
@ kParseInsituFlag
In-situ(destructive) parsing.
Definition reader.h:147
@ kParseDefaultFlags
Default parse flags. Can be customized by defining RAPIDJSON_PARSE_DEFAULT_FLAGS.
Definition reader.h:156
@ kParseValidateEncodingFlag
Validate encoding of JSON strings.
Definition reader.h:148
@ kParseIterativeFlag
Iterative(constant complexity in terms of function call stack size) parsing.
Definition reader.h:149
Dynamically select encoding according to stream's runtime-specified UTF encoding type.
Definition encodings.h:615
Default implementation of Handler.
Definition reader.h:196
Read-only string stream.
Definition stream.h:154
Represents an in-memory input byte stream.
UTF-8 encoding.
Definition encodings.h:96
bool EndArray(SizeType elementCount)
bool EndObject(SizeType memberCount)
char * s
memcpy((char *) pInfo->slotDescription, s, l)