性能
• 以下测试数值为虚拟机
• 常规HTTP的Curl:这个受端口最大值及打开文件句柄数限制
• 当使用HTTP Curl时
• 同步:2000/sec
• Gearman:未优化情况下
• 用PHP+gearman揑件测试并发批量调用:
• 同步:4000/sec
• 异步:10000/sec
• 以上还有上升空间,官方测试数据为5w/sec
• 增加持久化揑件设置后性能会下降一些
yum install vim wget gcc gcc-c++ make dos2unix gperf libevent libevent-devel zlib-devel bzip2-devel openssl-devel ncurses-devel boost boost-devel mysql-devel
# wget https://launchpad.net/gearmand/1.2/1.1.9/+download/gearmand-1.1.9.tar.gz # tar -zxvf gearmand-1.1.9.tar.gz # cd gearmand-1.1.9 # ./configure 如果出现错误请查看下面的错误解决
* LIBS: * LDFLAGS Flags: * Assertions enabled: no * Debug enabled: no * Warnings as failure: no * Building with libsqlite3 no * Building with libdrizzle no * Building with libmemcached not found * Building with libpq no * Building with tokyocabinet no * Building with libmysql yes * SSL enabled: no * make -j: 3 * VCS checkout: no
# make # make install
# wget http://pecl.php.net/get/gearman # mv gearman gearman.tar.gz # tar -zxvf gearman.tar.gz # cd gearman-1.1.2/ # phpize # ./configure # make # make install # cd /etc/php.d/ # cp gd.ini gearman.ini # vim gearman.ini
; Enable gearman extension module extension=gearman.so
# service php-fpm restart
checking for Boost headers version >= 1.39.0… no configure: error: cannot find Boost headers version >= 1.39.0
# yum search boost # yum install boost.x86_64 # yum install boost-devel.x86_64
checking for gperf... no configure: error: could not find gperf
#yum search gperf #yum install gperf.x86_64
checking test for a working libevent... no configure: error: Unable to find libevent
# yum install libevent libevent-devel
Client mode: gearman [options] [<data>] Worker mode: gearman -w [options] [<command> [<args> ...]] Common options to both client and worker modes. -f <function> - Function name to use for jobs (can give many) -h <host> - Job server host -H - Print this help menu -v - Print diagnostic information to stdout(false) -p <port> - Job server port -t <timeout> - Timeout in milliseconds -i <pidfile> - Create a pidfile for the process Client options: -b - Run jobs in the background(false) -I - Run jobs as high priority -L - Run jobs as low priority -n - Run one job per line(false) -N - Same as -n, but strip off the newline(false) -P - Prefix all output lines with functions names -s - Send job without reading from standard input -u <unique> - Unique key to use for job Worker options: -c <count> - Number of jobs for worker to run before exiting -n - Send data packet for each line(false) -N - Same as -n, but strip off the newline(false) -w - Run in worker mode(false)
# gearman -w -f abc -- wc -m
# gearman -f abc 'aaaa'
# gearman -f abc < /etc/php.ini
# vi /var/www/html/company/gearman/worker.php
<?php $worker= new GearmanWorker(); $worker->addServer('127.0.0.1', 4730); //连接job服务器 $worker->addFunction('reverse', 'my_reverse_function'); //注册支持任务及对应函数 while ($worker->work()); //循环等待任务,没有时阻塞,循环体内可以放错误处理 //work内尽量不要出现资源忘记回收情况 //处理任务的回调函数 function my_reverse_function($job) { $workload = $job->workload(); //过来的参数 $result = strrev($workload); //运算 $content = file_get_contents("http://www.google.com"); //加大运算时间 //加入执行日志 $file = fopen("worker_counter.log","a+"); fwrite($file,date("Y-m-d H:i:s")."\n"); //fwrite($file,var_export($job,TRUE)."\n"); fwrite($file,$workload."\n"); fwrite($file,$result."\n"); fclose($file); return $result; //返回给调用方的数据 } ?>
# vi /var/www/html/company/gearman/client.php
<?php try{ $client= new GearmanClient(); $client->addServer('127.0.0.1', 4730); echo $client->do('reverse', 'You can do it.'), "\n"; }catch(Exception $e){ print_r($e); } ?>
# php worker.php
# php client.php .ti od nac uoY
# vi /var/www/html/company/gearman/worker.php
只要能保证nginx能访问到就可以
<?php $worker= new GearmanWorker(); $worker->addServer('127.0.0.1', 4730); $worker->addFunction('reverse', 'my_reverse_function'); while ($worker->work()); function my_reverse_function($job) { $workload = $job->workload(); $result = strrev($workload); $content = file_get_contents("http://www.google.com"); //加大运算时间 //加入执行日志 $file = fopen("worker_counter.log","a+"); fwrite($file,date("Y-m-d H:i:s")."\n"); //fwrite($file,var_export($job,TRUE)."\n"); fwrite($file,$workload."\n"); fwrite($file,$result."\n"); fclose($file); return $result; } ?>
<?php try{ $client= new GearmanClient(); $client->addServer('127.0.0.1', 4730); echo $client->doBackground('reverse', 'You can do it.'), "\n"; //异步只是派发任务,不等待返回结果 }catch(Exception $e){ print_r($e); } ?>
# php worker.php
# tail -f /var/www/html/company/gearman/worker_counter.log
# php client.php H:localhost:150
2014-03-15 06:08:16 You can do it. .ti od nac uoY
2014-03-15 06:09:44 You can do it. .ti od nac uoY