测试
开篇说过,接口都是用PHP做的,不过请求里的Gzip数据是用LUA处理的,如何让PHP使用LUA处理后的数据呢?不同的语言似乎是个难题,好在Nginx有Phases一说,PHP作为FastCGI模块工作在content阶段,LUA可以工作在access阶段,这样它们就和谐了:
location ~ .php$ {
access_by_lua_file /path/to/lua/file;
include fastcgi.conf;
fastcgi_pass 127.0.0.1:9000;
}
那么lua-zlib和lua-files两种方案效率如何?下面是我用PHP写的测试脚本:
<?php
$url = 'http://url';
$header = implode("rn", array(
'Content-Type: application/x-www-form-urlencoded',
'Content-Encoding: gzip',
'Connection: close',
));
$content = gzencode(http_build_query(array(
'foo' => str_repeat('x', 100),
'bar' => str_repeat('y', 100),
)));
$options = array(
'http' => array(
'protocol_version' => '1.1',
'method' => 'POST',
'header' => $header,
'content' => $content,
),
);
$context = stream_context_create($options);
for ($i = 0; $i < 1000; $i++) {
file_get_contents($url, false, $context);
}
?>
很多人写测试脚本的时候,喜欢在开始结束部分加上时间,这样相减就得到了代码实际运行的时间,其实这是不必要的,利用Linux自带的time就可以获取运行时间:
shell> time php /path/to/php/file








