Skip to content

Instantly share code, notes, and snippets.

@malcolmsparks
Created December 11, 2014 09:34
Show Gist options
  • Save malcolmsparks/1c26adba3a55cefa057a to your computer and use it in GitHub Desktop.
Save malcolmsparks/1c26adba3a55cefa057a to your computer and use it in GitHub Desktop.
perf testing with http-kit and core.match
(ns perf.test
(:require
[org.httpkit.client :refer (request) :rename {request http-request}]
[clojure.core.async :refer (chan >! <! go go-loop)]
[clojure.core.match :refer (match)]
[ring.mock.request :refer (request) :rename {request mock-request}]))
(defn with-profiling [p & {:keys [callback]}]
(let [start (System/nanoTime)
dur (atom nil)]
(reify Object
clojure.lang.IDeref
(deref [_] (let [val (deref p)] [val @dur]))
clojure.lang.IFn
(invoke [_ val]
(let [d (reset! dur (- (System/nanoTime) start))]
(when callback
(callback val d))
[(deliver p val) d]))
clojure.lang.IPending
(isRealized [_] (realized? p)))))
;; Kick off some perf jobs
(let [ch (chan 20)]
(go-loop []
(let [[val timings] (<! ch)]
(println (:status val) timings))
(recur))
(dotimes [n 400]
(http-request
{:method :get
:url "http://localhost:631"
:headers {}
:timeout 1000
:response (with-profiling (promise)
:callback (fn [val dur]
(go (>! ch [val {:timings {:request dur}}]))))}
nil ; http-kit sees this as 'identity'
)))
(let [req (mock-request :get "/overview.html")
resp {:status 200}]
(match [req resp]
[{:uri "/" :use-case :login} {:status 200}] {:uri "/overview.html" :form {"user" "fred"}}
[{:uri "/overview.html" :use-case :login} {:status 200}] :end
[{:uri "/overview.html"} {:status 404}] :error
[{:uri "/overview.html"} {:status 404}] :error
[{:uri "/overview.html"} {:status 404}] :error
[{:uri "/overview.html"} {:status 404}] :error
[{:uri "/overview.html"} {:status 404}] :error
[_ _] :error))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment