diff --git a/Makefile b/Makefile index 21556ef..d39d6f0 100644 --- a/Makefile +++ b/Makefile @@ -12,6 +12,10 @@ run: build test: go test ./... --cover +bench: + go test -benchmem -bench=. ./php5 + go test -benchmem -bench=. ./php7 + compile: ./php5/php5.go ./php7/php7.go ./scanner/scanner.go rm -f y.output diff --git a/php5/php5_bench_test.go b/php5/php5_bench_test.go new file mode 100644 index 0000000..3573542 --- /dev/null +++ b/php5/php5_bench_test.go @@ -0,0 +1,419 @@ +package php5_test + +import ( + "bytes" + "testing" + + "github.com/z7zmey/php-parser/php5" +) + +func BenchmarkPhp5(b *testing.B) { + src := `bar($a, ...$b); + foo::bar($a, ...$b); + $foo::bar($a, ...$b); + new foo($a, ...$b); + + function foo(bar $bar=null, baz &...$baz) {} + class foo {public function foo(bar $bar=null, baz &...$baz) {}} + function(bar $bar=null, baz &...$baz) {}; + static function(bar $bar=null, baz &...$baz) {}; + + "test"; + "\$test"; + " + test + "; + '$test'; + ' + $test + '; + <<bar()"; + "test ${foo}"; + "test ${foo[0]}"; + "test {$foo->bar()}"; + + if ($a) : + endif; + if ($a) : + elseif ($b): + endif; + if ($a) : + else: + endif; + if ($a) : + elseif ($b): + elseif ($c): + else: + endif; + + while (1) { break; } + while (1) { break 2; } + while (1) : break(3); endwhile; + class foo{ const FOO = 1, BAR = 2; } + class foo{ function bar() {} } + class foo{ public static function &bar() {} } + class foo{ final private function bar() {} protected function baz() {} } + abstract class foo{ abstract public function bar(); } + final class foo extends bar { } + final class foo implements bar { } + final class foo implements bar, baz { } + + const FOO = 1, BAR = 2; + while (1) { continue; } + while (1) { continue 2; } + while (1) { continue(3); } + declare(ticks=1); + declare(ticks=1, strict_types=1) {} + declare(ticks=1): enddeclare; + do {} while(1); + echo $a, 1; + echo($a); + for($i = 0; $i < 10; $i++, $i++) {} + for(; $i < 10; $i++) : endfor; + foreach ($a as $v) {} + foreach ([] as $v) {} + foreach ($a as $v) : endforeach; + foreach ($a as $k => $v) {} + foreach ([] as $k => $v) {} + foreach ($a as $k => &$v) {} + foreach ($a as $k => list($v)) {} + function foo() {} + + function foo() { + __halt_compiler(); + function bar() {} + class Baz {} + return $a; + } + + function foo(array $a, callable $b) {return;} + function &foo() {return 1;} + function &foo() {} + global $a, $b, $$c, ${foo()}; + a: + goto a; + __halt_compiler(); + if ($a) {} + if ($a) {} elseif ($b) {} + if ($a) {} else {} + if ($a) {} elseif ($b) {} elseif ($c) {} else {} + if ($a) {} elseif ($b) {} else if ($c) {} else {} + ?>
1, &$b,); + array(3 =>&$b); + array(&$b, 1=>1, 1, 3 =>&$b); + ~$a; + !$a; + + Foo::Bar; + clone($a); + clone $a; + function(){}; + function($a, $b) use ($c, &$d) {}; + function($a, $b) use (&$c, $d) {}; + function() {}; + foo; + namespace\foo; + \foo; + + empty($a); + empty(Foo); + @$a; + eval($a); + exit; + exit($a); + die(); + die($a); + foo(); + namespace\foo(&$a); + \foo([]); + $foo(yield $a); + + $a--; + $a++; + --$a; + ++$a; + + include $a; + include_once $a; + require $a; + require_once $a; + + $a instanceof Foo; + $a instanceof namespace\Foo; + $a instanceof \Foo; + + isset($a, $b); + isset(Foo); + list() = $b; + list($a, $b) = $b; + list($a[]) = $b; + list(list($a)) = $b; + + $a->foo(); + new Foo; + new namespace\Foo(); + new \Foo(); + print($a); + $a->foo; + $a->foo[1]; + $a->foo->bar->baz()->quux[0]; + $a->foo()[1][1]; + ` + "`cmd $a`;" + ` + ` + "`cmd`;" + ` + ` + "``;" + ` + []; + [1]; + [1=>1, &$b,]; + + Foo::bar(); + namespace\Foo::bar(); + \Foo::bar(); + Foo::$bar(); + $foo::$bar(); + Foo::$bar; + namespace\Foo::$bar; + \Foo::$bar; + $a ? $b : $c; + $a ? : $c; + $a ? $b ? $c : $d : $e; + $a ? $b : $c ? $d : $e; + -$a; + +$a; + $$a; + $$$a; + yield; + yield $a; + yield $a => $b; + yield Foo::class; + yield $a => Foo::class; + + (array)$a; + (boolean)$a; + (bool)$a; + (double)$a; + (float)$a; + (integer)$a; + (int)$a; + (object)$a; + (string)$a; + (unset)$a; + + $a & $b; + $a | $b; + $a ^ $b; + $a && $b; + $a || $b; + $a . $b; + $a / $b; + $a == $b; + $a >= $b; + $a > $b; + $a === $b; + $a and $b; + $a or $b; + $a xor $b; + $a - $b; + $a % $b; + $a * $b; + $a != $b; + $a !== $b; + $a + $b; + $a ** $b; + $a << $b; + $a >> $b; + $a <= $b; + $a < $b; + + $a =& $b; + $a =& new Foo; + $a =& new Foo($b); + $a = $b; + $a &= $b; + $a |= $b; + $a ^= $b; + $a .= $b; + $a /= $b; + $a -= $b; + $a %= $b; + $a *= $b; + $a += $b; + $a **= $b; + $a <<= $b; + $a >>= $b; + + + (new \Foo()); + (new \Foo())->bar()->baz; + (new \Foo())[0][0]; + (new \Foo())[0]->bar(); + + array([0])[0][0]; + "foo"[0]; + foo[0]; + static::foo; + + new $foo; + new $foo::$bar; + new $a->b[0]; + new $a->b{$b ?: null}->$c->d[0];static $a = [1][0]; + + static $a = !1; + static $a = ~1; + static $a = +1; + static $a = -1; + static $a = (1); + static $a = 1 ?: 2; + static $a = 1 ? 2 : 3; + static $a = 1 & 2; + static $a = 1 | 2; + static $a = 1 ^ 2; + static $a = 1 && 2; + static $a = 1 || 2; + static $a = 1 . 2; + static $a = 1 / 2; + static $a = 1 == 2; + static $a = 1 >= 2; + static $a = 1 > 2; + static $a = 1 === 2; + static $a = 1 and 2; + static $a = 1 or 2; + static $a = 1 xor 2; + static $a = 1 - 2; + static $a = 1 % 2; + static $a = 1 * 2; + static $a = 1 != 2; + static $a = 1 !== 2; + static $a = 1 + 2; + static $a = 1 ** 2; + static $a = 1 << 2; + static $a = 1 >> 2; + static $a = 1 <= 2; + static $a = 1 < 2; + static $a = Foo::bar; + static $a = Foo::class; + static $a = __CLASS__; + static $a = Foo; + static $a = namespace\Foo; + static $a = \Foo; + static $a = array(); + static $a = array(1 => 1, 2); + static $a = [1, 2 => 2][0]; + + if (yield 1) {} + Foo::$$bar; + + $foo(); + $foo()[0][0]; + $a{$b}; + ${$a}; + $foo::{$bar}(); + $foo::bar; + ` + + for n := 0; n < b.N; n++ { + php5.Parse(bytes.NewBufferString(src), "test.php") + } +} diff --git a/php7/php7_bench_test.go b/php7/php7_bench_test.go new file mode 100644 index 0000000..3c1e7d5 --- /dev/null +++ b/php7/php7_bench_test.go @@ -0,0 +1,387 @@ +package php7_test + +import ( + "bytes" + "testing" + + "github.com/z7zmey/php-parser/php7" +) + +func BenchmarkPhp7(b *testing.B) { + src := `bar($a, ...$b); + foo::bar($a, ...$b); + $foo::bar($a, ...$b); + new foo($a, ...$b); + /** anonymous class */ + new class ($a, ...$b) {}; + new class {}; + new $foo; + new $foo[1]; + new $foo{$bar}; + new $foo->bar; + new $foo::$bar; + new static::$bar; + + function foo(?bar $bar=null, baz &...$baz) {} + class foo {public function foo(?bar $bar=null, baz &...$baz) {}} + function(?bar $bar=null, baz &...$baz) {}; + static function(?bar $bar=null, baz &...$baz) {}; + + "test"; + "\$test"; + " + test + "; + '$test'; + ' + $test + '; + <<bar()"; + "test ${foo}"; + "test ${foo[0]}"; + "test ${$foo}"; + "test {$foo->bar()}"; + + if ($a) : + endif; + if ($a) : + elseif ($b): + endif; + if ($a) : + else: + endif; + if ($a) : + elseif ($b): + elseif ($c): + else: + endif; + + while (1) { break; } + while (1) { break 2; } + while (1) : break(3); endwhile; + class foo{ public const FOO = 1, BAR = 2; } + class foo{ const FOO = 1, BAR = 2; } + class foo{ function bar() {} } + class foo{ public static function &bar() {} } + class foo{ public static function &bar(): void {} } + abstract class foo{ } + final class foo extends bar { } + final class foo implements bar { } + final class foo implements bar, baz { } + new class() extends foo implements bar, baz { }; + + const FOO = 1, BAR = 2; + while (1) { continue; } + while (1) { continue 2; } + while (1) { continue(3); } + declare(ticks=1); + declare(ticks=1) {} + declare(ticks=1): enddeclare; + do {} while(1); + echo $a, 1; + echo($a); + for($i = 0; $i < 10; $i++, $i++) {} + for(; $i < 10; $i++, $i++) : endfor; + foreach ($a as $v) {} + foreach ($a as $v) : endforeach; + foreach ($a as $k => $v) {} + foreach ($a as $k => &$v) {} + foreach ($a as $k => list($v)) {} + foreach ($a as $k => [$v]) {} + function foo() {} + function foo() {return;} + function &foo() {return 1;} + function &foo(): void {} + global $a, $b; + a: + goto a; + __halt_compiler(); + if ($a) {} + if ($a) {} elseif ($b) {} + if ($a) {} else {} + if ($a) {} elseif ($b) {} elseif ($c) {} else {} + if ($a) {} elseif ($b) {} else if ($c) {} else {} + ?>
1, &$b,); + ~$a; + !$a; + + Foo::Bar; + $foo::Bar; + clone($a); + clone $a; + function(){}; + function($a, $b) use ($c, &$d) {}; + function(): void {}; + foo; + namespace\foo; + \foo; + + empty($a); + @$a; + eval($a); + exit; + exit($a); + die; + die($a); + foo(); + namespace\foo(); + \foo(); + $foo(); + + $a--; + $a++; + --$a; + ++$a; + + include $a; + include_once $a; + require $a; + require_once $a; + + $a instanceof Foo; + $a instanceof namespace\Foo; + $a instanceof \Foo; + + isset($a, $b); + list($a) = $b; + list($a[]) = $b; + list(list($a)) = $b; + + $a->foo(); + new Foo(); + new namespace\Foo(); + new \Foo(); + new class ($a, ...$b) {}; + print($a); + $a->foo; + ` + "`cmd $a`;" + ` + ` + "`cmd`;" + ` + ` + "``;" + ` + []; + [1]; + [1=>1, &$b,]; + + [$a] = $b; + [$a[]] = $b; + [list($a)] = $b; + Foo::bar(); + namespace\Foo::bar(); + \Foo::bar(); + Foo::$bar; + $foo::$bar; + namespace\Foo::$bar; + \Foo::$bar; + $a ? $b : $c; + $a ? : $c; + $a ? $b ? $c : $d : $e; + $a ? $b : $c ? $d : $e; + -$a; + +$a; + $$a; + yield; + yield $a; + yield $a => $b; + yield from $a; + + (array)$a; + (boolean)$a; + (bool)$a; + (double)$a; + (float)$a; + (integer)$a; + (int)$a; + (object)$a; + (string)$a; + (unset)$a; + + $a & $b; + $a | $b; + $a ^ $b; + $a && $b; + $a || $b; + $a ?? $b; + $a . $b; + $a / $b; + $a == $b; + $a >= $b; + $a > $b; + $a === $b; + $a and $b; + $a or $b; + $a xor $b; + $a - $b; + $a % $b; + $a * $b; + $a != $b; + $a !== $b; + $a + $b; + $a ** $b; + $a << $b; + $a >> $b; + $a <= $b; + $a < $b; + $a <=> $b; + + $a =& $b; + $a = $b; + $a &= $b; + $a |= $b; + $a ^= $b; + $a .= $b; + $a /= $b; + $a -= $b; + $a %= $b; + $a *= $b; + $a += $b; + $a **= $b; + $a <<= $b; + $a >>= $b; + + class foo {public function class() {} } + \foo\bar(); + + function foo(&$a, ...$b) { + __halt_compiler(); + function bar() {} + class Baz {} + trait Quux{} + interface Quuux {} + } + + function foo(&$a = 1, ...$b = 1, $c = 1) {} + function foo(array $a, callable $b) {} + abstract final class foo { abstract protected static function bar(); final private function baz() {} } + + (new Foo)->bar; + (new Foo)(); + [$foo][0](); + foo[1](); + "foo"(); + [1]{$foo}(); + ${foo()}; + + Foo::$bar(); + Foo::{$bar[0]}(); + + $foo->$bar; + $foo->{$bar[0]}; + + [1=>&$a, 2=>list($b)]; + ` + + for n := 0; n < b.N; n++ { + php7.Parse(bytes.NewBufferString(src), "test.php") + } +}