PHP setcookie函数的问题

针对问题的改进点

ab 性能测试

经过上面的说明, 我们做了这么些优化, 是时候看看性能怎么样了。

Asf\Http\Cookie 与 PHP原生的 setcookie 函数做简单的性能测试(ab -c50 -n10000), 测试脚本如下:

PHP setcookie

<?php
for ($i = 0; $i < 10; $i++) {
    setcookie('php', 'helloworld', time() + 3600, '/', '360.cn', 1, 1);
}

Asf Asf\Http\Cookie

<?php
$cookie = new cookie(['path' => '/', 'domain' => '360.cn', 'expire' => 3600, 'secure' => 1, 'httponly' => 1]);
for ($i = 0; $i < 10; $i++) {
    $cookie->set('php', 'helloword');
}

ab 测试结果如下:

PHP setcookie

Concurrency Level:      50
Time taken for tests:   1.197 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Total transferred:      13570000 bytes
HTML transferred:       0 bytes
Requests per second:    8355.01 [#/sec] (mean)
Time per request:       5.984 [ms] (mean)
Time per request:       0.120 [ms] (mean, across all concurrent requests)
Transfer rate:          11072.03 [Kbytes/sec] received

Asf Asf\Http\Cookie

Concurrency Level:      50
Time taken for tests:   0.994 seconds
Failed requests:        0
Write errors:           0
Total transferred:      13470000 bytes
HTML transferred:       0 bytes
Requests per second:    10059.85 [#/sec] (mean)
Time per request:       4.970 [ms] (mean)
Time per request:       0.099 [ms] (mean, across all concurrent requests)
Transfer rate:          13233.02 [Kbytes/sec] received

提示

测试时, 这是一个庞大的header头, 会超出Nginx默认的header头限制, 前端会收到响应 "HTTP/1.1 502 Bad Gateway"。

解决办法: 请按实际情况调整 fastcgi_buffers, fastcgi_buffer_size 两项Nginx配置的大小。