Closed7

[Ruby] isucon11-qualifyを解説を見ながらなぞってみる

d yoshikawad yoshikawa

topコマンドで計測

ターミナルを2つ開き、両方でEC2インスタンスにsshする。1つ目のターミナルでbenchコマンドを実行している間、2つ目のターミナルでtopコマンドを叩きモニタリングしてみる。

mysqlのプロセスが重いのかな〜など雑に思う。

d yoshikawad yoshikawa

MySQL(MariaDB)スロークエリログを出力

mysql -uisucon -pisucon
MariaDB> SET GLOBAL slow_query_log = ON;
MariaDB> SET GLOBAL long_query_time = 0.01;
MariaDB> SET GLOBAL min_examined_row_limit = 0;
MariaDB> SET GLOBAL log_slow_admin_statements = OFF;
MariaDB> SET GLOBAL log_queries_not_using_indexes = ON;
MariaDB> SET GLOBAL slow_query_log_file = 'my-slow.log';

スロークエリログファイルの場所を探す。

sudo find / -name "my-slow.log"
# => /var/lib/mysql/my-slow.log

/var/lib/mysql/my-slow.log にあるようなので、benchコマンドを流してからmysqldumpslowコマンドで整形して確認する。

sudo mysqldumpslow /var/lib/mysql/my-slow.log

参考:
【MySQL】スロークエリーログの出力から確認まで(実例あり) - Qiita

d yoshikawad yoshikawa

alpインストール手順

asdfインストール

今回はasdf経由で入れてみる。

git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.10.1
echo ". $HOME/.asdf/asdf.sh" >> ~/.bashrc
exec $SHELL -l
asdf version

でバージョンが表示されることを確認。

alpインストール

asdf plugin-add alp https://github.com/asdf-community/asdf-alp.git
asdf install alp 1.0.9
asdf global alp 1.0.9
alp --version

でバージョン表示を確認。

d yoshikawad yoshikawa

alpでnginxログを解析

まずはnginx設定を変更する。

念の為 nginx.conf を複製して設定を戻せるようにしておく。

sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bk

nginx.confvi で開く。

sudo vi /etc/nginx/nginx.conf

下記のように変更。LTSV形式でログ出力するようにする。

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

-   log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
-                     '$status $body_bytes_sent "$http_referer" '
-                     '"$http_user_agent" "$http_x_forwarded_for"';

-   access_log  /var/log/nginx/access.log  main;

+  log_format ltsv "time:$time_local"
+     "\thost:$remote_addr"
+     "\tforwardedfor:$http_x_forwarded_for"
+     "\treq:$request"
+     "\tmethod:$request_method"
+     "\turi:$request_uri"
+     "\tstatus:$status"
+     "\tsize:$body_bytes_sent"
+     "\treferer:$http_referer"
+     "\tua:$http_user_agent"
+     "\treqtime:$request_time"
+     "\truntime:$upstream_http_x_runtime"
+     "\tapptime:$upstream_response_time"
+     "\tcache:$upstream_http_x_cache"
+     "\tvhost:$host";

+   access_log  /var/log/nginx/access.log  ltsv;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*.conf;
}

nginxを再起動する。

sudo systemctl restart nginx

benchスクリプトを走らせる。これでアクセスログが出力されるので、 alp コマンドで解析する。

sudo cat /var/log/nginx/access.log | alp ltsv

参考:

このスクラップは2022/07/10にクローズされました