8
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【👷‍♂️環境構築編👷‍♂️】ISUCON本に載っているprivate-isu (Iscogram) をWSL上でsystemdを使って作りパフォーマンスチューニングする

Last updated at Posted at 2025-11-04

「ISUCON」とは何か

ISUCONとは、Iikanjini Speed Up Contest(いい感じにスピードアップコンテスト)の略で、お題となるWebサービスを決められたレギュレーションの中で限界まで高速化を図るチューニングバトルです。
高速化手法は 「何でもあり」 です。仕様を維持する限り,コードの書き換え,ミドルウェアの交換,OS設定の調整など,やれることは無限にあります。ただし,フロントエンドのチューニングは不要です。

ISUCONの技術領域

  • (かなりざっくりしているが)Webサービスを高速化する上で以下のような知識が必要になってくる。
    • アプリ
    • ミドルウェア
    • OS
    • DB
    • クラウド
    • ネットワーク

参考:ISUCON 事前講習 2022 座学

↑の知識が必要なので、Webアプリケーションを作るエンジニアにとってISUCONを取り組むことが良い勉強になる。
また、dockerやAMIを用いることで簡単に環境構築ができるが、WSLにsystemdを用いて環境構築した方が↑の勉強になると思ったのであ、え、て、WSLにsystemdを用いて環境構築をしてみる。

ISUCON本とは

達人が教えるWebパフォーマンスチューニング 〜ISUCONから学ぶ高速化の実践のことをISUCON本と言っている。このISUCON本には実際の「private-isu」というWebサービスを題材にして、パフォーマンスの計測方法から負荷試験ツールや実際のアプリケーションの改善方法などISUCONに必要な知識が記載されている。

private-isuの構成

  • Webサーバー:nginx
  • APサーバー:Go
    • 他にもRuby, Python, Node.jsなども利用できる
    • ISUCON14の言語使用率をみるとGoは70%ほどだったのでGoを選択している
  • DBサーバー:MySQL
  • インメモリキャッシュ:Memcached

それでは、「private-isu」をWSL上に作ってみる

【 STEP 1 】 wslをダウンロードしてバージョンを確認

Power Shell上で以下のコマンドを実行する

$ wsl --install
〜略〜
$ wsl --version
WSL バージョン: 2.5.9.0
カーネル バージョン: 6.6.87.2-1
WSLg バージョン: 1.0.66
MSRDC バージョン: 1.2.6074
Direct3D バージョン: 1.611.1-81528511
DXCore バージョン: 10.0.26100.1-240331-1435.ge-release
Windows バージョン: 10.0.26100.4652

【 STEP 2 】 wslを起動

Power Shell上で以下のコマンドを実行する

$ wsl.exe -d Ubuntu

以下のSTEP3からは、WSL上で設定を行う

【 STEP 3 】 mysqlをインストールして起動

$ sudo apt update
$ sudo apt install mysql-server

初期のDBの構成は以下の感じ(MySQLデフォルトの構成)

$ sudo mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.43-0ubuntu0.24.04.2 (Ubuntu)

Copyright (c) 2000, 2025, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql>

【 STEP 4 】 mysqlにアクセスしてデータ投入する

https://github.com/catatsuy/private-isu/blob/master/Makefile
を読むとDBに投入するデータが記載してあるのでダウンロードする

$ curl -L -O https://github.com/catatsuy/private-isu/releases/download/img/dump.sql.bz2ls

その後、解凍してからデータ投入をする
README.mdに記載してあった bunzip2 -c dump.sql.bz2 ではうまく行かなかった

$ sudo apt install bzip2
$ bzip2 -d dump.sql.bz2
$ cat dump.sql | sudo mysql

DBにアクセスしてみると、「isuconp」DBが追加されていることを確認できた

mysql> show stabases;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'stabases' at line 1
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| isuconp            |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.01 sec)
mysql>
mysql> use isuconp
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql>
mysql> show tables;
+-------------------+
| Tables_in_isuconp |
+-------------------+
| comments          |
| posts             |
| users             |
+-------------------+
3 rows in set (0.01 sec)
mysql>

【 STEP 5 】 MySQLのパスワードを設定する

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'p@ssw0rd';

exitしてから、設定したパスワードを用いて接続できるか確認

