您在這裡

APC Cache + Memcached + Varnish server 三箭齊發設定檔

Felix LIN's 的頭像
Felix LIN 在 2012-02-03 (週五) 17:56 發表

APC:
http://drupal.org/project/apc

Memcache:
http://drupal.org/project/memcache

Varnish Server (2.x/3.0都相容)
http://drupal.org/project/varnish

按照指示,修改varnish server 設定檔:
http://drupal.org/node/1196916

安裝完畢啟用後,接著在settings.php的尾端加上:

# Memcache
include_once('./includes/cache.inc');
include_once('./sites/all/modules/memcache/memcache.inc');
$conf['cache_default_class'] = 'MemCacheDrupal';

# APC
$conf['cache_backends'] = array('sites/all/modules/apc/drupal_apc_cache.inc');
$conf['cache_class_cache'] = 'DrupalAPCCache';
$conf['cache_class_cache_bootstrap'] = 'DrupalAPCCache';

# Varnish
#$conf['cache_backends'] = array('sites/all/modules/varnish/varnish.cache.inc');
$conf['cache_class_cache_page'] = 'VarnishCache';
$conf['page_cache_invoke_hooks'] = false;
$conf['reverse_proxy'] = true;
$conf['cache'] = 1;
$conf['cache_lifetime'] = 0;
$conf['page_cache_maximum_age'] = 21600;
$conf['reverse_proxy_header'] = 'HTTP_X_FORWARDED_FOR';
$conf['reverse_proxy_addresses'] = array('127.0.0.1');
$conf['omit_vary_cookie'] = true;

