PHP8 JIT性能测试

344 阅读1分钟

测试代码 testjit.php

<?php

function feb($n): int
{
	if($n == 1){
        return 1;
    }

    if($n == 2){
        return 2;
    }

    return feb($n-1) + feb($n-2);
}

$n = 40;

$start = microtime(true);

echo feb($n), PHP_EOL;

$end = microtime(true);

echo $end-$start, PHP_EOL;

docker安装PHP8.0

$ docker pull php:8.0-cli
$ docker run -itd --name php8.0-cli -v /data/www:/www php:8.0-cli
$ docker exec -it php8.0-cli bash

$ cd /www 
$ php testjit.php

165580141
25.239120006561

php7.2 是 28 秒。

配置php.ini

$ php -i |grep php.ini

	Configuration File (php.ini) Path => /usr/local/etc/php

没有列出php.ini文件那说明使用的是默认值

$ cd /usr/local/etc/php
$ ls 
	conf.d  php.ini-development  php.ini-production

$ cp php.ini-production php.ini

$ php -i | grep php.ini
	Configuration File (php.ini) Path => /usr/local/etc/php
	Loaded Configuration File => /usr/local/etc/php/php.ini

这次有了

$ php -m 

发现没有开启 Zend OPcache 

$ echo zend_extension=opcache >> php.ini

带参数执行命令行

$ cd /www
$ php -d opcache.enable_cli=1 -d opcache.jit=1205 -d opcache.jit_buffer_size=64M testjit.php

	165580141
	6.0113999843597

效果还是很明显的吧,4倍左右。

或者编辑配置文件,开启jit

$ apt-get update
$ apt-get install -y vim 

$ vi /usr/local/etc/php/php.ini

	opcache.enable_cli=1
	opcache.jit=1205
	opcache.jit_buffer_size=64M

$ php -i |grep jit

	auto_globals_jit => On => On
	pcre.jit => 1 => 1
	opcache.jit => 1205 => 1205
	opcache.jit_bisect_limit => 0 => 0
	opcache.jit_blacklist_root_trace => 16 => 16
	opcache.jit_blacklist_side_trace => 8 => 8
	opcache.jit_buffer_size => 64M => 64M
	opcache.jit_debug => 0 => 0
	opcache.jit_hot_func => 127 => 127
	opcache.jit_hot_loop => 64 => 64
	opcache.jit_hot_return => 8 => 8
	opcache.jit_hot_side_exit => 8 => 8
	opcache.jit_max_exit_counters => 8192 => 8192
	opcache.jit_max_loop_unrolls => 8 => 8
	opcache.jit_max_polymorphic_calls => 2 => 2
	opcache.jit_max_recursive_calls => 2 => 2
	opcache.jit_max_recursive_returns => 2 => 2
	opcache.jit_max_root_traces => 1024 => 1024
	opcache.jit_max_side_traces => 128 => 128
	opcache.jit_prof_threshold => 0.005 => 0.005

$ php testjit.php

	165580141
	5.8304328918457

对比Golang

package main

import (
	"fmt"
	"time"
)

func main() {
	n := 40

	start := time.Now()
	fmt.Println(feb(n))
	end := time.Now()

	fmt.Println(end.Sub(start))
}

func feb(n int) int {
	if n == 1 {
		return 1
	}

	if n == 2 {
		return 2
	}

	return feb(n-1) + feb(n-2)
}

docker 安装golang

$ docker pull golang:1.14.13
$ docker run -itd --name go1.14 -v /data/www:/www golang:1.14.13
$ docker exec -it go1.14 bash
$ go version
$ go env

配置环境变量

$ echo export GOPATH=/data/www/golang/path >> /etc/profile
$ echo export GOPROXY=https://goproxy.io >> /etc/profile
$ echo export GO111MODULE=on >> /etc/profile
$ echo export PATH=$PATH:/usr/local/go/bin >> /etc/profile
$ echo export PATH=$PATH:/data/www/golang/path/bin >> /etc/profile
$ source /etc/profile
$ go env
$ cd /www/golang/project/demo1/
$ go build main48.go
$ ./main48

	165580141
	2.489641839s

$ go run main48.go

	165580141
	2.565413202s

$ time go run main48.go

	165580141
	2.481155007s

	real    0m3.328s
	user    0m2.866s
	sys     0m0.390s

但是相对于强类型编译型语言还是慢一些。

关于 jit 配置:www.laruence.com/2020/06/27/…