JSONPathBenchmarks.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace Flow\JSONPath\Test;
  3. use Flow\JSONPath\JSONPath;
  4. use Peekmo\JsonPath\JsonPath as PeekmoJsonPath;
  5. require_once __DIR__ . "/../vendor/autoload.php";
  6. class JSONPathBenchmarks extends \PHPUnit_Framework_TestCase
  7. {
  8. public function testBenchmark()
  9. {
  10. $goessnerJsonPath = new PeekmoJsonPath;
  11. $exampleData = $this->exampleData();
  12. $start1 = microtime(true);
  13. for ($i = 0; $i < 100; $i += 1) {
  14. $results1 = $goessnerJsonPath->jsonPath($exampleData, '$.store.books[?(@."price" < 10)]');
  15. }
  16. $end1 = microtime(true);
  17. $start2 = microtime(true);
  18. for ($i = 0; $i < 100; $i += 1) {
  19. $results2 = (new JSONPath($exampleData))->find('$.store.books[?(@.price < 10)]');
  20. }
  21. $end2 = microtime(true);
  22. $this->assertEquals($results1, $results2->data());
  23. echo "Old JsonPath: " . ($end1 - $start1) . PHP_EOL;
  24. echo "JSONPath: " . ($end2 - $start2) . PHP_EOL;
  25. }
  26. public function testBenchmark2()
  27. {
  28. $goessnerJsonPath = new PeekmoJsonPath;
  29. $exampleData = $this->exampleData();
  30. $start1 = microtime(true);
  31. for ($i = 0; $i < 100; $i += 1) {
  32. $results1 = $goessnerJsonPath->jsonPath($exampleData, '$.store.*');
  33. }
  34. $end1 = microtime(true);
  35. $start2 = microtime(true);
  36. for ($i = 0; $i < 100; $i += 1) {
  37. $results2 = (new JSONPath($exampleData))->find('$.store.*');
  38. }
  39. $end2 = microtime(true);
  40. $this->assertEquals($results1, $results2->data());
  41. echo "Old JsonPath: " . ($end1 - $start1) . PHP_EOL;
  42. echo "JSONPath: " . ($end2 - $start2) . PHP_EOL;
  43. }
  44. public function testBenchmark3()
  45. {
  46. $goessnerJsonPath = new PeekmoJsonPath;
  47. $exampleData = $this->exampleData();
  48. $start1 = microtime(true);
  49. for ($i = 0; $i < 100; $i += 1) {
  50. $results1 = $goessnerJsonPath->jsonPath($exampleData, '$..*');
  51. }
  52. $end1 = microtime(true);
  53. $start2 = microtime(true);
  54. for ($i = 0; $i < 100; $i += 1) {
  55. $results2 = (new JSONPath($exampleData))->find('$..*');
  56. }
  57. $end2 = microtime(true);
  58. $this->assertEquals($results1, $results2->data());
  59. echo "Old JsonPath: " . ($end1 - $start1) . PHP_EOL;
  60. echo "JSONPath: " . ($end2 - $start2) . PHP_EOL;
  61. }
  62. public function testBenchmark4()
  63. {
  64. $goessnerJsonPath = new PeekmoJsonPath;
  65. $exampleData = $this->exampleData();
  66. $start1 = microtime(true);
  67. for ($i = 0; $i < 100; $i += 1) {
  68. $results1 = $goessnerJsonPath->jsonPath($exampleData, '$..price');
  69. }
  70. $end1 = microtime(true);
  71. $exampleData = $this->exampleData(true);
  72. $start2 = microtime(true);
  73. for ($i = 0; $i < 100; $i += 1) {
  74. $results2 = (new JSONPath($exampleData))->find('$..price');
  75. }
  76. $end2 = microtime(true);
  77. $this->assertEquals($results1, $results2->data());
  78. echo "Old JsonPath: " . ($end1 - $start1) . PHP_EOL;
  79. echo "JSONPath: " . ($end2 - $start2) . PHP_EOL;
  80. }
  81. public function exampleData($asArray = true)
  82. {
  83. $json = '
  84. {
  85. "store":{
  86. "books":[
  87. {
  88. "category":"reference",
  89. "author":"Nigel Rees",
  90. "title":"Sayings of the Century",
  91. "price":8.95
  92. },
  93. {
  94. "category":"fiction",
  95. "author":"Evelyn Waugh",
  96. "title":"Sword of Honour",
  97. "price":12.99
  98. },
  99. {
  100. "category":"fiction",
  101. "author":"Herman Melville",
  102. "title":"Moby Dick",
  103. "isbn":"0-553-21311-3",
  104. "price":8.99
  105. },
  106. {
  107. "category":"fiction",
  108. "author":"J. R. R. Tolkien",
  109. "title":"The Lord of the Rings",
  110. "isbn":"0-395-19395-8",
  111. "price":22.99
  112. }
  113. ],
  114. "bicycle":{
  115. "color":"red",
  116. "price":19.95
  117. }
  118. }
  119. }';
  120. return json_decode($json, $asArray);
  121. }
  122. public function exampleDataExtra($asArray = true)
  123. {
  124. $json = '
  125. {
  126. "http://www.w3.org/2000/01/rdf-schema#label":[
  127. {
  128. "@language":"en"
  129. },
  130. {
  131. "@language":"de"
  132. }
  133. ]
  134. }
  135. ';
  136. return json_decode($json, $asArray);
  137. }
  138. }