Nao000のぶろぐ

蝶を追っている少年になりたい

ab (Apache HTTP server benchmarking tool)を触ってみる

前提

  • PHP アプリに ab コマンドを実行します
  • ローカル環境で実行しています
  • インターネットは経由していないです
  • OS , apache httpd , php, php-fpm のバージョンは最後に記載します

どういうテストか

ab -n 5 -c 1 -l http://microblog-user.example.com/

「-n 5」で総アクセス数を 5 に設定,「-c 1」で並列数を1に設定、「-l」でレスポンスサイズが異なってもエラーとしてレポートしない、最後に URL 設定しています。最後にテスト対象の URL 記載しています。

この URL は PHP アプリが稼働しています。

PHPアプリの内容

sleep(1) を実行して終わるアプリです。

<?php
sleep(1);
die;

sleep(1) を入れることで、5秒かかったことを確認する狙いです。

入れないとすぐに終了してしまうので入れています。

実行結果

root@b1bb52df23b4:/# ab -n 5 -c 1 -l http://microblog-user.example.com/
This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking microblog-user.example.com (be patient).....done


Server Software:        Apache/2.4.52
Server Hostname:        microblog-user.example.com
Server Port:            80

Document Path:          /
Document Length:        Variable

Concurrency Level:      1
Time taken for tests:   5.071 seconds
Complete requests:      5
Failed requests:        0
Total transferred:      960 bytes
HTML transferred:       0 bytes
Requests per second:    0.99 [#/sec] (mean)
Time per request:       1014.184 [ms] (mean)
Time per request:       1014.184 [ms] (mean, across all concurrent requests)
Transfer rate:          0.18 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       0
Processing:  1012 1014   2.1   1014    1017
Waiting:     1012 1014   2.1   1014    1017
Total:       1012 1014   2.2   1014    1017

Percentage of the requests served within a certain time (ms)
  50%   1013
  66%   1016
  75%   1016
  80%   1017
  90%   1017
  95%   1017
  98%   1017
  99%   1017
 100%   1017 (longest request)
root@b1bb52df23b4:/#

注目する部分

狙い通り 5 秒ほどかかっています。

Time taken for tests:   5.071 seconds

試しに以下で 10 アクセスをテストしました。

ab -n 10 -c 1 -l http://microblog-user.example.com/

結果 10 秒程かかっていました。

Time taken for tests:   10.161 seconds

10アクセスを並列数を2にしてテストしてみる

おそらく約 5 秒になります。

php-fpm , apache httpd それぞれのプロセスは起動時に 2 プロセスは生成されるようになっていますので並列数を 2 にして 5 秒になる計算です。

結果は約 6.2 秒でした。個人的に 1.2 秒は許容しますので予想通りということで。

Time taken for tests:   6.241 seconds

↓↓ こんな感じでリクエストの終了と開始の間のオーバーヘッドによる1.2秒だと解釈しています。

{リクエスト1 を処理開始 => リクエスト1 を処理終了 =>オーバーヘッド!!=> リクエスト2 を処理開始 => ...}

おわり

簡単なテストをしました。

引き続き ab コマンドの腕を磨きたいです。

以下、バージョンなど

OS

root@b1bb52df23b4:/# cat /etc/os-release
PRETTY_NAME="Ubuntu 22.04.3 LTS"
NAME="Ubuntu"
VERSION_ID="22.04"
VERSION="22.04.3 LTS (Jammy Jellyfish)"
VERSION_CODENAME=jammy
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=jammy
root@b1bb52df23b4:/#

apache httpd

Syntax error ありますが気にしないです。 Server MPM:がなぜか空( empty )ですが event です。

更新 2024-12-22 11:22 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

/usr/sbin/apache2 でダイレクトに実行した場合は初期設定が反映されないらしいです。

/etc/apache2/apache2.conf に記載されていました。

* The binary is called apache2. Due to the use of environment variables, in

the default configuration, apache2 needs to be started/stopped with

/etc/init.d/apache2 or apache2ctl. Calling /usr/bin/apache2 directly will not

work with the default configuration.

初期設定を読み込まれた状態で確認するには apache2ctl -V を実行すればいいみたいです。Syntax error もありません。

Server MPM: event となっていることが確認できます。

root@57d6e40d3fa9:/# apache2ctl -V
Server version: Apache/2.4.52 (Ubuntu)
Server built:   2024-07-17T18:57:26
Server's Module Magic Number: 20120211:126
Server loaded:  APR 1.7.0, APR-UTIL 1.6.1
Compiled using: APR 1.7.0, APR-UTIL 1.6.1
Architecture:   64-bit
Server MPM:     event
  threaded:     yes (fixed thread count)
    forked:     yes (variable process count)
Server compiled with....
 -D APR_HAS_SENDFILE
 -D APR_HAS_MMAP
 -D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
 -D APR_USE_PROC_PTHREAD_SERIALIZE
 -D APR_USE_PTHREAD_SERIALIZE
 -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
 -D APR_HAS_OTHER_CHILD
 -D AP_HAVE_RELIABLE_PIPED_LOGS
 -D DYNAMIC_MODULE_LIMIT=256
 -D HTTPD_ROOT="/etc/apache2"
 -D SUEXEC_BIN="/usr/lib/apache2/suexec"
 -D DEFAULT_PIDLOG="/var/run/apache2.pid"
 -D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
 -D DEFAULT_ERRORLOG="logs/error_log"
 -D AP_TYPES_CONFIG_FILE="mime.types"
 -D SERVER_CONFIG_FILE="apache2.conf"
root@57d6e40d3fa9:/#

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

php

root@b1bb52df23b4:/# php -v
PHP 8.1.2-1ubuntu2.14 (cli) (built: Aug 18 2023 11:41:11) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.2-1ubuntu2.14, Copyright (c), by Zend Technologies
root@b1bb52df23b4:/#

php-fpm

root@b1bb52df23b4:/# php-fpm8.1 -v
PHP 8.1.2-1ubuntu2.14 (fpm-fcgi) (built: Aug 18 2023 11:41:11)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.2-1ubuntu2.14, Copyright (c), by Zend Technologies
root@b1bb52df23b4:/#

apache httpd, php-fpm の主な設定

apache httpd

root@b1bb52df23b4:/# cat /etc/apache2/mods-enabled/mpm_event.conf
# event MPM
# StartServers: initial number of server processes to start
# MinSpareThreads: minimum number of worker threads which are kept spare
# MaxSpareThreads: maximum number of worker threads which are kept spare
# ThreadsPerChild: constant number of worker threads in each server process
# MaxRequestWorkers: maximum number of worker threads
# MaxConnectionsPerChild: maximum number of requests a server process serves
<IfModule mpm_event_module>
        StartServers                     2
        MinSpareThreads          25
        MaxSpareThreads          75
        ThreadLimit                      64
        ThreadsPerChild          25
        MaxRequestWorkers         150
        MaxConnectionsPerChild   0
</IfModule>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
root@b1bb52df23b4:/#

php-fpm

root@b1bb52df23b4:/# cat /etc/php/8.1/fpm/pool.d/www.conf | grep pm. | grep -v ";"
listen = /run/php/php8.1-fpm.sock
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
root@b1bb52df23b4:/#