天天看點

壓力測試及緩存伺服器 解決方法:進入源碼包的ext/mysqli目錄再執行phpize即可。或者直接安裝php-devel即可。

第一篇 WEB壓力測試

一、常用工具

常用壓力測試工具:ab,httpd_load,webbench,seige。

僅僅作為測試使用,與實際能力差別也挺大的。

比較好的測試工具:惠普公司的loadrunning(收費,不好找),網易公司的tcpcopy,

二、ab

網站壓力測試,模拟并發n個用戶端同時發送n個請求,共m個并發請求。

前提條件:調高并發通路檔案的最大限度 ulimit

[root@localhost php-5.5.32]# ulimit -n 3000

三、使用方法:

-c:模拟的并發使用者數

-n:總的使用者請求數量

[root@localhost php-5.5.32]# ab -c 10 -n 100 http://www.baidu.com/index.html

   //需要指明具體通路的檔案,否則報錯。

This is ApacheBench, Version 2.3 <$Revision: 1706008 $>

Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/

Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking www.baidu.com (be patient).....done

Server Software:        BWS/1.1

Server Hostname:        www.baidu.com

Server Port:            80

Document Path:          /index.html

Document Length:        111566 bytes

Concurrency Level:      10 并發等級,同時通路的使用者數量

Time taken for tests:   3.484 seconds 總耗時

Complete requests:      100 完成響應數

Failed requests:        98 失敗請求數

   (Connect: 0, Receive: 0, Length: 98, Exceptions: 0)

Total transferred:      11246439 bytes

HTML transferred:       11151543 bytes

