Trong phần trước, xTraffic.pep.vn đã hướng dẫn bạn cách cài đặt Phalcon PHP Framework trên VPS của bạn. Tiếp theo trong bài này, chúng ta sẽ cùng nhau tạo chương trình “Hello World” của Phalcon PHP Framework, đồng thời sẽ tiến hành benchmark performance (kiểm tra tốc độ) “Hello World” của Phalcon PHP Framework bằng các phương pháp kiểm tra khả năng chịu tải của website.
Tạo “Hello World” của Phalcon PHP Framework
Để thực hiện được theo hướng dẫn này, trước hết bạn cần phải cài đặt Phalcon & Phalcon DevTools trên VPS của bạn.
Tiếp theo, chúng ta sẽ tạo project có tên là “helloworld” tại thư mục “/home/websites/“. Các bạn thực hiện các lệnh sau :
mkdir -p /home/websites cd /home/websites phalcon create-project helloworld
Ví dụ :
[[email protected] opt]# cd /home/websites [[email protected] websites]# phalcon create-project helloworld Phalcon DevTools (1.3.4) Success: Controller "index" was successfully created. Success: Project 'helloworld' was successfully created. [[email protected] websites]#
Tiếp theo, chúng ta sẽ cấu hình Nginx để chạy project vừa tạo. Bạn tạo file “/etc/nginx/conf.d/websites.conf” có nội dung như sau :
server { listen 80; server_name test.pep.vn; set $root_path '/home/websites/helloworld/public'; root $root_path; index index.php index.html index.htm; try_files $uri $uri/ @rewrite; location @rewrite { rewrite ^/(.*)$ /index.php?_url=/$1; } location ~ \.php { fastcgi_pass 127.0.0.1:9000; fastcgi_index /index.php; include /etc/nginx/fastcgi_params; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } location ~* ^/(css|img|js|flv|swf|download)/(.+)$ { root $root_path; } location ~ /\.ht { deny all; } }
Có 2 điểm bạn cần lưu ý để cấu hình Nginx với các project khác :
- server_name test.pep.vn; : bạn thay thế “test.pep.vn” trong ví dụ này bằng domain của bạn
- set $root_path ‘/home/websites/helloworld/public’; : đây là đường dẫn chứa project, khi bạn tạo project khác thì lưu ý thay thế đường dẫn project cho đúng.
Truy cập vào http://test.pep.vn sẽ thấy thông báo như sau :
Vậy là chúng ta đã tạo project thành công. Tiếp theo, chúng ta sẽ hiển thị “Hello World” với Phalcon PHP Framework
Bạn tạo controller “hello” bằng lệnh sau :
cd /home/websites/helloworld phalcon create-controller --name hello
Khi thực hiện lệnh “phalcon create-controller –name hello“, chương trình sẽ tạo 1 file “/home/websites/helloworld/app/controllers/HelloController.php” có nội dung như sau :
<?php class HelloController extends \Phalcon\Mvc\Controller { public function indexAction() { } }
Bạn thêm vào nội dung sau vào “/home/websites/helloworld/app/controllers/HelloController.php”
<?php class HelloController extends \Phalcon\Mvc\Controller { public function indexAction() { $this->view->hello = 'Hello World';//Thêm dòng này } }
Sau đó tạo file “/home/websites/helloworld/app/views/hello/index.volt” có nội dung sau :
<h1>{{ hello }}</h1>
Bây giờ bạn mở trình duyệt và truy cập vào url “http://test.pep.vn/hello/” sẽ thấy nội dung như sau :
Chúc mừng bạn đã tạo thành công “Hello World” cho Phalcon PHP Framework.
Benchmark performance “Hello World” Phalcon PHP Framework
Benchmark bằng siege
[[email protected] ~]# siege -c20 -t60s http://test.pep.vn/hello/ Lifting the server siege... done. Transactions: 2346 hits Availability: 100.00 % Elapsed time: 59.99 secs Data transferred: 0.28 MB Response time: 0.01 secs Transaction rate: 39.11 trans/sec Throughput: 0.00 MB/sec Concurrency: 0.55 Successful transactions: 2346 Failed transactions: 0 Longest transaction: 0.46 Shortest transaction: 0.00 [[email protected] ~]#
Benchmark bằng Apache Benchmark (ab)
[[email protected] ~]# ab -c 20 -k -r -t 60 -v 3 http://test.pep.vn/hello/ Completed 50000 requests Finished 50000 requests Server Software: nginx/1.0.15 Server Hostname: test.pep.vn Server Port: 80 Document Path: /hello/ Document Length: 125 bytes Concurrency Level: 20 Time taken for tests: 55.479 seconds Complete requests: 50000 Failed requests: 0 Write errors: 0 Keep-Alive requests: 0 Total transferred: 13600000 bytes HTML transferred: 6250000 bytes Requests per second: 901.24 [#/sec] (mean) Time per request: 22.192 [ms] (mean) Time per request: 1.110 [ms] (mean, across all concurrent requests) Transfer rate: 239.39 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 0.0 0 1 Processing: 10 22 12.2 22 645 Waiting: 10 22 12.2 21 645 Total: 11 22 12.2 22 646 Percentage of the requests served within a certain time (ms) 50% 22 66% 22 75% 22 80% 22 90% 24 95% 25 98% 27 99% 28 100% 646 (longest request) [[email protected] ~]#
Benchmark bằng wrk
[[email protected] ~]# wrk -t10 -c20 -d60s http://test.pep.vn/hello/ Running 1m test @ http://test.pep.vn/hello/ 10 threads and 20 connections Thread Stats Avg Stdev Max +/- Stdev Latency 19.93ms 2.50ms 106.85ms 92.74% Req/Sec 101.44 15.76 170.00 62.68% 60283 requests in 1.00m, 18.16MB read Requests/sec: 1004.49 Transfer/sec: 309.93KB [[email protected] ~]#
Benchmark bằng boom
[[email protected]]# boom -n 2000 -c 20 -cpus 1 -allow-insecure http://test.pep.vn/hello/ 2000 / 2000 Boooooooooooooooooooooooooo! 100.00 % Summary: Total: 2.5999 secs. Slowest: 0.2401 secs. Fastest: 0.0043 secs. Average: 0.0258 secs. Requests/sec: 769.2729 Status code distribution: [200] 2000 responses Response time histogram: 0.004 [1] | 0.028 [1962] |∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎ 0.051 [16] | 0.075 [1] | 0.099 [0] | 0.122 [0] | 0.146 [0] | 0.169 [0] | 0.193 [0] | 0.216 [0] | 0.240 [20] | Latency distribution: 10% in 0.0229 secs. 25% in 0.0232 secs. 50% in 0.0236 secs. 75% in 0.0241 secs. 90% in 0.0249 secs. 95% in 0.0253 secs. 99% in 0.2338 secs. [[email protected] ~]#