|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of the pinepain/php-v8 PHP extension. |
| 5 | + * |
| 6 | + * Copyright (c) 2015-2017 Bogdan Padalko <[email protected]> |
| 7 | + * |
| 8 | + * Licensed under the MIT license: http://opensource.org/licenses/MIT |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view the |
| 11 | + * LICENSE file that was distributed with this source or visit |
| 12 | + * http://opensource.org/licenses/MIT |
| 13 | + */ |
| 14 | + |
| 15 | + |
| 16 | +namespace Pinepain\V8\Tests\Perf; |
| 17 | + |
| 18 | + |
| 19 | +use V8\BooleanValue; |
| 20 | +use V8\Context; |
| 21 | +use V8\FunctionObject; |
| 22 | +use V8\Isolate; |
| 23 | +use V8\NullValue; |
| 24 | +use V8\NumberValue; |
| 25 | +use V8\ObjectValue; |
| 26 | +use V8\StringValue; |
| 27 | +use V8\UndefinedValue; |
| 28 | + |
| 29 | + |
| 30 | +/** |
| 31 | + * @Warmup(2) |
| 32 | + * @Revs(100) |
| 33 | + * @Iterations(10) |
| 34 | + * @BeforeMethods("init") |
| 35 | + */ |
| 36 | +class CreatePrimitiveValue |
| 37 | +{ |
| 38 | + /** |
| 39 | + * @var Isolate |
| 40 | + */ |
| 41 | + private $isolate; |
| 42 | + /** |
| 43 | + * @var Context |
| 44 | + */ |
| 45 | + private $context; |
| 46 | + /** |
| 47 | + * @var FunctionObject |
| 48 | + */ |
| 49 | + private $fnc; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var Context |
| 53 | + */ |
| 54 | + private $context2; |
| 55 | + /** |
| 56 | + * @var FunctionObject |
| 57 | + */ |
| 58 | + private $fnc2; |
| 59 | + |
| 60 | + /** |
| 61 | + * @var callable |
| 62 | + */ |
| 63 | + private $callback; |
| 64 | + |
| 65 | + private $pairs = []; |
| 66 | + |
| 67 | + public function init() |
| 68 | + { |
| 69 | + $this->isolate = $isolate = new Isolate(); |
| 70 | + $this->context = $context = new Context($isolate); |
| 71 | + |
| 72 | + $this->fnc = new FunctionObject($context, function () { |
| 73 | + if ($this->callback) { |
| 74 | + ($this->callback)(); |
| 75 | + } |
| 76 | + }); |
| 77 | + |
| 78 | + $this->context2 = $context2 = new Context($isolate); |
| 79 | + |
| 80 | + $this->fnc2 = new FunctionObject($context2, function () { |
| 81 | + if ($this->callback) { |
| 82 | + ($this->callback)(); |
| 83 | + } |
| 84 | + }); |
| 85 | + } |
| 86 | + |
| 87 | + public function benchOutsideContext() |
| 88 | + { |
| 89 | + $callback = $this->buildCallback(); |
| 90 | + |
| 91 | + $callback(); |
| 92 | + $this->fnc->call($this->context, $this->fnc); |
| 93 | + } |
| 94 | + |
| 95 | + public function benchOutsideContextWithWarm() |
| 96 | + { |
| 97 | + $callback = $this->buildCallback(); |
| 98 | + |
| 99 | + $this->fnc->call($this->context, $this->fnc); |
| 100 | + $callback(); |
| 101 | + } |
| 102 | + |
| 103 | + public function benchWithinContext() |
| 104 | + { |
| 105 | + $this->callback = $this->buildCallback(); |
| 106 | + $this->fnc->call($this->context, $this->fnc); |
| 107 | + } |
| 108 | + |
| 109 | + public function benchWithinContext2() |
| 110 | + { |
| 111 | + $this->callback = $this->buildCallback(); |
| 112 | + $this->fnc2->call($this->context2, $this->fnc2); |
| 113 | + } |
| 114 | + |
| 115 | + public function benchWithinIsolate() |
| 116 | + { |
| 117 | + $cb = $this->buildCallback(); |
| 118 | + $this->fnc->call($this->context, $this->fnc); |
| 119 | + |
| 120 | + $this->isolate->within($cb); |
| 121 | + } |
| 122 | + |
| 123 | + public function benchWithinIsolateLight() |
| 124 | + { |
| 125 | + $cb = $this->buildCallback(); |
| 126 | + |
| 127 | + $this->isolate->within($cb); |
| 128 | + } |
| 129 | + |
| 130 | + public function benchWithinContextNew() |
| 131 | + { |
| 132 | + $cb = $this->buildCallback(); |
| 133 | + $this->fnc->call($this->context, $this->fnc); |
| 134 | + |
| 135 | + $this->context->within($cb); |
| 136 | + } |
| 137 | + |
| 138 | + public function benchWithinContextNewLight() |
| 139 | + { |
| 140 | + $cb = $this->buildCallback(); |
| 141 | + |
| 142 | + $this->context->within($cb); |
| 143 | + } |
| 144 | + |
| 145 | + protected function buildCallback() |
| 146 | + { |
| 147 | + $callback = function ($isolate = null) { |
| 148 | + $isolate = $isolate ?: $this->isolate; |
| 149 | + |
| 150 | + $values =[]; |
| 151 | + for ($i = 0; $i < 200; $i++) { |
| 152 | + $values[] = new UndefinedValue($isolate); |
| 153 | + $values[] = new NullValue($isolate); |
| 154 | + $values[] = new BooleanValue($isolate, true); |
| 155 | + $values[] = new NumberValue($isolate, 123.456); |
| 156 | + $values[] = new StringValue($isolate, 'foo'); |
| 157 | + } |
| 158 | + }; |
| 159 | + |
| 160 | + return $callback; |
| 161 | + } |
| 162 | +} |
0 commit comments