Memcached花了太长时间回复

时间:2016-06-22 09:20:42

标签: php performance caching memcached file-get-contents

我对memcached一个奇怪的问题。我搜索了hereherehereherefew other places关于我的查询。所以我有两页index.phpindex2.php(请不要介意文件命名)。

index.php包含以下代码:

<?php
    $data = file_get_contents('test.txt');
    echo "done";

index2.php包含以下代码:

<?php
function file_get_contents_new($filename, $memcache){
    $time = filemtime($filename);
    $hash = md5($filename.$time);
    $content = $memcache->get($hash);
    if($content){
        return $content;
    }
    $content = file_get_contents($filename);
    $memcache->set($hash, $content, 10000);
    return $content;
}
$memcache = new Memcached;
$memcache->addServer('localhost', 11211);
file_get_contents_new('test.txt', $memcache);
echo "done";

还有一个文件test.txt来自随机网站的html来源,其中58967个字符大约为57.6kb

现在,当我尝试分析index.php时,我得到了以下分析结果(我使用xdebug进行性能分析,使用phpstorm查看数据):

file_get_contents shows 0% time

现在,当我尝试分析index2.php时,我会收到以下快照: shows $memcached->get() takes a lot of time

我们可以清楚地看到$memcache->get()花了很长时间,因为我在本地计算机上运行Memcached,所以没有多大意义。

然后我想也许这只是一些错误并尝试了apache的基准测试工具ab。我执行的确切命令是ab -n 10000 -c 100 http://localhost/index.php 这很快,结果是:

Server Software:        Apache/2.4.20
Server Hostname:        localhost
Server Port:            80

Document Path:          /index.php
Document Length:        4 bytes

Concurrency Level:      100
Time taken for tests:   0.555 seconds
Complete requests:      10000
Failed requests:        0
Total transferred:      2030000 bytes
HTML transferred:       40000 bytes
Requests per second:    18025.33 [#/sec] (mean)
Time per request:       5.548 [ms] (mean)
Time per request:       0.055 [ms] (mean, across all concurrent requests)
Transfer rate:          3573.38 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       1
Processing:     1    5   0.8      5      19
Waiting:        1    5   0.7      5      19
Total:          2    5   0.7      5      19

Percentage of the requests served within a certain time (ms)
  50%      5
  66%      6
  75%      6
  80%      6
  90%      6
  95%      7
  98%      7
  99%      8
 100%     19 (longest request)

然后我做了以下测试ab -n 10000 -c 100 http://localhost/index2.php

Server Software:        Apache/2.4.20
Server Hostname:        localhost
Server Port:            80

Document Path:          /index2.php
Document Length:        4 bytes

Concurrency Level:      100
Time taken for tests:   9.044 seconds
Complete requests:      10000
Failed requests:        0
Total transferred:      2030000 bytes
HTML transferred:       40000 bytes
Requests per second:    1105.72 [#/sec] (mean)
Time per request:       90.439 [ms] (mean)
Time per request:       0.904 [ms] (mean, across all concurrent requests)
Transfer rate:          219.20 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       1
Processing:     6   79  71.1     76    5090
Waiting:        6   79  71.1     76    5090
Total:          7   79  71.1     76    5090

Percentage of the requests served within a certain time (ms)
  50%     76
  66%     78
  75%     79
  80%     81
  90%     85
  95%     89
  98%     93
  99%    107
 100%   5090 (longest request)

这很慢并且很奇怪。为什么从内存中读取比从二级存储中读取要慢。或者他们是否在file_get_contents

上实施了一些缓存

我工作的电脑非常强大,配置如下:

  • MANJARO OS(Linux内核4.1.26-1)
  • 16GB主存储器
  • 256GB SSD
  • 英特尔酷睿i7处理器

编辑:正如@ ShiraNai7评论,我试图将我的服务器URL更改为127.0.0.1及以下是来自apache基准测试工具的结果

Server Software:        Apache/2.4.20
Server Hostname:        localhost
Server Port:            80

Document Path:          /index2.php
Document Length:        4 bytes

Concurrency Level:      100
Time taken for tests:   11.611 seconds
Complete requests:      10000
Failed requests:        0
Total transferred:      2030000 bytes
HTML transferred:       40000 bytes
Requests per second:    861.25 [#/sec] (mean)
Time per request:       116.111 [ms] (mean)
Time per request:       1.161 [ms] (mean, across all concurrent requests)
Transfer rate:          170.74 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    3  47.0      0    1009
Processing:     6  113  67.6    105     633
Waiting:        6  111  67.1    103     633
Total:          6  116  82.5    106    1197

Percentage of the requests served within a certain time (ms)
  50%    106
  66%    135
  75%    153
  80%    167
  90%    204
  95%    235
  98%    286
  99%    334
 100%   1197 (longest request)

这是一项改进,但不是很多。而且我无法理解为什么dns查找需要这么长时间,因为它位于/etc/hosts并且它位于我的本地计算机上。

编辑:我还试图查看是否有任何APC正在进行,我无法找到我找到Opcache模块。这就是为什么file_get_contents更快?

我是hosted一个jsbin,在那里你可以看到我的phpinfo在我的机器中的样子。

1 个答案:

答案 0 :(得分:1)

好吧,我发现了这个问题背后的谜团。第一条线索是file_get_contents非常快。即使我使用SSD,也不应该那么快。所以我整夜挖掘并发现了一些有趣的信息。

这是因为file_get_contents也返回缓存的信息。 PHP本身并不包含缓存,但Linux系统内置了文件缓存,这使得重复访问数据的速度非常快。

参考:page cache