Add php protobuffer support for transition to GTFS-realtime
[busui.git] / lib / Protobuf-PHP / tests / benchmark.php
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
<?php
 
require_once 'Benchmark/Profiler.php';
require_once __DIR__ . '/../library/DrSlump/Protobuf.php';
 
use \DrSlump\Protobuf;
 
Protobuf::autoload();
 
include_once __DIR__ . '/protos/simple.php';
include_once __DIR__ . '/protos/addressbook.php';
 
 
class Benchmark {
 
    protected $tests = array(
        'DecodeBinarySimple',
        'DecodeJsonSimple',
    );
 
    public function run($iterations = 1000)
    {
        $profiler = new Benchmark_Profiler(true);
        foreach ($this->tests as $test) {
            $method = 'config' . $test;
            $args = $this->$method();
 
            $method = 'run' . $test;
            $profiler->enterSection($test);
            for ($i=0; $i<$iterations; $i++) {
                call_user_func_array(array($this, $method), $args);
            }
            $profiler->leaveSection($test);
        }
 
        $profiler->stop();
        $profiler->display();
    }
 
    protected function configDecodeBinarySimple()
    {
        return array(
            new Protobuf\Codec\Binary(),
            file_get_contents(__DIR__ . '/protos/simple.bin')
        );
    }
 
    protected function runDecodeBinarySimple($codec, $data)
    {
        $codec->decode(new tests\Simple(), $data);
    }
 
    protected function configDecodeJsonSimple()
    {
        $codecBin = new Protobuf\Codec\Binary();
        $codecJson = new Protobuf\Codec\Json();
 
        $bin = $this->configDecodeBinarySimple();
        $simple = $codecBin->decode(new tests\Simple(), $bin[1]);
        $data = $codecJson->encode($simple);
        return array($codecJson, $data);
    }
 
    protected function runDecodeJsonSimple($codec, $data)
    {
        $codec->decode(new tests\Simple(), $data);
    }
}
 
 
$bench = new Benchmark();
$bench->run(1000);