1ffe3c632Sopenharmony_ci<?php
2ffe3c632Sopenharmony_ci
3ffe3c632Sopenharmony_cinamespace Google\Protobuf\Benchmark;
4ffe3c632Sopenharmony_ciini_set('memory_limit', '4096M');
5ffe3c632Sopenharmony_ci
6ffe3c632Sopenharmony_ciconst NAME = "PhpBenchmark.php";
7ffe3c632Sopenharmony_ci
8ffe3c632Sopenharmony_cifunction _require_all($dir, &$prefix) {
9ffe3c632Sopenharmony_ci    // require all php files
10ffe3c632Sopenharmony_ci    foreach (glob("$dir/*") as $path) {
11ffe3c632Sopenharmony_ci        if (preg_match('/\.php$/', $path) &&
12ffe3c632Sopenharmony_ci            substr($path, -strlen(NAME)) != NAME) {
13ffe3c632Sopenharmony_ci                require_once(substr($path, strlen($prefix) + 1));
14ffe3c632Sopenharmony_ci            } elseif (is_dir($path)) {
15ffe3c632Sopenharmony_ci                _require_all($path, $prefix);
16ffe3c632Sopenharmony_ci            }
17ffe3c632Sopenharmony_ci    }
18ffe3c632Sopenharmony_ci}
19ffe3c632Sopenharmony_ci// include all file
20ffe3c632Sopenharmony_ciforeach (explode(PATH_SEPARATOR, get_include_path()) as $one_include_path) {
21ffe3c632Sopenharmony_ci    _require_all($one_include_path, $one_include_path);
22ffe3c632Sopenharmony_ci}
23ffe3c632Sopenharmony_ci
24ffe3c632Sopenharmony_ciuse Benchmarks\BenchmarkDataset;
25ffe3c632Sopenharmony_ci
26ffe3c632Sopenharmony_ciclass BenchmarkMethod
27ffe3c632Sopenharmony_ci{
28ffe3c632Sopenharmony_ci    // $args[0]: dataset
29ffe3c632Sopenharmony_ci    // $args[1]: message class
30ffe3c632Sopenharmony_ci    static function parse(&$args) {
31ffe3c632Sopenharmony_ci        $payloads = $args[0]->getPayload();
32ffe3c632Sopenharmony_ci        for ($i = $payloads->count() - 1; $i >= 0; $i--) {
33ffe3c632Sopenharmony_ci            (new $args[1]())->mergeFromString($payloads->offsetGet($i));
34ffe3c632Sopenharmony_ci        }
35ffe3c632Sopenharmony_ci    }
36ffe3c632Sopenharmony_ci
37ffe3c632Sopenharmony_ci    // $args: array of message
38ffe3c632Sopenharmony_ci    static function serialize(&$args) {
39ffe3c632Sopenharmony_ci        foreach ($args as &$temp_message) {
40ffe3c632Sopenharmony_ci            $temp_message->serializeToString();
41ffe3c632Sopenharmony_ci        }
42ffe3c632Sopenharmony_ci    }
43ffe3c632Sopenharmony_ci}
44ffe3c632Sopenharmony_ci
45ffe3c632Sopenharmony_ciclass Benchmark
46ffe3c632Sopenharmony_ci{
47ffe3c632Sopenharmony_ci    private $benchmark_name;
48ffe3c632Sopenharmony_ci    private $args;
49ffe3c632Sopenharmony_ci    private $benchmark_time;
50ffe3c632Sopenharmony_ci    private $total_bytes;
51ffe3c632Sopenharmony_ci    private $coefficient;
52ffe3c632Sopenharmony_ci
53ffe3c632Sopenharmony_ci    public function __construct($benchmark_name, $args, $total_bytes,
54ffe3c632Sopenharmony_ci        $benchmark_time = 5.0) {
55ffe3c632Sopenharmony_ci            $this->args = $args;
56ffe3c632Sopenharmony_ci            $this->benchmark_name = $benchmark_name;
57ffe3c632Sopenharmony_ci            $this->benchmark_time = $benchmark_time;
58ffe3c632Sopenharmony_ci            $this->total_bytes = $total_bytes;
59ffe3c632Sopenharmony_ci            $this->coefficient = pow (10, 0) / pow(2, 20);
60ffe3c632Sopenharmony_ci    }
61ffe3c632Sopenharmony_ci
62ffe3c632Sopenharmony_ci    public function runBenchmark() {
63ffe3c632Sopenharmony_ci        $t = $this->runBenchmarkWithTimes(1);
64ffe3c632Sopenharmony_ci        $times = ceil($this->benchmark_time / $t);
65ffe3c632Sopenharmony_ci        return $this->total_bytes * $times /
66ffe3c632Sopenharmony_ci        ($times == 1 ? $t : $this->runBenchmarkWithTimes($times)) *
67ffe3c632Sopenharmony_ci        $this->coefficient;
68ffe3c632Sopenharmony_ci    }
69ffe3c632Sopenharmony_ci
70ffe3c632Sopenharmony_ci    private function runBenchmarkWithTimes($times) {
71ffe3c632Sopenharmony_ci        $st = microtime(true);
72ffe3c632Sopenharmony_ci        for ($i = 0; $i < $times; $i++) {
73ffe3c632Sopenharmony_ci            call_user_func_array($this->benchmark_name, array(&$this->args));
74ffe3c632Sopenharmony_ci        }
75ffe3c632Sopenharmony_ci        $en = microtime(true);
76ffe3c632Sopenharmony_ci        return $en - $st;
77ffe3c632Sopenharmony_ci    }
78ffe3c632Sopenharmony_ci}
79ffe3c632Sopenharmony_ci
80ffe3c632Sopenharmony_cifunction getMessageName(&$dataset) {
81ffe3c632Sopenharmony_ci    switch ($dataset->getMessageName()) {
82ffe3c632Sopenharmony_ci        case "benchmarks.proto3.GoogleMessage1":
83ffe3c632Sopenharmony_ci            return "\Benchmarks\Proto3\GoogleMessage1";
84ffe3c632Sopenharmony_ci        case "benchmarks.proto2.GoogleMessage1":
85ffe3c632Sopenharmony_ci            return "\Benchmarks\Proto2\GoogleMessage1";
86ffe3c632Sopenharmony_ci        case "benchmarks.proto2.GoogleMessage2":
87ffe3c632Sopenharmony_ci            return "\Benchmarks\Proto2\GoogleMessage2";
88ffe3c632Sopenharmony_ci        case "benchmarks.google_message3.GoogleMessage3":
89ffe3c632Sopenharmony_ci            return "\Benchmarks\Google_message3\GoogleMessage3";
90ffe3c632Sopenharmony_ci        case "benchmarks.google_message4.GoogleMessage4":
91ffe3c632Sopenharmony_ci            return "\Benchmarks\Google_message4\GoogleMessage4";
92ffe3c632Sopenharmony_ci        default:
93ffe3c632Sopenharmony_ci            exit("Message " . $dataset->getMessageName() . " not found !");
94ffe3c632Sopenharmony_ci    }
95ffe3c632Sopenharmony_ci}
96ffe3c632Sopenharmony_ci
97ffe3c632Sopenharmony_cifunction runBenchmark($file, $behavior_prefix) {
98ffe3c632Sopenharmony_ci    $datafile = fopen($file, "r") or die("Unable to open file " . $file);
99ffe3c632Sopenharmony_ci    $bytes = fread($datafile, filesize($file));
100ffe3c632Sopenharmony_ci    $dataset = new BenchmarkDataset(NULL);
101ffe3c632Sopenharmony_ci    $dataset->mergeFromString($bytes);
102ffe3c632Sopenharmony_ci    $message_name = getMessageName($dataset);
103ffe3c632Sopenharmony_ci    $message_list = array();
104ffe3c632Sopenharmony_ci    $total_bytes = 0;
105ffe3c632Sopenharmony_ci    $payloads = $dataset->getPayload();
106ffe3c632Sopenharmony_ci    for ($i = $payloads->count() - 1; $i >= 0; $i--) {
107ffe3c632Sopenharmony_ci        $new_message = new $message_name();
108ffe3c632Sopenharmony_ci        $new_message->mergeFromString($payloads->offsetGet($i));
109ffe3c632Sopenharmony_ci        array_push($message_list, $new_message);
110ffe3c632Sopenharmony_ci        $total_bytes += strlen($payloads->offsetGet($i));
111ffe3c632Sopenharmony_ci    }
112ffe3c632Sopenharmony_ci
113ffe3c632Sopenharmony_ci    $parse_benchmark = new Benchmark(
114ffe3c632Sopenharmony_ci        "\Google\Protobuf\Benchmark\BenchmarkMethod::parse",
115ffe3c632Sopenharmony_ci        array($dataset, $message_name), $total_bytes);
116ffe3c632Sopenharmony_ci    $serialize_benchmark = new Benchmark(
117ffe3c632Sopenharmony_ci        "\Google\Protobuf\Benchmark\BenchmarkMethod::serialize",
118ffe3c632Sopenharmony_ci        $message_list, $total_bytes);
119ffe3c632Sopenharmony_ci
120ffe3c632Sopenharmony_ci    return array(
121ffe3c632Sopenharmony_ci        "filename" => $file,
122ffe3c632Sopenharmony_ci        "benchmarks" => array(
123ffe3c632Sopenharmony_ci            $behavior_prefix . "_parse" => $parse_benchmark->runBenchmark(),
124ffe3c632Sopenharmony_ci            $behavior_prefix . "_serailize" => $serialize_benchmark->runBenchmark()
125ffe3c632Sopenharmony_ci        ),
126ffe3c632Sopenharmony_ci        "message_name" => $dataset->getMessageName()
127ffe3c632Sopenharmony_ci    );
128ffe3c632Sopenharmony_ci}
129ffe3c632Sopenharmony_ci
130ffe3c632Sopenharmony_ci// main
131ffe3c632Sopenharmony_ci$json_output = false;
132ffe3c632Sopenharmony_ci$results = array();
133ffe3c632Sopenharmony_ci$behavior_prefix = "";
134ffe3c632Sopenharmony_ci
135ffe3c632Sopenharmony_ciforeach ($argv as $index => $arg) {
136ffe3c632Sopenharmony_ci    if ($index == 0) {
137ffe3c632Sopenharmony_ci        continue;
138ffe3c632Sopenharmony_ci    }
139ffe3c632Sopenharmony_ci    if ($arg == "--json") {
140ffe3c632Sopenharmony_ci        $json_output = true;
141ffe3c632Sopenharmony_ci    } else if (strpos($arg, "--behavior_prefix") == 0) {
142ffe3c632Sopenharmony_ci        $behavior_prefix = str_replace("--behavior_prefix=", "", $arg);
143ffe3c632Sopenharmony_ci    }
144ffe3c632Sopenharmony_ci}
145ffe3c632Sopenharmony_ci
146ffe3c632Sopenharmony_ciforeach ($argv as $index => $arg) {
147ffe3c632Sopenharmony_ci    if ($index == 0) {
148ffe3c632Sopenharmony_ci        continue;
149ffe3c632Sopenharmony_ci    }
150ffe3c632Sopenharmony_ci    if (substr($arg, 0, 2) == "--") {
151ffe3c632Sopenharmony_ci        continue;
152ffe3c632Sopenharmony_ci    } else {
153ffe3c632Sopenharmony_ci        array_push($results, runBenchmark($arg, $behavior_prefix));
154ffe3c632Sopenharmony_ci    }
155ffe3c632Sopenharmony_ci}
156ffe3c632Sopenharmony_ci
157ffe3c632Sopenharmony_ciif ($json_output) {
158ffe3c632Sopenharmony_ci    print json_encode($results);
159ffe3c632Sopenharmony_ci} else {
160ffe3c632Sopenharmony_ci    print "PHP protobuf benchmark result:\n\n";
161ffe3c632Sopenharmony_ci    foreach ($results as $result) {
162ffe3c632Sopenharmony_ci        printf("result for test data file: %s\n", $result["filename"]);
163ffe3c632Sopenharmony_ci        foreach ($result["benchmarks"] as $benchmark => $throughput) {
164ffe3c632Sopenharmony_ci            printf("   Throughput for benchmark %s: %.2f MB/s\n",
165ffe3c632Sopenharmony_ci                $benchmark, $throughput);
166ffe3c632Sopenharmony_ci        }
167ffe3c632Sopenharmony_ci    }
168ffe3c632Sopenharmony_ci}
169ffe3c632Sopenharmony_ci
170ffe3c632Sopenharmony_ci?>
171