使用本地的環境測試10萬次和 100萬次 快取的讀寫,測試環境和結果如下。
環境
<code>W<span>in</span>7 x64 AMD7750双核 内存<span>8</span>G Apache <span>2.4</span>.<span>9</span> PHP <span>5.5</span>.<span>12</span> ts vc11 memcache <span>2.2</span>.<span>7</span></code>
程式碼
<code><span><?php</span><span><span>function</span><span>convert</span><span>(<span>$size</span>)</span> {</span><span>$unit</span> = <span>array</span>(<span>'b'</span>, <span>'kb'</span>, <span>'mb'</span>, <span>'gb'</span>, <span>'tb'</span>, <span>'pb'</span>); <span>return</span> @round(<span>$size</span> / pow(<span>1024</span>, (<span>$i</span> = floor(log(<span>$size</span>, <span>1024</span>)))), <span>2</span>) . <span>' '</span> . <span>$unit</span>[<span>$i</span>]; } <span><span>function</span><span>cacheFile</span><span>(<span>$key</span>)</span> {</span><span>$dir</span> = <span>'cache'</span>; <span>if</span> (!is_dir(<span>$dir</span>) && !mkdir(<span>$dir</span>)) { <span>throw</span><span>new</span><span>Exception</span>(<span>" can't make dir $dir"</span>); } <span>$filepath</span> = <span>$dir</span> . DIRECTORY_SEPARATOR . sprintf(<span>'%x'</span>, crc32(<span>$key</span>)); <span>if</span> (!(file_exists(<span>$filepath</span>) && (<span>$data</span> = file_get_contents(<span>$filepath</span>)) && !<span>empty</span>(<span>$data</span>))) { <span>$data</span> = date(<span>'Y-m-d H:i:s'</span>); file_put_contents(<span>$filepath</span>, <span>$data</span>); } <span>return</span><span>$data</span>; } <span><span>function</span><span>cacheMem</span><span>(<span>$key</span>)</span> {</span><span>$mem</span> = <span>new</span> Memcache(); <span>$mem</span>->connect(<span>'127.0.0.1'</span>, <span>11211</span>); <span>$data</span> = <span>$mem</span>->get(<span>$key</span>); <span>if</span> (<span>empty</span>(<span>$data</span>)) { <span>$data</span> = date(<span>'Y-m-d H:i:s'</span>); <span>$mem</span>->set(<span>$key</span>, <span>$data</span>); } <span>return</span><span>$data</span>; } <span>$t1</span> = microtime(<span>true</span>); <span>$i</span> = <span>0</span>; <span>$limit</span> = <span>1000</span> * <span>100</span>; <span>//10 万次</span><span>$data</span> = <span>null</span>; <span>while</span> (<span>$i</span> < <span>$limit</span>) { <span>// $data = cacheFile($i);</span><span>$data</span> = cacheMem(<span>$i</span>); <span>$i</span>++; } <span>$timeUse</span> = microtime(<span>true</span>) - <span>$t1</span>; <span>$arr</span> = [ <span>'cost'</span> => sprintf(<span>'%.7fs'</span>, <span>$timeUse</span>), <span>'mem'</span> => convert(memory_get_usage()) ]; var_dump(<span>$arr</span>); </span></code>
結果1萬次
<code> 花费时间 内存耗费 File <span>1.9</span><span>sec</span><span>250</span>kb Memcache <span>11</span><span>sec</span><span>250</span>kb</code>
結果10萬次
<code> 花费时间 内存耗费 <span>File</span><span>94</span>s <span>251.18</span>KB Memcache 超时<span>120</span>s 报错 </code>
Memcache 10萬次測試失敗,報錯內容如下例直接File檔案快取快,之所以做這個測試,是因為面試的時候我回答自己並沒有使用這個memcache 來做緩存,直接使用File檔案快取(單一伺服器),結果直接被技術官認定為是
很菜鳥。但我通過這樣的測試,雖然是在win下面,但為什麼Memcahe效能不如File ?
還是說我測試的方式有錯誤? 求解 default.fu@foxmail.com版權聲明:本文為部落客原創文章,未經部落客允許不得轉載。