2014/07/11

ClojureでFibonacci(10) Calculation per Request

Express vs Flask vs Go vs Sparkjava
https://medium.com/@tschundeee/express-vs-flask-vs-go-acc0879c2122

Clojureだとどの程度なのか気になったのでベンチとってみたら、
比較用のNodeの4倍程度速度だった。

GolangとSparkjavaはやはり速い……

環境

Hardware: Macbook Air 2011mid Core i7@1.8GHz and 4GB RAM
Software: OSX 10.9.3
Java 1.8.0_05 Java HotSpot(TM) 64-Bit Server VM

ソースコード

github.com/ysasaki/clj-fib-serverにアップしてある

src/fib/handler.clj


(ns fib.handler
  (:gen-class)
  (:require [compojure.core :refer :all]
            [compojure.route :as route]
            [ring.middleware.params :as mp]
            [org.httpkit.server :as httpkit]))

(defn- ^long fib [^long n]
  (case n
    0 0
    1 1
    (+ (fib (- n 1)) (fib (- n 2)))))

(defroutes app-routes
  (GET "/:number" {{number :number} :params}
       {:status 200 :header {"Content-Type" "text/plain"} :body (str (fib (Integer/parseInt number)))}))

(def app
  (mp/wrap-params app-routes))

(defn -main [port]
  (httpkit/run-server app {:port (Integer. port)}))

ベンチマーク

1回目

Running 30s test @ http://localhost:5000/10
  2 threads and 64 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     4.84ms   10.50ms 169.24ms   96.29%
    Req/Sec     9.76k     3.31k   14.07k    77.83%
  571981 requests in 30.00s, 51.82MB read
  Socket errors: connect 0, read 25, write 0, timeout 0
Requests/sec:  19065.88
Transfer/sec:      1.73MB

2回目。JVMが頑張っているのか2回目は大体1000req/secくらい速い

% wrk -c 64 -d 30s http://localhost:5000/10
Running 30s test @ http://localhost:5000/10
  2 threads and 64 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     3.42ms    5.11ms 133.44ms   99.18%
    Req/Sec    10.75k     1.43k   14.22k    85.30%
  605999 requests in 30.00s, 54.90MB read
  Socket errors: connect 0, read 3, write 0, timeout 0
Requests/sec:  20199.93
Transfer/sec:      1.83MB

fibにtype hintsを付けないと1000req/sec減る感じだった

Node

比較用にNodeも元記事のソースコードでベンチマーク

% node -v
v0.10.7

% wrk -c 64 -d 30s http://localhost:3000/10
Running 30s test @ http://localhost:3000/10
  2 threads and 64 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    12.72ms    1.51ms  30.10ms   71.00%
    Req/Sec     2.55k   224.26     3.03k    68.94%
  150699 requests in 30.00s, 31.19MB read
Requests/sec:   5022.71
Transfer/sec:      1.04MB