Home  >  Article  >  Backend Development  >  Gzip compression performance test in php curl_PHP tutorial

Gzip compression performance test in php curl_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:37:241094browse

Antecedents:

1
There are many requests to the interface, more than 200 million times a day, mainly because some interfaces return a large amount of data up to 110KB (in order to reduce the number of requests, multiple interfaces are merged into one).
The nginx of the backend interface has enabled gzip, so do a test to see if compression and decompression are used during the request
 
The extension installation of php CURL will not be discussed here
Two parameters of curl used
//Add gzip compression to the http request header
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept-Encoding:gzip'));
//The result returned by curl is decompressed using gzip
curl_setopt($ch, CURLOPT_ENCODING, "gzip");
1. Do not use compression or decompression
$s1 = microtime(true);
$ch = curl_init();
for($i=0; $i<100;$i++){
$url="http://192.168.0.11:8080/xxxxx/xxxxx?";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
$data = curl_exec($ch);
}
curl_close($ch);
echo microtime(true)-$s1;
echo "n";
Test results: The average time taken for 100 requests is 2.1s 0.021s/time
2. Use compression and decompression
$s1 = microtime(true);
$ch = curl_init();
for($i=0; $i<100;$i++){
$url="http://192.168.0.1:8080/xxxxx/xxxxx?";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept-Encoding:gzip'));
curl_setopt($ch, CURLOPT_ENCODING, "gzip");
$data = curl_exec($ch);
}
curl_close($ch);
echo microtime(true)-$s1;
echo "n";
Test results: The average time taken for 100 requests is 2.6s 0.026/time
Results
1. Without compression, one request is 5ms faster than using compression
2. Gigabit network, it takes about 0.7ms to transmit these data within the LAN

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/735884.htmlTechArticleAntecedents: 1. There are many interface requests, more than 200 million times a day, mainly because some interfaces return a large amount of data Up to 110KB (resulting from merging multiple interfaces into one in order to reduce the number of requests)...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn