schematest.cpp 7.0 KB

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