- 论坛徽章:
- 0
|
/dev/shm共享内存
最近听说/dev/shm共享内存是天生的memcache,于是在linux中做了一个测试:
测试一:读取100000次数据。
Php代码- 1.//使用memcache
- 2.require_once 'tools/cache/memcached-client.php';
- 3.$mem = new memcached($options);
- 4.$mem->set('x' , '0');
- 5.$time = microtime(TRUE);
- 6.for ($i = 0 ; $i < 100000 ; $i++){
- 7. $mem->get('x');
- 8.}
- 9.$end = microtime(TRUE);
- 10.echo round($end - $time , 2);
- //使用memcache
- require_once 'tools/cache/memcached-client.php';
- $mem = new memcached($options);
- $mem->set('x' , '0');
- $time = microtime(TRUE);
- for ($i = 0 ; $i < 100000 ; $i++){
- $mem->get('x');
- }
- $end = microtime(TRUE);
- echo round($end - $time , 2);
复制代码 连续运行三次运行上面的脚本,得到的结果分别为10.5,10.46,10.63。
Php代码- 1.//从文件中读取,test.log的内容只有1字节
- 2.$time = microtime(TRUE);
- 3.for ($i = 0 ; $i < 100000 ; $i++){
- 4. $data = file_get_contents('test.log');
- 5.}
- 6.$end = microtime(TRUE);
- 7.echo round($end - $time , 2);
- //从文件中读取,test.log的内容只有1字节
- $time = microtime(TRUE);
- for ($i = 0 ; $i < 100000 ; $i++){
- $data = file_get_contents('test.log');
- }
- $end = microtime(TRUE);
- echo round($end - $time , 2);
复制代码 连续运行三次该脚本,得到的结果分别为4.4,3.16,3.16。
Php代码- 1.//使用共享内存/dev/shm
- 2.$time = microtime(TRUE);
- 3.for ($i = 0 ; $i < 100000 ; $i++){
- 4. $data = file_get_contents('/dev/shm/php_system/vipcache');
- 5.}
- 6.$end = microtime(TRUE);
- 7.echo round($end - $time , 2);
- //使用共享内存/dev/shm
- $time = microtime(TRUE);
- for ($i = 0 ; $i < 100000 ; $i++){
- $data = file_get_contents('/dev/shm/php_system/vipcache');
- }
- $end = microtime(TRUE);
- echo round($end - $time , 2);
复制代码 连续运行三次得到的结果分别为3.2,3.25,3.2。
读取实验的结果是:共享内存与读取本地文件的速度差不多,比memcache快。memcache为啥会比文件IO慢,我暂时没搞清楚,听别人说可能是因为memcache还有一层网络操作。
测试二:写100000次数据。
Php代码- 1.//使用memcache
- 2.$mem = new memcached($options);
- 3.$time = microtime(TRUE);
- 4.for ($i = 0 ; $i < 100000 ; $i++){
- 5. $mem->set('x' , '0');
- 6.}
- 7.$end = microtime(TRUE);
- 8.echo round($end - $time , 2);
- //使用memcache
- $mem = new memcached($options);
- $time = microtime(TRUE);
- for ($i = 0 ; $i < 100000 ; $i++){
- $mem->set('x' , '0');
- }
- $end = microtime(TRUE);
- echo round($end - $time , 2);
复制代码 连续三次运行得到的结果分别是10.93,9.22,9.41,跟读取测试的结果差不多,说明memcache的读与写时间是差不多的。
Php代码- 1.//文件系统
- 2.$time = microtime(TRUE);
- 3.for ($i = 0 ; $i < 100000 ; $i++){
- 4. $data = file_put_contents('test.log',0);
- 5.}
- 6.$end = microtime(TRUE);
- 7.echo round($end - $time , 2);
- //文件系统
- $time = microtime(TRUE);
- for ($i = 0 ; $i < 100000 ; $i++){
- $data = file_put_contents('test.log',0);
- }
- $end = microtime(TRUE);
- echo round($end - $time , 2);
复制代码 连续三次运行得到的结果分别是14.12,13.62,13.34。比读取测试的结果差了很多,比memcache慢了一些。我猜测原因可能是读取的时候,由于我读取的是同一块内容,系统可能自动给我做了缓存,所以读比写快了很多。
Php代码- 1.//共享内存
- 2.$time = microtime(TRUE);
- 3.for ($i = 0 ; $i < 100000 ; $i++){
- 4. $data = file_put_contents('/dev/shm/php_system/vipcache' , '0');
- 5.}
- 6.$end = microtime(TRUE);
- 7.echo round($end - $time , 2);
- //共享内存
- $time = microtime(TRUE);
- for ($i = 0 ; $i < 100000 ; $i++){
- $data = file_put_contents('/dev/shm/php_system/vipcache' , '0');
- }
- $end = microtime(TRUE);
- echo round($end - $time , 2);
复制代码 连续三次运行得到的结果分别是2.5,2.52,2.53。写跟读的性能一样优越。
从以上的测试可以看出,直接操作本地的共享内存,速度是最快的,比文件操作或者memcache都快。
我总结了一下其优缺点:
优点:读写速度最快。
缺点:只适合单机应用,不适用于分布式应用。重启机器会丢失内容(memcache也会丢)。
在一些特殊的场景中,如果适当使用共享内存,可能会使性能得到非常大的提升。例如缓存,PHP session等。 |
|