A test case analysis of gzip compression in PHP curl _php tips

Source: Internet
Author: User
Tags curl php programming php regular expression

In this paper, we analyze the compression performance test of gzip in PHP curl. Share to everyone for your reference, specific as follows:

Antecedent:

Request interface A lot of times, 200 million times a day, mainly some interfaces to return data large up to 110KB (in order to reduce the number of requests, the combination of multiple interfaces into one result).

The nginx of the backend interface is already turned on gzip, so do a test to see if you want to use compression decompression on request

Extended installation of PHP CURL here's the word.

Two parameters of the curl used

In the HTTP request header, add gzip compression
curl_setopt ($ch, Curlopt_httpheader, Array (' Accept-encoding:gzip '));
Curl returns the results, using gzip decompression
curl_setopt ($ch, curlopt_encoding, "gzip");

1, do not use compression decompression

$s 1 = 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)-$s 1;
echo "\ n";

Test results:

Request 100 times Average time consuming 2.1s 0.021s/times

2, use compression decompression

$s 1 = 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)-$s 1;
echo "\ n";

Test results:

Request 100 times Average time consuming 2.6s 0.026/Time

Results Analysis:

1, do not use compression than the use of compression request a fast 5ms

2, gigabit Network, in the LAN transmission of these data is probably 0.7ms

Conclusion:

Temporarily do not use curl compression and decompression

More about PHP Interested readers can view the site topics: "Php Curl Usage Summary", "PHP array" operation Skills Daquan, "PHP Sorting algorithm Summary", "PHP common traversal algorithm and skills summary", "PHP Data structure and algorithm tutorial", " PHP Programming algorithm Summary, "PHP Mathematical Calculation Skills Summary", "PHP Regular Expression Usage summary", "PHP operation and operator Usage Summary", "PHP string (String) Usage summary" and "PHP common database Operation skill Summary"

I hope this article will help you with the PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.