- 論壇徽章:
- 0
|
/dev/shm共享內(nèi)存
最近聽說/dev/shm共享內(nèi)存是天生的memcache,于是在linux中做了一個(gè)測試:
測試一:讀取100000次數(shù)據(jù)。
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);
復(fù)制代碼 連續(xù)運(yùn)行三次運(yùn)行上面的腳本,得到的結(jié)果分別為10.5,10.46,10.63。
Php代碼- 1.//從文件中讀取,test.log的內(nèi)容只有1字節(jié)
- 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的內(nèi)容只有1字節(jié)
- $time = microtime(TRUE);
- for ($i = 0 ; $i < 100000 ; $i++){
- $data = file_get_contents('test.log');
- }
- $end = microtime(TRUE);
- echo round($end - $time , 2);
復(fù)制代碼 連續(xù)運(yùn)行三次該腳本,得到的結(jié)果分別為4.4,3.16,3.16。
Php代碼- 1.//使用共享內(nèi)存/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);
- //使用共享內(nèi)存/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);
復(fù)制代碼 連續(xù)運(yùn)行三次得到的結(jié)果分別為3.2,3.25,3.2。
讀取實(shí)驗(yàn)的結(jié)果是:共享內(nèi)存與讀取本地文件的速度差不多,比memcache快。memcache為啥會(huì)比文件IO慢,我暫時(shí)沒搞清楚,聽別人說可能是因?yàn)閙emcache還有一層網(wǎng)絡(luò)操作。
測試二:寫100000次數(shù)據(jù)。
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);
復(fù)制代碼 連續(xù)三次運(yùn)行得到的結(jié)果分別是10.93,9.22,9.41,跟讀取測試的結(jié)果差不多,說明memcache的讀與寫時(shí)間是差不多的。
Php代碼- 1.//文件系統(tǒng)
- 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);
- //文件系統(tǒng)
- $time = microtime(TRUE);
- for ($i = 0 ; $i < 100000 ; $i++){
- $data = file_put_contents('test.log',0);
- }
- $end = microtime(TRUE);
- echo round($end - $time , 2);
復(fù)制代碼 連續(xù)三次運(yùn)行得到的結(jié)果分別是14.12,13.62,13.34。比讀取測試的結(jié)果差了很多,比memcache慢了一些。我猜測原因可能是讀取的時(shí)候,由于我讀取的是同一塊內(nèi)容,系統(tǒng)可能自動(dòng)給我做了緩存,所以讀比寫快了很多。
Php代碼- 1.//共享內(nèi)存
- 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);
- //共享內(nèi)存
- $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);
復(fù)制代碼 連續(xù)三次運(yùn)行得到的結(jié)果分別是2.5,2.52,2.53。寫跟讀的性能一樣優(yōu)越。
從以上的測試可以看出,直接操作本地的共享內(nèi)存,速度是最快的,比文件操作或者memcache都快。
我總結(jié)了一下其優(yōu)缺點(diǎn):
優(yōu)點(diǎn):讀寫速度最快。
缺點(diǎn):只適合單機(jī)應(yīng)用,不適用于分布式應(yīng)用。重啟機(jī)器會(huì)丟失內(nèi)容(memcache也會(huì)丟)。
在一些特殊的場景中,如果適當(dāng)使用共享內(nèi)存,可能會(huì)使性能得到非常大的提升。例如緩存,PHP session等。 |
|