前因:
1
请求接口次数很多,每日两亿多次,主要是有些接口返回数据量很大高达110KB(为了减少请求次数,将多个接口合并成一个导致的)。
后端接口的nginx已经开启gzip,所以做个测试,看看是否在请求时使用压缩解压
php CURL 的扩展安装这里就不说了
用到的curl的两个参数
//在http 请求头加入 gzip压缩
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept-Encoding:gzip'));
//curl返回的结果,采用gzip解压
curl_setopt($ch, CURLOPT_ENCODING, "gzip");
1、不使用压缩解压
$s1 = microtime(true);
$ch = curl_init();
for($i=0; $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";
测试结果 请求100次平均耗时 2.1s 0.021s/次
2、使用压缩解压
$s1 = microtime(true);
$ch = curl_init();
for($i=0; $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";
测试结果 请求100次平均耗时 2.6s 0.026/次
结果
1、不使用压缩比使用压缩 请求一次快 5ms
2、千兆网,在局域网内传输这些数据大概是 0.7ms

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6
Visual web development tools
