Open5

private-isu

nyancatnyancat

EC2

SSH

chmod 400 .ssh/private-isu-key-pair.pem

.ssh/config

Host private-isu
  HostName [EC2 Web Service Instance Public IPv4 address]
  User ubuntu
  IdentityFile ~/.ssh/private-isu-key-pair.pem
  RemoteCommand sudo su - isucon
  RequestTTY true

Host private-isu-bench
  HostName [EC2 Benchmark Instance Public IPv4 address]
  User ubuntu
  IdentityFile ~/.ssh/private-isu-key-pair.pem
  RemoteCommand sudo su - isucon
  RequestTTY true
ssh private-isu
ssh private-isu-bench

or ssh directly

ssh -i .ssh/private-isu-key-pair.pem ubuntu@[EC2 Web Service Instance Public IPv4 address]
ssh -i .ssh/private-isu-key-pair.pem ubuntu@[EC2 Benchmark Instance Public IPv4 address]

nyancatnyancat

Calculate the score

sudo su - isucon
/home/isucon/private_isu.git/benchmarker/bin/benchmarker -u /home/isucon/private_isu.git/benchmarker/userdata -t http://[WEB Service Instance Public IPv4 address]

output

{"pass":true,"score":323,"success":410,"fail":8,"messages":["リクエストがタイムアウトしました (GET /logout)","リクエストがタイムアウトしました (POST /login)","リクエストがタイムアウトしました (POST /register)"]}

while running the command, run top command on the WEB service instance to monitor processes

top
nyancatnyancat

Output slow query log

edit /etc/mysql/mysql.conf.d/mysqld.cnf to enable logging of slow queries

[mysqld]
slow_query_log=1
slow_query_log_file='/var/log/mysql/mysql-slow.log'
long_query_time=0

restart MySQL

$ sudo su - root
$ systemctl restart mysql

delete the previous log

実行する前に、MySQLのスロークエリログが前回実行時のものと混ざらないように削除しておきます。ログファイルを削除したり名前を変更した場合は、mysqladmin flush-logsを実行してファイルが更新されていることをMySQLに伝える必要があります。

rm /var/log/mysql/mysql-slow.log
mysqladmin flush-logs

mysqldumpslow

mysqldumpslow /var/log/mysql/mysql-slow.log

sort by total query time

mysqldumpslow -s t /var/log/mysql/mysql-slow.log
nyancatnyancat

Speed up

comments table

mysql> ALTER TABLE `comments` ADD INDEX `post_id_idx` (`post_id`);
Query OK, 0 rows affected (0.56 sec)
Records: 0  Duplicates: 0  Warnings: 0
{"pass":true,"score":8358,"success":7109,"fail":0,"messages":[]}

posts table

Reading mysql slow query log from /var/log/mysql/mysql-slow.log
Count: 1974  Time=0.02s (32s)  Lock=0.00s (0s)  Rows=10039.6 (19818200), isuconp[isuconp]@localhost
  SELECT `id`, `user_id`, `body`, `created_at`, `mime` FROM `posts` ORDER BY `created_at` DESC
mysql> explain SELECT `id`, `user_id`, `body`, `created_at`, `mime` FROM `posts` ORDER BY `created_at` DESC;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------+
| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra          |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------+
|  1 | SIMPLE      | posts | NULL       | ALL  | NULL          | NULL | NULL    | NULL | 9169 |   100.00 | Using filesort |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------+
1 row in set, 1 warning (0.00 sec)

webapp/ruby/app.rb

https://github.com/nyancat3/private-isu/compare/master...nyancat3:private-isu:feature/practice

"score":8042 -> "score":16882