Debian Codegraph @

LspCpp/third_party/rapidjson/test/perftest/schematest.cpp

raw
1 #include "perftest.h"
2
3 #if TEST_RAPIDJSON
4
5 #include "rapidjson/schema.h"
6 #include <ctime>
7 #include <string>
8 #include <vector>
9
10 #define ARRAY_SIZE(a) sizeof(a) / sizeof(a[0])
11
12 using namespace rapidjson;
13
14 RAPIDJSON_DIAG_PUSH
15 #if defined(__GNUC__) && __GNUC__ >= 7
16 RAPIDJSON_DIAG_OFF(format-overflow)
17 #endif
18
19 template <typename Allocator>
20 static char* ReadFile(const char* filename, Allocator& allocator) {
21 const char *paths[] = {
22 "",
23 "bin/",
24 "../bin/",
25 "../../bin/",
26 "../../../bin/"
27 };
28 char buffer[1024];
29 FILE *fp = 0;
30 for (size_t i = 0; i < sizeof(paths) / sizeof(paths[0]); i++) {
31 sprintf(buffer, "%s%s", paths[i], filename);
32 fp = fopen(buffer, "rb");
33 if (fp)
34 break;
35 }
36
37 if (!fp)
38 return 0;
39
40 fseek(fp, 0, SEEK_END);
41 size_t length = static_cast<size_t>(ftell(fp));
42 fseek(fp, 0, SEEK_SET);
43 char* json = reinterpret_cast<char*>(allocator.Malloc(length + 1));
44 size_t readLength = fread(json, 1, length, fp);
45 json[readLength] = '\0';
46 fclose(fp);
47 return json;
48 }
49
50 RAPIDJSON_DIAG_POP
51
52 class Schema : public PerfTest {
53 public:
54 Schema() {}
55
56 virtual void SetUp() {
57 PerfTest::SetUp();
58
59 const char* filenames[] = {
60 "additionalItems.json",
61 "additionalProperties.json",
62 "allOf.json",
63 "anyOf.json",
64 "default.json",
65 "definitions.json",
66 "dependencies.json",
67 "enum.json",
68 "items.json",
69 "maximum.json",
70 "maxItems.json",
71 "maxLength.json",
72 "maxProperties.json",
73 "minimum.json",
74 "minItems.json",
75 "minLength.json",
76 "minProperties.json",
77 "multipleOf.json",
78 "not.json",
79 "oneOf.json",
80 "pattern.json",
81 "patternProperties.json",
82 "properties.json",
83 "ref.json",
84 "refRemote.json",
85 "required.json",
86 "type.json",
87 "uniqueItems.json"
88 };
89
90 char jsonBuffer[65536];
91 MemoryPoolAllocator<> jsonAllocator(jsonBuffer, sizeof(jsonBuffer));
92
93 for (size_t i = 0; i < ARRAY_SIZE(filenames); i++) {
94 char filename[FILENAME_MAX];
95 sprintf(filename, "jsonschema/tests/draft4/%s", filenames[i]);
96 char* json = ReadFile(filename, jsonAllocator);
97 if (!json) {
98 printf("json test suite file %s not found", filename);
99 return;
100 }
101
102 Document d;
103 d.Parse(json);
104 if (d.HasParseError()) {
105 printf("json test suite file %s has parse error", filename);
106 return;
107 }
108
109 for (Value::ConstValueIterator schemaItr = d.Begin(); schemaItr != d.End(); ++schemaItr) {
110 std::string schemaDescription = (*schemaItr)["description"].GetString();
111 if (IsExcludeTestSuite(schemaDescription))
112 continue;
113
114 TestSuite* ts = new TestSuite;
115 ts->schema = new SchemaDocument((*schemaItr)["schema"]);
116
117 const Value& tests = (*schemaItr)["tests"];
118 for (Value::ConstValueIterator testItr = tests.Begin(); testItr != tests.End(); ++testItr) {
119 if (IsExcludeTest(schemaDescription + ", " + (*testItr)["description"].GetString()))
120 continue;
121
122 Document* d2 = new Document;
123 d2->CopyFrom((*testItr)["data"], d2->GetAllocator());
124 ts->tests.push_back(d2);
125 }
126 testSuites.push_back(ts);
127 }
128 }
129 }
130
131 virtual void TearDown() {
132 PerfTest::TearDown();
133 for (TestSuiteList::const_iterator itr = testSuites.begin(); itr != testSuites.end(); ++itr)
134 delete *itr;
135 testSuites.clear();
136 }
137
138 private:
139 // Using the same exclusion in https://github.com/json-schema/JSON-Schema-Test-Suite
140 static bool IsExcludeTestSuite(const std::string& description) {
141 const char* excludeTestSuites[] = {
142 //lost failing these tests
143 "remote ref",
144 "remote ref, containing refs itself",
145 "fragment within remote ref",
146 "ref within remote ref",
147 "change resolution scope",
148 // these below were added to get jsck in the benchmarks)
149 "uniqueItems validation",
150 "valid definition",
151 "invalid definition"
152 };
153
154 for (size_t i = 0; i < ARRAY_SIZE(excludeTestSuites); i++)
155 if (excludeTestSuites[i] == description)
156 return true;
157 return false;
158 }
159
160 // Using the same exclusion in https://github.com/json-schema/JSON-Schema-Test-Suite
161 static bool IsExcludeTest(const std::string& description) {
162 const char* excludeTests[] = {
163 //lots of validators fail these
164 "invalid definition, invalid definition schema",
165 "maxLength validation, two supplementary Unicode code points is long enough",
166 "minLength validation, one supplementary Unicode code point is not long enough",
167 //this is to get tv4 in the benchmarks
168 "heterogeneous enum validation, something else is invalid"
169 };
170
171 for (size_t i = 0; i < ARRAY_SIZE(excludeTests); i++)
172 if (excludeTests[i] == description)
173 return true;
174 return false;
175 }
176
177 Schema(const Schema&);
178 Schema& operator=(const Schema&);
179
180 protected:
181 typedef std::vector<Document*> DocumentList;
182
183 struct TestSuite {
184 TestSuite() : schema() {}
185 ~TestSuite() {
186 delete schema;
187 for (DocumentList::iterator itr = tests.begin(); itr != tests.end(); ++itr)
188 delete *itr;
189 }
190 SchemaDocument* schema;
191 DocumentList tests;
192 };
193
194 typedef std::vector<TestSuite* > TestSuiteList;
195 TestSuiteList testSuites;
196 };
197
198 TEST_F(Schema, TestSuite) {
199 char validatorBuffer[65536];
200 MemoryPoolAllocator<> validatorAllocator(validatorBuffer, sizeof(validatorBuffer));
201
202 const int trialCount = 100000;
203 int testCount = 0;
204 clock_t start = clock();
205 for (int i = 0; i < trialCount; i++) {
206 for (TestSuiteList::const_iterator itr = testSuites.begin(); itr != testSuites.end(); ++itr) {
207 const TestSuite& ts = **itr;
208 GenericSchemaValidator<SchemaDocument, BaseReaderHandler<UTF8<> >, MemoryPoolAllocator<> > validator(*ts.schema, &validatorAllocator);
209 for (DocumentList::const_iterator testItr = ts.tests.begin(); testItr != ts.tests.end(); ++testItr) {
210 validator.Reset();
211 (*testItr)->Accept(validator);
212 testCount++;
213 }
214 validatorAllocator.Clear();
215 }
216 }
217 clock_t end = clock();
218 double duration = double(end - start) / CLOCKS_PER_SEC;
219 printf("%d trials in %f s -> %f trials per sec\n", trialCount, duration, trialCount / duration);
220 printf("%d tests per trial\n", testCount / trialCount);
221 }
222
223 #endif