(http://drupal.org/node/1215564)

最後實測速度比起單裝一個apc或是memcached後,再做全局varnish 反向快取快到小弟實在不會算有幾倍了。

補充一下,如果使用多站台共用資料的話,memcached會產生錯亂,會出現A站、B站、C站的資料內容亂跳。

這個時候在memcache那段的最前面加上:


$conf = array(
'memcache_key_prefix' => '獨特的字串',
);

我是填入該站的網址,或是資料庫名稱做為區別,如此一來就不會出錯了。

參見:
http://drupal.org/node/1132588

今天我又讀了這一篇:http://groups.drupal.org/node/21897

看完之後,我發現drupal7 除了boost(還在DEV)之外,尚有
Cache Heuristic http://drupal.org/project/cache_heuristic
Cache Gracefull http://drupal.org/project/cache_graceful

這兩個模組可以安裝(同一個人寫的),我的設定檔在除了以上的設定之外,加上了:

#Cache Graceful& Heuristic
#$conf['cache_backends'] = array('sites/all/modules/cache_graceful/cache_graceful.inc','sites/all/modules/cache_heuristic/cache_heuristic.inc',);

$conf['cache_default_page_class'] = 'GracefulCache';
$conf['graceful_cache_default_class'] = 'HeuristicCache';
$conf['heuristic_cache_default_class'] = 'DrupalDatabaseCache';

經過我比較加與不加,以指令:ab -c400 -n400 http://example.com/ 為指令來計算的話,平均來說有快那麼一點點。

前50%大概從平均850 進步到820。有多一些快取是有可能在讀同一頁面的時候快那一點點,不過效果實在不如剛加apc+memcahe的時候來得明顯,加上vanish server的時候也有很突出的表現,加這兩個模組的話,就看人高不高興多那麼一滴滴的差別了。

至於Boost的話我在drupal7的時候還沒有實驗過,我的drupal 6站台使用過很長一段時間,站愈大的話它所生成的靜態 html很多。如果不在意空間,或是站的內容不常改變的話的話,其實是可以用的。小弟等boost 進到beta之後再來實驗看看。

我的環境是軟體raid hdd*5跑 raid5+1,Varnish我很久以前就裝上去了,我沒試過有沒有裝Varnish的差別,apc我也是很久以前就裝了,因此我沒試過差異。

另外,我把我的apc的暫存在設在已掛載的tmpfs上,Varnish暫存檔也是,講真的我最近安裝過的東西,安裝後到了人體有感的程度的玩意兒就是memcached/memcache,

之前一樣的首頁,使用一樣的數據,前50%的數據在第一次執行的時候沒差別,第二次之前都有3~5倍的差異。實際數據方面小弟就沒有保留了,當時裝的時候都只有看是否有效,有效的就保留模組。

使用測試的指令為:

#ab -c100 -n100 http://zelee.eu/

今天小弟又裝了一個新站,以下是這三個模組、settings.php 沒有開啟:設定之下跑出來的結果的情況。

This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking zelee.eu (be patient).....done

Server Software: Apache/2.2.16
Server Hostname: zelee.eu
Server Port: 80

Document Path: /
Document Length: 20213 bytes

Concurrency Level: 100
Time taken for tests: 6.279 seconds
Complete requests: 100
Failed requests: 0
Write errors: 0
Total transferred: 2063000 bytes
HTML transferred: 2021300 bytes
Requests per second: 15.92 [#/sec] (mean)
Time per request: 6279.453 [ms] (mean)
Time per request: 62.795 [ms] (mean, across all concurrent requests)
Transfer rate: 320.83 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 4 16 4.0 18 20
Processing: 85 3136 1834.8 3135 6260
Waiting: 83 3134 1834.9 3133 6258
Total: 89 3152 1835.4 3154 6277

Percentage of the requests served within a certain time (ms)
50% 3154
66% 4181
75% 4776
80% 5109
90% 5765
95% 6044
98% 6219
99% 6277
100% 6277 (longest request)

下面是安裝好之後再測結果:

This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking zelee.eu (be patient).....done

Server Software: Apache/2.2.16
Server Hostname: zelee.eu
Server Port: 80

Document Path: /
Document Length: 20213 bytes

Concurrency Level: 100
Time taken for tests: 0.362 seconds
Complete requests: 100
Failed requests: 0
Write errors: 0
Total transferred: 2062300 bytes
HTML transferred: 2021300 bytes
Requests per second: 276.32 [#/sec] (mean)
Time per request: 361.900 [ms] (mean)
Time per request: 3.619 [ms] (mean, across all concurrent requests)
Transfer rate: 5564.98 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 4 16 4.7 18 21
Processing: 147 257 58.3 251 337
Waiting: 136 205 53.6 189 309
Total: 151 273 62.5 268 358

Percentage of the requests served within a certain time (ms)
50% 268
66% 309
75% 337
80% 341
90% 349
95% 357
98% 358
99% 358
100% 358 (longest request)

以下是第三次執行測試,通常安裝完畢之後,第二次執行同一頁面,速度會再加速一點:

This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking zelee.eu (be patient).....done

Server Software: Apache/2.2.16
Server Hostname: zelee.eu
Server Port: 80

Document Path: /
Document Length: 20213 bytes

Concurrency Level: 100
Time taken for tests: 0.236 seconds
Complete requests: 100
Failed requests: 0
Write errors: 0
Total transferred: 2062300 bytes
HTML transferred: 2021300 bytes
Requests per second: 423.30 [#/sec] (mean)
Time per request: 236.237 [ms] (mean)
Time per request: 2.362 [ms] (mean, across all concurrent requests)
Transfer rate: 8525.19 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 4 15 4.5 16 20
Processing: 102 158 41.0 162 213
Waiting: 21 73 42.9 66 143
Total: 106 173 44.8 178 232

Percentage of the requests served within a certain time (ms)
50% 178
66% 193
75% 224
80% 224
90% 226
95% 227
98% 228
99% 232
100% 232 (longest request)

不太看懂你的意思...

因為靜態 CACHES 很易超過你的結果
ab -c100 -n100 結果超過 100 reqs /s 很多時候並不準確


Server Software: nginx
Server Hostname: ---------
Server Port: 80

Document Path: /
Document Length: 26379 bytes

Concurrency Level: 100
Time taken for tests: 1.052 seconds
Complete requests: 5000
Failed requests: 0
Write errors: 0
Total transferred: 133159600 bytes
HTML transferred: 132053274 bytes
Requests per second: 4753.12 [#/sec] (mean)
Time per request: 21.039 [ms] (mean)
Time per request: 0.210 [ms] (mean, across all concurrent requests)
Transfer rate: 123617.84 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 2 1.7 2 18
Processing: 5 19 3.8 19 35
Waiting: 1 5 2.5 4 16
Total: 7 21 3.8 21 36

Percentage of the requests served within a certain time (ms)
50% 21
66% 22
75% 23
80% 23
90% 25
95% 27
98% 30
99% 32
100% 36 (longest request)

而且測試中 [+/-sd] (Standard Deviation) 不應該這麼大