CrystalとGoでHTTPサーバのベンチマーク

[Big Sky :: Crystal と CRuby でHTTPサーバのベンチマーク](http://mattn.kaoriya.net/software/lang/ruby/20150618020626.htm)

こちらの記事を見て気になったのでGoと比較してみた。

※ Goのプロセス数が1の場合の他に、全コア分使うコードでの測定も追記した。

## Crystal

### バージョン

```shell-session
$ crystal --version                                                                                                                                                                                     15:13:21
Crystal 0.7.3 [bf72b07] (Sun Jun  7 16:17:59 UTC 2015)
```

### コード

上記の記事とまったく同じ。
--release付きでビルドした。

-----

## Go(1プロセス版）

### バージョン

```shell
$ go version
go version go1.4.2 darwin/amd64
```

### コード

```go
package main

import (
	"fmt"
	"net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Hello World!")
}

func main() {
	http.HandleFunc("/", handler)
	http.ListenAndServe(":8080", nil)
}
```

### ビルドと実行

```shell-session
$ go build main.go
$ ./main
```

-----

## Go(全コア使う版）

コード以外は上記1プロセス版と同じ。

### コード

```go
package main

import (
	"fmt"
	"net/http"
	"runtime"
)

func handler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Hello World!")
}

func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())
	http.HandleFunc("/", handler)
	http.ListenAndServe(":8080", nil)
}
```

-----

# ベンチマーク

## 実行環境

```
  機種名：	MacBook Pro
  プロセッサ名：	Intel Core i5
  プロセッサ速度：	2.8 GHz
  プロセッサの個数：	1
  コアの総数：	2
  二次キャッシュ（コア単位）：	256 KB
  三次キャッシュ：	3 MB
  メモリ：	8 GB
  OS: OSX 10.10.3
```

## 実行コマンド

```shell-session
$ ab -k -c 10 -n 10000 http://localhost:8080/
```

## 結果

### まとめ

| 言語 | 秒間リクエスト処理数 |
|:-----------|------------:|
|Crystal|28216.23|
|Go(1プロセス版）|21486.29|
|Go(全コア使う版）|40682.65|

### Crystal

```shell-session
This is ApacheBench, Version 2.3 <$Revision: 1604373 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests


Server Software:
Server Hostname:        localhost
Server Port:            8080

Document Path:          /
Document Length:        12 bytes

Concurrency Level:      10
Time taken for tests:   0.354 seconds
Complete requests:      10000
Failed requests:        0
Keep-Alive requests:    10000
Total transferred:      1010000 bytes
HTML transferred:       120000 bytes
Requests per second:    28216.23 [#/sec] (mean)
Time per request:       0.354 [ms] (mean)
Time per request:       0.035 [ms] (mean, across all concurrent requests)
Transfer rate:          2783.05 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       1
Processing:     0    0   3.1      0     305
Waiting:        0    0   3.1      0     305
Total:          0    0   3.1      0     305

Percentage of the requests served within a certain time (ms)
  50%      0
  66%      0
  75%      0
  80%      0
  90%      1
  95%      1
  98%      1
  99%      1
 100%    305 (longest request)
```

### Go(1プロセス版）

```shell-session
This is ApacheBench, Version 2.3 <$Revision: 1604373 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests


Server Software:
Server Hostname:        localhost
Server Port:            8080

Document Path:          /
Document Length:        12 bytes

Concurrency Level:      10
Time taken for tests:   0.465 seconds
Complete requests:      10000
Failed requests:        0
Keep-Alive requests:    10000
Total transferred:      1530000 bytes
HTML transferred:       120000 bytes
Requests per second:    21486.29 [#/sec] (mean)
Time per request:       0.465 [ms] (mean)
Time per request:       0.047 [ms] (mean, across all concurrent requests)
Transfer rate:          3210.35 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       0
Processing:     0    0   0.3      0       3
Waiting:        0    0   0.3      0       3
Total:          0    0   0.3      0       3

Percentage of the requests served within a certain time (ms)
  50%      0
  66%      1
  75%      1
  80%      1
  90%      1
  95%      1
  98%      1
  99%      2
 100%      3 (longest request)
```

### Go(全コア使う版）

```shell-session
This is ApacheBench, Version 2.3 <$Revision: 1604373 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests


Server Software:
Server Hostname:        localhost
Server Port:            8080

Document Path:          /
Document Length:        12 bytes

Concurrency Level:      10
Time taken for tests:   0.246 seconds
Complete requests:      10000
Failed requests:        0
Keep-Alive requests:    10000
Total transferred:      1530000 bytes
HTML transferred:       120000 bytes
Requests per second:    40682.65 [#/sec] (mean)
Time per request:       0.246 [ms] (mean)
Time per request:       0.025 [ms] (mean, across all concurrent requests)
Transfer rate:          6078.56 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       1
Processing:     0    0   0.2      0       4
Waiting:        0    0   0.2      0       4
Total:          0    0   0.2      0       4

Percentage of the requests served within a certain time (ms)
  50%      0
  66%      0
  75%      0
  80%      0
  90%      0
  95%      1
  98%      1
  99%      1
 100%      4 (longest request)
```
-----

Crystal速い。でもどっちも十分な速さではある。
Crystalの並行・並列処理の辺りがどうなっているかわかっていないので、
その辺りを追いかけてみたい。

-----

## 追記

Crystalの作者であるasteriteさんから、spawnという並列処理用のコマンドがあると
教えていただいたので、試してみました。

```crystal
 100.times do
   spawn do
     sleep 1
     puts Time.now
   end
 end

 sleep 5
```

これで並列処理されなければ100秒以上かかる処理のはずですが、
実行するとすぐ終わります。
まさにgoroutineのような感じで素晴らしい。
まだ開発中のようですが、今後がとても楽しみです。