mirror of
https://github.com/metosin/reitit.git
synced 2026-02-25 02:32:22 +00:00
Request perf test
This commit is contained in:
parent
7a7542b57f
commit
14595f8037
1 changed files with 96 additions and 0 deletions
96
perf-test/clj/reitit/request_perf.cljc
Normal file
96
perf-test/clj/reitit/request_perf.cljc
Normal file
|
|
@ -0,0 +1,96 @@
|
||||||
|
(ns reitit.request-perf
|
||||||
|
(:require [criterium.core :as cc]
|
||||||
|
[reitit.perf-utils :refer :all]
|
||||||
|
[potemkin :as p]))
|
||||||
|
|
||||||
|
(set! *warn-on-reflection* true)
|
||||||
|
|
||||||
|
;;
|
||||||
|
;; start repl with `lein perf repl`
|
||||||
|
;; perf measured with the following setup:
|
||||||
|
;;
|
||||||
|
;; Model Name: MacBook Pro
|
||||||
|
;; Model Identifier: MacBookPro113
|
||||||
|
;; Processor Name: Intel Core i7
|
||||||
|
;; Processor Speed: 2,5 GHz
|
||||||
|
;; Number of Processors: 1
|
||||||
|
;; Total Number of Cores: 4
|
||||||
|
;; L2 Cache (per Core): 256 KB
|
||||||
|
;; L3 Cache: 6 MB
|
||||||
|
;; Memory: 16 GB
|
||||||
|
;;
|
||||||
|
|
||||||
|
(defprotocol RawRequest
|
||||||
|
(-uri [this])
|
||||||
|
(-request-method [this])
|
||||||
|
(-path-params [this]))
|
||||||
|
|
||||||
|
(p/def-derived-map
|
||||||
|
ZeroCopyRequest
|
||||||
|
[raw]
|
||||||
|
:uri (-uri raw)
|
||||||
|
:request-method (-request-method raw)
|
||||||
|
:path-params (-path-params raw))
|
||||||
|
|
||||||
|
(defprotocol RingRequest
|
||||||
|
(get-uri [this])
|
||||||
|
(get-request-method [this])
|
||||||
|
(get-path-params [this]))
|
||||||
|
|
||||||
|
(defn ring-request [raw]
|
||||||
|
{:uri (-uri raw)
|
||||||
|
:request-method (-request-method raw)
|
||||||
|
:path-params (-path-params raw)})
|
||||||
|
|
||||||
|
(defn record-request [raw]
|
||||||
|
(->RecordRequest (-uri raw) (-request-method raw) (-path-params raw)))
|
||||||
|
|
||||||
|
(defrecord RawRingRequest [raw]
|
||||||
|
RingRequest
|
||||||
|
(get-uri [_] (-uri raw))
|
||||||
|
(get-request-method [_] (-request-method raw))
|
||||||
|
(get-path-params [_] (-path-params raw)))
|
||||||
|
|
||||||
|
(def raw
|
||||||
|
(reify
|
||||||
|
RawRequest
|
||||||
|
(-uri [_] "/ping")
|
||||||
|
(-request-method [_] :get)
|
||||||
|
(-path-params [_] {:a 1})))
|
||||||
|
|
||||||
|
(defn bench-all! []
|
||||||
|
|
||||||
|
;; 530ns
|
||||||
|
(title "potemkin zero-copy")
|
||||||
|
(assert (= :get (:request-method (->ZeroCopyRequest raw))))
|
||||||
|
(cc/quick-bench
|
||||||
|
(let [req (->ZeroCopyRequest raw)]
|
||||||
|
(dotimes [_ 10]
|
||||||
|
(:request-method req))))
|
||||||
|
|
||||||
|
;; 73ns
|
||||||
|
(title "map copy-request")
|
||||||
|
(assert (= :get (:request-method (ring-request raw))))
|
||||||
|
(cc/quick-bench
|
||||||
|
(let [req (ring-request raw)]
|
||||||
|
(dotimes [_ 10]
|
||||||
|
(:request-method req))))
|
||||||
|
|
||||||
|
;; 7ns
|
||||||
|
(title "record copy-request")
|
||||||
|
(assert (= :get (:request-method (record-request raw))))
|
||||||
|
(cc/quick-bench
|
||||||
|
(let [req (record-request raw)]
|
||||||
|
(dotimes [_ 10]
|
||||||
|
(:request-method req))))
|
||||||
|
|
||||||
|
;; 7ns
|
||||||
|
(title "request protocols")
|
||||||
|
(assert (= :get (get-request-method (->RawRingRequest raw))))
|
||||||
|
(cc/quick-bench
|
||||||
|
(let [req (->RawRingRequest raw)]
|
||||||
|
(dotimes [_ 10]
|
||||||
|
(get-request-method req)))))
|
||||||
|
|
||||||
|
(comment
|
||||||
|
(bench-all!))
|
||||||
Loading…
Reference in a new issue