HHVM配置及简单性能测试

HHVM配置及简单性能测试

机器配置如下:

OS: Ubuntu 16.04 xenial
Kernel: x86_64 Linux 4.13.0-36-generic
CPU: Intel Xeon CPU E3-1230 v3 @ 3.3GHz
RAM: 720MiB / 3001MiB

PHP Version:

PHP 7.0.22-0ubuntu0.16.04.1 (cli) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
with Zend OPcache v7.0.22-0ubuntu0.16.04.1, Copyright (c) 1999-2017, by Zend Technologies

hvm –version

HipHop VM 3.11.1 (rel)
Compiler: 3.11.1+dfsg-1ubuntu1
Repo schema: 2f678922fc70b326c82e56bedc2fc106c2faca61

1、安装LNMP/LAMP服务器

性能测试对比使用,根据具体需要安装

2、安装HHVM

文档地址

Installation:

For Ubuntu 14.04 there is an officially supported HHVM repository. To add this repository you have to import its GnuPG public keys with the command:
sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0x5a16e7281be7a449

After that you can safely install HHVM’s repository with the command:
sudo add-apt-repository "deb http://dl.hhvm.com/ubuntu $(lsb_release -sc) main"

Once you have the repository added you have to make apt, Ubuntu’s software manager, aware that there are new packages which can be installed with it. This can be done by updating apt’s cache with the command:
sudo apt-get update

Finally, you can install HHVM with the command:
sudo apt-get install hhvm

The above command installs HHVM and starts it for the first time. To make sure HHVM starts and stops automatically with the Droplet, add HHVM to the default runlevels with the command:
sudo update-rc.d hhvm defaults

3、配置HHVM

1
2
3
4
5
6
7
8
9
10
11
12
13
14
; php options
; hhvm specific
;php-fpm使用的端口号是9000
;端口号可sock 选用其中1个
hhvm.server.port = 9003
;hhvm.server.file_socket=/var/run/hhvm/hhvm.sock
hhvm.server.type = fastcgi
hhvm.server.default_document = index.php
hhvm.server.source_root=/home/wwwroot/default
hhvm.log.use_log_file = false
hhvm.log.use_syslog = true
hhvm.repo.central.path = /var/cache/hhvm/hhvm.hhbc

配置完成后启动
sudo service hhvm start
可以使用 ps -ef | grep hhvm 查看是否启动成功,netstat -na | grep 9003 查看端口是否正常监听

4、配置nginx php配置

我这里使用的是lnmp,php配置文件是/usr/local/nginx/conf/enable-php.conf,其他同理

1
2
3
4
5
6
7
8
9
10
11
#location ~ \.php$
location ~ \.(hh|php)$ #添加.hh文件解析
{
try_files $uri =404;
fastcgi_keep_conn on;
#fastcgi_pass unix:/tmp/php-cgi.sock; #php-fpm sock地址
#fastcgi_pass 127.0.0.1:9000; #php-fpm端口号
fastcgi_pass 127.0.0.1:9003; #hhvm端口号
fastcgi_index index.php;
include fastcgi.conf;
}

ubuntu: sudo service nginx restart
centos: /usr/sbin/nginx -s reload
lnmp nginx restart #如果使用的是lnmp重启nginx

5、查看phpinfo是否配置成功

在网站目录下新建info.php文件

1
2
3
<?php
phpinfo();
?>

sudo chown www-data: /usr/share/nginx/html/info.php

访问phpinfo http://your_server_ip/info.php 显示如下信息则配置成功
hhvm info

6、附上简单的性能测试对比

###HHVM 下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
default webbench-c 100 -t 60 http://wordpress.demo/
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.
Request:
GET / HTTP/1.0
User-Agent: WebBench 1.5
Host: wordpress.demo
Runing info: 100 clients, running 60 sec.
Speed=3136 pages/min, 896948 bytes/sec.
Requests: 3136 susceed, 0 failed.
default webbench -c 1000 -t 60 http://wordpress.demo/
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.
Request:
GET / HTTP/1.0
User-Agent: WebBench 1.5
Host: wordpress.demo
Runing info: 1000 clients, running 60 sec.
Speed=183728 pages/min, 967769 bytes/sec.
Requests: 183728 susceed, 0 failed.
default webbench -c 10000 -t 60 http://wordpress.demo/
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.
Request:
GET / HTTP/1.0
User-Agent: WebBench 1.5
Host: wordpress.demo
Runing info: 10000 clients, running 60 sec.
Speed=428134 pages/min, 2219073 bytes/sec.
Requests: 428116 susceed, 18 failed.

###php-fpm模式下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
default webbench -c 100 -t 60 http://wordpress.demo/
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.
Request:
GET / HTTP/1.0
User-Agent: WebBench 1.5
Host: wordpress.demo
Runing info: 100 clients, running 60 sec.
Speed=625 pages/min, 178648 bytes/sec.
Requests: 625 susceed, 0 failed.
default webbench -c 1000 -t 60 http://wordpress.demo/
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.
Request:
GET / HTTP/1.0
User-Agent: WebBench 1.5
Host: wordpress.demo
Runing info: 1000 clients, running 60 sec.
Speed=778 pages/min, 171166 bytes/sec.
Requests: 778 susceed, 0 failed.
default webbench -c 10000 -t 60 http://wordpress.demo/
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.
Request:
GET / HTTP/1.0
User-Agent: WebBench 1.5
Host: wordpress.demo
Runing info: 10000 clients, running 60 sec.
Speed=288 pages/min, 46903 bytes/sec.
Requests: 288 susceed, 0 failed.