Requests per second:    28.71 [#/sec] (mean) 每秒請求數

Time per request:       348.369 [ms] (mean) 每10個請求消耗的時間

Time per request:       34.837 [ms] (mean, across all concurrent requests)每個請求消耗時間

Transfer rate:          3152.65 [Kbytes/sec] received 傳輸速率

Connection Times (ms)

              min  mean[+/-sd] median   max

Connect:        2    6   4.6      5      30

Processing:    19  223 448.6     53    3479

Waiting:        4   23  48.1      9     212

Total:         22  230 448.8     59    3483

Percentage of the requests served within a certain time (ms)

  50%     59

  66%     76

  75%    298

  80%    341

  90%    736

  95%    864

  98%   1606

  99%   3483

 100%   3483 (longest request)

[root@localhost php-5.5.32]# 

第二篇 PHP緩存-xcache

一、Xcache加速器

使用方法:将xcache編譯安裝成PHP的一個子產品。

二、安裝使用:

1、下載下傳源代碼、解壓縮

2、執行編譯安裝的php的phpize指令

[root@localhost xcache-3.2.0]# /usr/local/php/bin/phpize

Configuring for:

PHP Api Version:         20121113

Zend Module Api No:      20121212

Zend Extension Api No:   220121212

3、編譯xcache

[root@localhost xcache-3.2.0]# ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config

[root@localhost xcache-3.2.0]#make && make install

4、配置xcache.ini檔案

可以将源碼中的xcache.ini檔案重定向追加到php.ini中,也可以在php.d目錄下建立一個*.ini的檔案,并修改配置檔案。

[root@localhost xcache-3.2.0]# cat xcache.ini >> /etc/php.ini 

Or

[root@localhost xcache-3.2.0]# mkdir /etc/php.d

[root@localhost xcache-3.2.0]# cp xcache.ini /etc/php.d/

extension = xcache.so,保留預設即可。

[root@localhost xcache-3.2.0]# vim /etc/php.d/xcache.ini 

;; this is an example, it won't work unless properly configured into php.ini

[xcache-common]

;; non-Windows example:

extension = xcache.so

5、檢查是否正确配置。

通過phpinfo這樣的簡單腳本即可檢查xcache是否挂載上,如果沒有挂載,可以檢查php的配置檔案檢查位置是否和實際相同。

XCache Version 

3.2.0 

Modules Built 

cacher 

Directive

Local Value

Master Value

xcache.coredump_directory

no value

xcache.disable_on_crash

Off

xcache.experimental

xcache.test

通過web頁面檢視緩存使用 情況及操作(此處實驗環境是xcache3.2、php5.5)

未使用xcache進行緩存後壓力測試結果

Concurrency Level:      1000

Time taken for tests:   17.992 seconds

Complete requests:      1000

Failed requests:        95

   (Connect: 0, Receive: 0, Length: 95, Exceptions: 0)

Total transferred:      82144897 bytes

HTML transferred:       81977897 bytes

Requests per second:    55.58 [#/sec] (mean)

Time per request:       17992.489 [ms] (mean)

Time per request:       17.992 [ms] (mean, across all concurrent requests)

Transfer rate:          4458.51 [Kbytes/sec] received

使用xcache進行壓力測試結果:

Time taken for tests:   2.134 seconds

Failed requests:        905

   (Connect: 0, Receive: 0, Length: 905, Exceptions: 0)

Total transferred:      78216889 bytes

HTML transferred:       78049889 bytes

Requests per second:    468.66 [#/sec] (mean)

Time per request:       2133.730 [ms] (mean)

Time per request:       2.134 [ms] (mean, across all concurrent requests)

Transfer rate:          35798.19 [Kbytes/sec] received

a、将源代碼中的htdocs目錄下的檔案拷貝到web伺服器可以通路到的連結下、我這裡是/usr/local/nginx/html/xcache。

b、編輯php.ini(xcache.ini)檔案,為網頁添加使用者名密碼,密碼是md5加密的。

echo  -n password |openssl md5

c、vi /etc/php.ini

xcache.admin.user = "root"

xcache.admin.pass = "63a9f0ea7bb98050796b649e85481845"

d、重新開機php-fpm服務。

注意:檢查檔案版本是否存在互相支援的問題,比如xcache和php之間的版本是否支援,這樣的問題适用于所有的安裝情況!!!

memcached:緩存的是mysql查詢的結果。并且緩存的每個數值不能超過1兆。

xcache:用來緩存有zend  engine引擎編譯php腳本,實質上是opcode資料。

第三篇 memcached

一、memcached安裝和配置

1、介紹

    在沒有給memcached下定義之前,我先說幾個要點,友善更好地了解memcached。

    首先memcached是一個分布式的高速緩存系統,分布式說明可以部署多個,并且他和應用伺服器之間通訊是通過獨立的文本協定完成的。另外memcached的緩存是緩存在記憶體中而不是硬碟上,是以稱為高速。

    多個memcached之間并不互相通訊。memcached緩存的資料是由應用程式伺服器決定的,而不是由自己。這樣就要去應用程式伺服器比如php在開發的時候就要支援memcached功能,否則部署沒有意義。

    那麼當有多個緩存伺服器的時候,資料是如何緩存的呢,這裡先說一下,由于緩存伺服器之間不通訊,不存在同步緩存的事情,那麼資料緩存之後,在查找緩存的時候就需要知道去哪裡找的問題。

    資料緩存的方法有兩種:取餘法和一緻性hash算法。

    取餘法,就是對資料進行hash取餘,并将結果放到指定的伺服器上,缺點是如果增加新額裝置,取餘結果混亂,造成無法正确查找資料。

    一緻性hash算法,這個比較複雜,簡單說就是假定有這樣一個環,對所有主機進行hash計算,然後通過2的32次幂的取餘運算,并生成一個唯一的數作為伺服器的id号,應用伺服器在對資料查詢的時候,會依據hash計算查詢語句,并計算一個唯一值,同樣放到這個環上,并順時針查找離自己最近的緩存伺服器,然後将資料緩存到這裡。由于一緻性hash算法的特定可能會造成這個環上伺服器負責區域大小範圍不同,是以提供了虛拟的功能,就是可以為同一個主機建立多個點,來平均配置設定區域。來實作區域的均衡。

    這樣即使一台伺服器添加或者缺失,隻會影響一部分資料(資料順時針查找的一個固定範圍),隻需應用伺服器重新查找并緩存即可。

重點提示:由于緩存都是存儲在記憶體中,是以重新開機memcached伺服器會造成資料丢失,需要重建立立緩存。

memcached本身并不提供備援功能,是以當一台緩存伺服器故障後,和他關聯的持久連結session就會失效造成資料丢失,這個是不能避免的。

解決此辦法,而又可以實作高效的資料存儲就出現了nosql和redis的技術。

2、安裝

使用的端口:

11211:tcp 

11211:udp

依賴軟體包:

libevent

     libevent提供高性能的網絡代碼庫,并且目前很多主流的網絡事件工具都是依據此庫開發的。

2.1編譯安裝:(redhat可以通過yum方式自動完成memcached的安裝,這裡隻是提供另一種安裝方式)

a、安裝libevent

[[email protected] ~/libevent-2.0.22-stable]# ./configure --prefix=/usr/local/libevent

[[email protected] ~/libevent-2.0.22-stable]# make && make install

将頭檔案和庫檔案導出到linux系統預設的路徑,友善其他程式調用。

建立頭檔案的連結

[[email protected] ~/libevent-2.0.22-stable]# ln -sv /usr/local/libevent/incloud /usr/include/libevent 

"/usr/include/libevent" -> "/usr/local/libevent/incloud"

建立libevent.conf,添加/usr/local/libevent/lib。

[root@227 magedu.com]# cat /etc/ld.so.conf.d/libevent.conf

/usr/local/libevent/lib

動态連結庫管理工具、執行此指令用于将新的庫檔案加載進核心。

[root@227 magedu.com]# ldconfig 

b、安裝

[[email protected] ~/memcached-1.4.39]# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent

[[email protected] ~/memcached-1.4.39]# make && make install

啟動

[[email protected] ~/memcached-1.4.39]# vim /etc/profile.d/memcached.sh

[[email protected] ~/memcached-1.4.39]# . /etc/profile.d/memcached.sh 

[[email protected] ~/memcached-1.4.39]# cat /etc/profile.d/memcached.sh 

export PATH=/usr/local/memcached/bin/:$PATH

[[email protected] ~/memcached-1.4.39]# memcached -f 2 -d -u daemon

3、配置詳解

指令配置:

.memcached -h

-p:指定tcp監聽端口

-U:指定udp監聽端口(啟動時監聽的端口位址,為了安全建議修改監聽端口)

-l:指定監聽的ip位址,當伺服器有多個網卡的時候設定啟動時監聽的網卡位址(本機)

-d:以守護程序的方式運作,就是在背景運作。

-m:設定緩存大小,預設是64M,建議設定也不要太大,(2g足夠)不友善記憶體管理。

-f:設定增長因子,就是緩存(記憶體劃分大小)遞增倍率。

-v:顯示細節、可以看出記憶體配置設定增長的過程

4、編寫啟動腳本

vim /etc/rc.d/init.d/memcached

問題:

我們一般在腳本開頭加入下面兩句就好了

#gedit /etc/init.d/myservice

添加下面兩句到 #!/bin/bash 之後。

其中2345是預設啟動級别,級别有0-6共7個級别。

  等級0表示:表示關機   

  等級1表示:單使用者模式   

  等級2表示:無網絡連接配接的多使用者指令行模式   

  等級3表示:有網絡連接配接的多使用者指令行模式   

  等級4表示:不可用   

  等級5表示:帶圖形界面的多使用者模式   

  等級6表示:重新啟動

85是啟動優先級,90是停止優先級,優先級範圍是0-100,數字越大,優先級越低

5、memcached常用指令

通過telnet連接配接memcached伺服器,并對其進行操作。

[[email protected] ~/memcached-1.4.39]# telnet 10.40.0.226 11211

Trying 10.40.0.226...

Connected to 10.40.0.226.

Escape character is '^]'.

指令:

存儲類指令:set、add、replace、append、prepend

擷取資料類指令:get、delete、incr/decr

統計類指令:stats items、stats slabs、stats sizes

清理指令:flush_all

二、搭建PHP連接配接到緩存伺服器的方法

1、概述

這裡說明一下,PHP連接配接memcached伺服器的插件有兩種,一個叫memcach另一個叫memcached。他和緩存伺服器的memcached同名不同義。

并且使用perl開發的應用程式伺服器要和memcached進行通信使用的插件也叫memcached,而使用c或者c++開發的應用程式伺服器連接配接memcached伺服器使用的插件叫做libmemcached。

memcached作為伺服器,用戶端需要和他連結是需要“驅動”的。

memcached:伺服器端,即安裝的memcached服務,類似于mysql服務

memcache:使用pecl擴充庫形式操作memcached(類似于php_mysqli)

memcached:使用libmemcached形式操作memcached

2、php安裝驅動(插件)

這裡安裝的是memcache擴充庫。

安裝環境:php-5.5.32、memcache-2.2.7

B、建立php的外挂子產品(需要進入要安裝的子產品目錄内)

[root@localhost mysqli]# /usr/local/php/bin/phpize 

PHP Api Version:         20121113

Zend Module Api No:      20121212

Zend Extension Api No:   220121212

[root@localhost memcache-2.2.7]# ./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcache

[root@localhost  memcache-2.2.7]# make 

[root@localhost memcache-2.2.7]#  make install

安裝完畢會顯示安裝的路徑                  

Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-zts-20121212/

建立并配置ini檔案,添加内容如下:(這裡主要檢查php配置檔案存放位置,我這裡使用的是編譯安裝,配置檔案指定的是/etc/php-fpm/目錄)

[root@localhost memcache-3.0.8]# vim /etc/php-fpm/php.ini

extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/memcache.so

~                                                                                  

重新開機php

service php-fpm restart(需要提前準備)

網頁測試代碼(index.php):

[[email protected]/usr/local/libmemcached/bin]$vim /www/magedu.com/index.php 

<?php

$memcache = new Memcache;

$memcache->connect('10.40.0.226', 11211) or die ("Could not connect");

$version = $memcache->getVersion();

echo "Server's version: ".$version."\n";

$tmp_object = new stdClass;

$tmp_object->str_attr = 'test';

$tmp_object->int_attr = 123;

$memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at the server");

echo "Store data in the cache (data will expire in 10 seconds)\n";

$get_result = $memcache->get('key');

echo "Data from the cache:\n";

var_dump($get_result);

?>

memcache實作跨伺服器登入

3、memadmin-master監控工具

錯誤彙總:

1、執行phpize過程中出現一下代碼:

[root@localhost php-5.5.32]# /usr/local/php/bin/phpize 

Cannot find config.m4. 

Make sure that you run '/usr/local/php/bin/phpize' in the top level source directory of the module

[root@localhost php-5.5.32]# cd ext/mysqli/

本文轉自 blackstome 51CTO部落格,原文連結:http://blog.51cto.com/137783/1968774,如需轉載請自行聯系原作者