Preface
Hello, everyone. I’m CrazyCodes. I recently investigated how PHP requests go through gRPC and PHP requests through HTTP. According to the increasing amount of data, the average response time will be much different.
Declaration of conformity
The test reports are all tested by the development machine without any configuration changes.
Native configuration
- 3 GHz six-core Intel Core i5
- 16 GB 2400 MHz DDR4
- MacOs
Go start HTTP
http.ListenAndServe
PHP start HTTP
php -S 127.0.0.1:8088
Ab
ab -c 200 -n 200 -k
test data description
As the server, Go is the test data iterated directly through for, without any other redundant operations
// 伪代码
for i := 0; i < 2,20,200,2000,5000; i++ {
}
Other instructions
The GPH[number] below represents gRPC + PHP + HTTP + data volume
GPH 2
returns the same data set size
gRPC Document Length≈87bytes
Http Document Length≈315bytes
Go Grpc -> Go Grpc average response time is 66.215 ms
Php Grpc -> Go Grpc average response time is 190.551 ms
Php Http -> Go Http average response time is 120.692 ms
GPH 20
returns the same data set size
gRPC Document Length≈556bytes
Http Document Length≈2198bytes
Go Grpc -> Go Grpc average response time is 71.525 ms
Php Grpc -> Go Grpc average response time is 212.019 ms
Php Http -> Go Http average response time is 138.332 ms
GPH 200
returns the same data set size
gRPC Document Length≈5527bytes
Http Document Length≈21199bytes
Go Grpc -> Go Grpc average response time is 93.963 ms
Php Grpc -> Go Grpc average response time is 250.104 ms
Php Http -> Go Http average response time is 175.916 ms
GPH 2000
returns the same data set size
gRPC Document Length≈56928bytes
Http Document Length≈213000bytes
Go Grpc -> Go Grpc average response time is 379.699 ms
Php Grpc -> Go Grpc average response time is 678.643 ms
Php Http -> Go Http average response time is 593.252 ms
GPH 5000
returns the same data set size
gRPC Document Length≈143928bytes
Http Document Length≈534000bytes
Go Grpc -> Go Grpc average response time is 920.897 ms
Php Grpc -> Go Grpc average response time is 1406.589 ms
Php Http -> Go Http average response time is 1285.640 ms
in conclusion
Way | 2 | 20 | 200 | 2000 | 5000 |
---|---|---|---|---|---|
PHP->gRPC | 190.551/ms | 212.332 | 250.104 | 678.643 | 1406.589 |
Go->gRPC | 66.215/ms | 71.525 | 93.963 | 379.699 | 920.897 |
PHP->Http | 120.692/ms | 138.332 | 175.916 | 593.252 | 1285.640 |
According to the test results, it is not difficult to see that when building microservices, it is the most undesirable situation for PHP to request via gRPC. In terms of response time, PHP invoking via Http is slightly better than PHP via gRPC in terms of response time. Some, but with the continuous growth of data and concurrency, this gap will continue to grow. Therefore, when deciding to use PHP as the client, it is best to use the traditional HTTP method to call.
Wan Zun
You must think that PHP has been crushed in doing microservices, but this is actually not a fault of PHP itself. We all know that PHP is an interpreted language, and Go is a compiled language. Go is pre-compiled and can be directly generated. Execute the file, and PHP is dynamically compiled.
So why is it so slow to call through gRPC in PHP? This depends on the package we use officially provided by Google. The package encapsulates the data in the class , and the class is not as small and exquisite as the structure of Go. When the data continues to grow, the class will expand exponentially, and we When I see that PHP is called through HTTP, although the content body is several times that of gRPC calls, the time is reduced. The reason for this is also very simple. is called through HTTP, and the communication must be carried out first, and the HTTP header is encapsulated, the content body Wait, this has caused the request body to be very large, but because it is a direct request for a result, and the class is not passed, the time is shorter than the gRPC call, which is one of the reasons why PHP uses gRPC slowly.
The second is what we call the characteristics of PHP itself, to the user after dynamic compilation. This process also takes time, but Go does not need this time, so this is also a key factor affecting response time.
Thanks
Thank you for seeing here, I hope I can help you.
The first publication of such analysis-related articles may have many shortcomings and accept various adjustments.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。