默认配置下性能差别5-6倍

网易云音乐爬虫 PHP版本

网易云音乐爬虫 - 爬取评论 - PHP

1、拼接url

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
* 生成接口url
*/
function crypt_api($song_id, $offset){
$url = "http://music.163.com/weapi/v1/resource/comments/R_SO_4_{$song_id}/?csrf_token=";
$first_param = "{rid:\"\", offset:\"{$offset}\", total:\"true\", limit:\"20\", csrf_token:\"\"}";
$forth_param = "0CoJUm6Qyw8W8jud";
$params = get_params($first_param, $forth_param);
$encSecKey = get_encSecKey();
$data = array(
"params"=> $params,
"encSecKey"=> $encSecKey
);
return array($url, $data);
}

2、生成接口的params参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
* 生成接口参数
*/
function get_params($first_param, $forth_param){
$iv = "0102030405060708";
$first_key = $forth_param;
$second_key = 'FFFFFFFFFFFFFFFF';
$encryptObj = new MagicCrypt();
$h_encText = $encryptObj->encrypt($first_param,$first_key,$iv);
$h_encText = $encryptObj->encrypt($h_encText,$second_key,$iv);
return $h_encText;
}
function get_encSecKey(){
$encSecKey = "257348aecb5e556c066de214e531faadd1c55d814f9be95fd06d6bff9f4c7a41f831f6394d5a3fd2e3881736d94a02ca919d952872e7d0a50ebfa1769a7a62d512f5f1ca21aec60bc3819a9c3ffca5eca9a0dba6d6f7249b06f5965ecfff3695b54e1c28f3f624750ed39e7de08fc8493242e26dbc4484a01c76f739e135637c";
return $encSecKey;
}

3、AES 加密解密

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class MagicCrypt {
/**
* @param $encryptStr 需要加密的字符串
* @param $encryptKey 加密的key
* @param $iv 向量,增加加密的安全性
*/
public function encrypt($encryptStr,$encryptKey,$iv) {
$module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, $iv);
mcrypt_generic_init($module, $encryptKey, $iv);
//Padding
$block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
$pad = $block - (strlen($encryptStr) % $block); //Compute how many characters need to pad
$encryptStr .= str_repeat(chr($pad), $pad); // After pad, the str length must be equal to block or its integer multiples
//encrypt
$encrypted = mcrypt_generic($module, $encryptStr);
mcrypt_generic_deinit($module);
mcrypt_module_close($module);
return base64_encode($encrypted);
}
/**
* @param $encryptStr 需要解密的字符串
* @param $encryptKey 加密时使用的key
* @param $iv 加密时使用的向量
*/
public function decrypt($encryptStr,$encryptKey,$iv) {
$module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, $iv);
mcrypt_generic_init($module, $encryptKey, $iv);
$encryptedData = base64_decode($encryptStr);
$encryptedData = mdecrypt_generic($module, $encryptedData);
return $encryptedData;
}
}

4、curlPost函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function _curlPost($url, $curlPost, $cookie_str = '', $header_ary = array()){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
if (substr($url,0,5)=='https') {
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}
if ($cookie_str) {
curl_setopt($curl, CURLOPT_COOKIE, $cookie_str);
}
if ($header_ary) {
curl_setopt($curl, CURLOPT_HTTPHEADER, $header_ary);
}
$return_str = curl_exec($curl);
curl_close($curl);
return $return_str;
}

5、请求数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//http://music.163.com/#/song?id=32507038
get_comment(32507038);
/**
* 获取评论信息
* @param $song_id 歌曲id
*/
function get_comment($song_id){
$offset = 0;
$has_more = true;
while ($has_more) {
$has_more = false;
list($url, $data) = crypt_api($song_id, $offset);
$headers = array(
'Referer'=>'http://music.163.com/',
'Cookie'=> 'appver=1.5.0.75771;MUSIC_U=e954e2600e0c1ecfadbd06b365a3950f2fbcf4e9ffcf7e2733a8dda4202263671b4513c5c9ddb66f1b44c7a29488a6fff4ade6dff45127b3e9fc49f25c8de500d8f960110ee0022abf122d59fa1ed6a2;'
);
$result = _curlPost($url,http_build_query($data),'',$headers);
if ($result) {
$data = json_decode($result);
if ($data) {
if ($data->code == 200) {
if ($data->comments) {
$comments = json_decode(json_encode($data->comments),true);
//保存评论信息
save_comments($song_id,$comments);
}
$has_more = $data->more == 'true';
//跟get_params里面的limit参数一致
$offset += 20;
}else{
handleError("data['code']:$data[code]");
}
}else{
handleError("json_decode error");
}
}else{
handleError("_curlPost result:null");
}
sleep(1);
}
}

参考:

  1. php实现AES/CBC/PKCS5Padding加密解密(又叫:对称加密)
  2. https://github.com/wenhaoliang/netease-music-spider
  3. http://blog.csdn.net/Ciiiiiing/article/details/62434438

Gitlab 源码安装版 升级笔记

Gitlab 升级笔记

适用于使用源码安装的Gitlab

GitLab Community Edition 8.3.2 fbb8b6e>

0. Backup

1
2
cd /home/git/gitlab
sudo -u git -H bundle exec rake gitlab:backup:create RAILS_ENV=production

备份完成后会在/home/git/gitlab/backup文件夹下生成已时间戳命名的文件1459217332_gitlab_backup.tar

1. Stop server

1
sudo service gitlab stop