ISUCON9予選問題をVagrantfileからVirtualBox上で動かす

概要

ISUCON9 予選問題の環境を、手元のMacOSで再現します。
ローカル環境でISUCON9予選の問題を動かす : ISUCON公式Blogにあるように、アプリケーション自体はホストOSで動かすことができますが、今回はより本番に近い環境を再現するため、matsuuさんが提供してくれているVagrantfileを使用し、VirtualBoxで構築した仮想OS上に構築します。

環境

準備

VirtualBoxVagrantをインストール

VirtualBoxとは

Oracleが提供する、クロスプラットフォームの仮想化アプリケーション。
WindowsMacOS,Linuxなどにインストールすると、VirtualBoxを通して別OSを動かすことができる。

Oracle VM VirtualBox is a cross-platform virtualization application.
ref. Oracle® VM VirtualBox®

Vagrantとは

HashiCorpが提供する、仮想マシンの環境構築ツール。
VirtualBoxVMwareなど、Vagrantプラグインをサポートしている仮想化ソフトウェア上で動作する。

Vagrant is a tool for building and managing virtual machine environments in a single workflow.
ref. Introduction - Vagrant by HashiCorp

install

Homebrewからインストール。

$ brew cask install virtualbox
$ VBoxManage -v
6.1.4r136177

$ brew cask install vagrant
$ vagrant -v
Vagrant 2.2.7

公式ページから直接ダウンロードしてもOK。

ISCON9 予選問題環境構築のためのVagrantfileを取得する

Vagrantfileとは

仮想マシンの環境構築に必要なマシンタイプと、そのマシンの設定及びプロビジョニング (準備)方法を記載したファイル。Vagrantがサポートする全てのプラットフォーム間で移植可能。
通常、プロジェクトに必要な各種ソースコードと一緒にVCS(Gitなど)にコミットされる。

The primary function of the Vagrantfile is to describe the type of machine required for a project, and how to configure and provision these machines.
ref. Vagrantfile - Vagrant by HashiCorp

取得

githubから取得する。

$ ghq get git@github.com:matsuu/vagrant-isucon.git
# 普通に
# git clone git@github.com:matsuu/vagrant-isucon.git
# などでもOK

起動

環境構築

Vagrantfileの提供元READMEに書いてある通りに起動してみる。

$ cd ./isucon9-qualifier-standalone
$ vagrant up #up: starts and provisions the vagrant environment

初回はbento/ubuntu-18.04のダウンロードを挟むので時間がかかる。

ダウンロード終了後にエラーで起動が失敗してしまった時の奮闘記はこちら。
moeka.hatenablog.jp

ベンチマーク起動

$ vagrant ssh
vagrant@vagrant$ sudo -i -u isucon
isucon@vagrant$ cd isucari
isucon@vagrant$ bin/benchmarker

この状態でのスコアは以下。

{"pass":true,"score":2310,"campaign":0,"language":"Go","messages":["GET /items/47204.json: リクエストに失敗しました(タイムアウトしました)"]}

思ってたよりちゃんと出た。

ホストからアプリケーションにアクセス

ホストオンリーネットワークをDHCPで構築しているため、ゲストOS側に割り当てられているIPアドレスを調べる。

isucon@vagrant$ ifconfig
# ...
eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.28.128.3  netmask 255.255.255.0  broadcast 172.28.128.255
        inet6 fe80::a00:27ff:fe3f:84dc  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:3f:84:dc  txqueuelen 1000  (Ethernet)
        RX packets 21  bytes 5788 (5.7 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 30  bytes 4428 (4.4 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
# ...

以下のURLでホストOSのブラウザからアクセス
https://172.28.128.3

無事画面が表示できたので、接続成功。

後記

次は計測ツールの導入などしていきたいです。 ただ、提供されているvagrantfileを使うとansibleやソースコードgithubからの入手になってしまっていて手軽にコード修正&反映ができないので、ローカルのものを見るように改造するところからかな。