$ mysql -u root -p
Enter password: # ↑で設定したパスワードを入力
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 8.0.43-0ubuntu0.24.04.2 (Ubuntu)

Copyright (c) 2000, 2025, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

【 STEP 6 】 Memcachedのサービスを起動

$ sudo apt install memcached
$ sudo systemctl enable memcached
$ systemctl status memcached
● memcached.service - memcached daemon
     Loaded: loaded (/usr/lib/systemd/system/memcached.service; enabled; preset: enabled)
     Active: active (running) since Sat 2025-11-01 12:22:46 JST; 17s ago
       Docs: man:memcached(1)
   Main PID: 8195 (memcached)
      Tasks: 10 (limit: 6966)
     Memory: 1.8M (peak: 2.7M)
        CPU: 63ms
     CGroup: /system.slice/memcached.service
             └─8195 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1 -l ::1 -P /var/run/memcached/memcached.pid

Nov 01 12:22:46 WIN-7DT18NF20BJ systemd[1]: Started memcached.service - memcached daemon.

【 STEP 7 】 private_isuのアプリケーションコードを持ってくる

$ git clone https://github.com/catatsuy/private-isu.git

【 STEP 8 】 STEP5で設定したMYSQLのパスワードを用いてアプリケーションを立ち上げてブラウザでアクセスしてみる

goのインストール

$ sudo apt install golang-go 

実行

$ cd private-isu/webapp/golang
$ ISUCONP_DB_PASSWORD="p@ssw0rd" go run app.go

ブラウザでlocalhost:8080でアクセスしてみると、Iscogramが起動できた

image.png

【 STEP 9 】 Benchmarkを動かしてみる

nginxの準備ができてないですが、とりあえずアプリケーションは動くようになったので、ベンチマークを動かしてみる。

データの準備

$ cd private-isu/benchmarker/userdata && curl -L -O https://github.com/catatsuy/private-isu/releases/download/img/img.zip
$ sudo apt install unzip
$ unzip -qq -o img.zip

ベンチマーカーのコンパイルと実行

$ go build .
$ ./bin/benchmarker -t "http://localhost:8080" -u ./userdata

結果は、以下のようになっていて、scoreが4000程度出ていてちゃんと動いている

{"pass":true,"score":4041,"success":3900,"fail":0,"messages":[]}

【 STEP 10 】 nginxをインストールして起動

それでは、nginxの準備をしていく

$ sudo apt install nginx
〜略〜
$ systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: enabled)
     Active: active (running) since Sat 2025-11-01 11:20:17 JST; 1h 47min ago
       Docs: man:nginx(8)
   Main PID: 264 (nginx)
      Tasks: 5 (limit: 6966)
     Memory: 4.6M (peak: 5.3M)
        CPU: 68ms
     CGroup: /system.slice/nginx.service
             ├─264 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
             ├─265 "nginx: worker process"
             ├─266 "nginx: worker process"
             ├─267 "nginx: worker process"
             └─268 "nginx: worker process"

Nov 01 11:20:17 WIN-7DT18NF20BJ systemd[1]: Starting nginx.service - A high performance web server and a reverse proxy server...
Nov 01 11:20:17 WIN-7DT18NF20BJ systemd[1]: Started nginx.service - A high performance web server and a reverse proxy server.

ブラウザで、localhost:80にアクセスしてみると、nginxに接続できた。
image.png

【 STEP 11 】 nginxのリクエストをアプリケーションに転送する

/etc/nginx/sites-enabled/defaultを以下のように変更します。

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        # root /var/www/html;
        root path/to/private-isu/webapp/public;
        }

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name _;

        location ~ ^/(css|js|images|fonts|jpg|png|gif)/ {
           try_files $uri =404;
        }

        location / {
                proxy_pass http://127.0.0.1:8080;
        }
}

locahost:80でブラウザにアクセスしたら、localhost:8080にアクセスしたときと同じように接続できることが確認できた。

image.png

一応、ベンチマークが実行してみて、スコアが同程度に出ることも確認できた

./bin/benchmarker -t "http://localhost:80" -u ./userdata
{"pass":true,"score":4147,"success":4006,"fail":0,"messages":[]}

最後に

パフォーマンスチューニング編は後ほど公開予定です。
アカウントをフォローしてお楽しみにしてお待ちください ♪

8
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?