JSONPathBenchmarks.php 4.62 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
<?php
namespace Flow\JSONPath\Test;
use Flow\JSONPath\JSONPath;
use Peekmo\JsonPath\JsonPath as PeekmoJsonPath;

require_once __DIR__ . "/../vendor/autoload.php";

class JSONPathBenchmarks extends \PHPUnit_Framework_TestCase
{

    public function testBenchmark()
    {
        $goessnerJsonPath = new PeekmoJsonPath;
        $exampleData = $this->exampleData();

        $start1 = microtime(true);
        for ($i = 0; $i < 100; $i += 1) {
            $results1 = $goessnerJsonPath->jsonPath($exampleData, '$.store.books[?(@."price" < 10)]');
        }
        $end1 = microtime(true);

        $start2 = microtime(true);
        for ($i = 0; $i < 100; $i += 1) {
            $results2 = (new JSONPath($exampleData))->find('$.store.books[?(@.price < 10)]');
        }
        $end2 = microtime(true);

        $this->assertEquals($results1, $results2->data());

        echo "Old JsonPath: " . ($end1 - $start1) . PHP_EOL;
        echo "JSONPath: " . ($end2 - $start2) . PHP_EOL;
    }

    public function testBenchmark2()
    {
        $goessnerJsonPath = new PeekmoJsonPath;
        $exampleData = $this->exampleData();

        $start1 = microtime(true);
        for ($i = 0; $i < 100; $i += 1) {
            $results1 = $goessnerJsonPath->jsonPath($exampleData, '$.store.*');
        }
        $end1 = microtime(true);

        $start2 = microtime(true);
        for ($i = 0; $i < 100; $i += 1) {
            $results2 = (new JSONPath($exampleData))->find('$.store.*');
        }
        $end2 = microtime(true);

        $this->assertEquals($results1, $results2->data());

        echo "Old JsonPath: " . ($end1 - $start1) . PHP_EOL;
        echo "JSONPath: " . ($end2 - $start2) . PHP_EOL;
    }

    public function testBenchmark3()
    {
        $goessnerJsonPath = new PeekmoJsonPath;
        $exampleData = $this->exampleData();

        $start1 = microtime(true);
        for ($i = 0; $i < 100; $i += 1) {
            $results1 = $goessnerJsonPath->jsonPath($exampleData, '$..*');
        }
        $end1 = microtime(true);

        $start2 = microtime(true);
        for ($i = 0; $i < 100; $i += 1) {
            $results2 = (new JSONPath($exampleData))->find('$..*');
        }
        $end2 = microtime(true);

        $this->assertEquals($results1, $results2->data());

        echo "Old JsonPath: " . ($end1 - $start1) . PHP_EOL;
        echo "JSONPath: " . ($end2 - $start2) . PHP_EOL;
    }

    public function testBenchmark4()
    {
        $goessnerJsonPath = new PeekmoJsonPath;
        $exampleData = $this->exampleData();

        $start1 = microtime(true);
        for ($i = 0; $i < 100; $i += 1) {
            $results1 = $goessnerJsonPath->jsonPath($exampleData, '$..price');
        }
        $end1 = microtime(true);

        $exampleData = $this->exampleData(true);

        $start2 = microtime(true);
        for ($i = 0; $i < 100; $i += 1) {
            $results2 = (new JSONPath($exampleData))->find('$..price');
        }
        $end2 = microtime(true);

        $this->assertEquals($results1, $results2->data());

        echo "Old JsonPath: " . ($end1 - $start1) . PHP_EOL;
        echo "JSONPath: " . ($end2 - $start2) . PHP_EOL;
    }


    public function exampleData($asArray = true)
    {
        $json = '
        {
          "store":{
            "books":[
              {
                "category":"reference",
                "author":"Nigel Rees",
                "title":"Sayings of the Century",
                "price":8.95
              },
              {
                "category":"fiction",
                "author":"Evelyn Waugh",
                "title":"Sword of Honour",
                "price":12.99
              },
              {
                "category":"fiction",
                "author":"Herman Melville",
                "title":"Moby Dick",
                "isbn":"0-553-21311-3",
                "price":8.99
              },
              {
                "category":"fiction",
                "author":"J. R. R. Tolkien",
                "title":"The Lord of the Rings",
                "isbn":"0-395-19395-8",
                "price":22.99
              }
            ],
            "bicycle":{
              "color":"red",
              "price":19.95
            }
          }
        }';
        return json_decode($json, $asArray);
    }

    public function exampleDataExtra($asArray = true)
    {
        $json = '
            {
               "http://www.w3.org/2000/01/rdf-schema#label":[
                  {
                     "@language":"en"
                  },
                  {
                     "@language":"de"
                  }
               ]
            }
        ';

        return json_decode($json, $asArray);
    }

}