Matz も驚く Crystal
これ、Rubyでもそのまま動くんじゃないか? Crystalすげーっ。 https://t.co/pO0F0vqTly
— Yukihiro Matsumoto (@yukihiro_matz) June 16, 2015
Crystal
Language Goals Ruby-inspired syntax. Statically type-checked but without having to specify the type ...
http://crystal-lang.org/
ruby と殆ど同じシンタックスが通る様です。
# A very basic HTTP server
require "http/server"
server = HTTP::Server.new(8080) do |request|
HTTP::Response.ok "text/plain", "Hello world!"
end
puts "Listening on http://0.0.0.0:8080"
server.listen
しかも native code が吐けるらしい。ならばとベンチマークを取ってみた。CRuby 側は以下の sinatra。CRuby は 2.2.0p0 を使用。
require "sinatra"
set :environment, :production
get "/" do
content_type 'text/plain'
"Hello world!"
end
$ ab -k -c 10 -n 10000 http://localhost:8000
まずは sinatra
Server Hostname: localhost
Server Port: 4567
Document Path: /
Document Length: 12 bytes
Concurrency Level: 10
Time taken for tests: 8.502 seconds
Complete requests: 10000
Failed requests: 0
Keep-Alive requests: 10000
Total transferred: 1620000 bytes
HTML transferred: 120000 bytes
Requests per second: 1176.24 [#/sec] (mean)
Time per request: 8.502 [ms] (mean)
Time per request: 0.850 [ms] (mean, across all concurrent requests)
Transfer rate: 186.08 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 0
Processing: 1 8 2.6 8 66
Waiting: 1 8 2.6 8 66
Total: 1 8 2.6 8 67
Percentage of the requests served within a certain time (ms)
50% 8
66% 9
75% 9
80% 10
90% 11
95% 13
98% 14
99% 16
100% 67 (longest request)
そして Crystal
追記: 以下はデバッグビルドの結果でした。その下に続けてリリースビルド時の結果を追記しています。
デバッグビルド
Server Hostname: localhost
Server Port: 8080
Document Path: /
Document Length: 12 bytes
Concurrency Level: 10
Time taken for tests: 0.633 seconds
Complete requests: 10000
Failed requests: 0
Keep-Alive requests: 10000
Total transferred: 1010000 bytes
HTML transferred: 120000 bytes
Requests per second: 15802.98 [#/sec] (mean)
Time per request: 0.633 [ms] (mean)
Time per request: 0.063 [ms] (mean, across all concurrent requests)
Transfer rate: 1558.69 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 1
Processing: 0 1 0.2 1 3
Waiting: 0 1 0.2 1 3
Total: 0 1 0.2 1 3
Percentage of the requests served within a certain time (ms)
50% 1
66% 1
75% 1
80% 1
90% 1
95% 1
98% 1
99% 1
100% 3 (longest request)
リリースビルド
Server Software:
Server Hostname: localhost
Server Port: 8080
Document Path: /
Document Length: 12 bytes
Concurrency Level: 10
Time taken for tests: 0.295 seconds
Complete requests: 10000
Failed requests: 0
Keep-Alive requests: 10000
Total transferred: 1010000 bytes
HTML transferred: 120000 bytes
Requests per second: 33888.54 [#/sec] (mean)
Time per request: 0.295 [ms] (mean)
Time per request: 0.030 [ms] (mean, across all concurrent requests)
Transfer rate: 3342.52 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 1
Processing: 0 0 0.1 0 1
Waiting: 0 0 0.1 0 1
Total: 0 0 0.1 0 1
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% 1 (longest request)
比べ物にならなかった。ただ、h2o や http-server だとファイルをサーブしたとしても 28000req/sec から 30000req/sec は出る環境なので、Crystal のネイティブコードがめちゃくちゃ速いという訳ではない。