mirror of
https://github.com/metosin/reitit.git
synced 2025-12-24 02:48:25 +00:00
Run simple json perf test (defaults, yada, reitit)
… not testing exactly the same thing, so the numbers are not correct in any way. Still, looking good.
This commit is contained in:
parent
4be84d22f4
commit
68282a3dc7
2 changed files with 112 additions and 1 deletions
109
perf-test/clj/reitit/json_perf.cljc
Normal file
109
perf-test/clj/reitit/json_perf.cljc
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
(ns reitit.json-perf
|
||||
(:require [criterium.core :as cc]
|
||||
[reitit.perf-utils :refer :all]
|
||||
|
||||
;; reitit
|
||||
[reitit.ring :as ring]
|
||||
[muuntaja.middleware :as mm]
|
||||
|
||||
;; bidi-yada
|
||||
[yada.yada :as yada]
|
||||
[bidi.ring :as bidi-ring]
|
||||
[byte-streams :as bs]
|
||||
|
||||
;; defaults
|
||||
[ring.middleware.defaults :as defaults]
|
||||
[compojure.core :as compojure]
|
||||
[clojure.string :as str]))
|
||||
|
||||
;;
|
||||
;; 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
|
||||
;;
|
||||
|
||||
;; TODO: naive implementation
|
||||
(defn- with-security-headers [response]
|
||||
(update
|
||||
response
|
||||
:headers
|
||||
(fn [headers]
|
||||
(-> headers
|
||||
(assoc "x-frame-options" "SAMEORIGIN")
|
||||
(assoc "x-xss-protection" "1; mode=block")
|
||||
(assoc "x-content-type-options" "nosniff")))))
|
||||
|
||||
(def security-middleware
|
||||
{:name ::security
|
||||
:wrap (fn [handler]
|
||||
(fn [request]
|
||||
(with-security-headers (handler request))))})
|
||||
|
||||
(def reitit-app
|
||||
(ring/ring-handler
|
||||
(ring/router
|
||||
["/api/ping"
|
||||
{:get {:handler (fn [_] {:status 200, :body {:ping "pong"}})}}]
|
||||
{:data {:middleware [mm/wrap-format
|
||||
security-middleware]}})))
|
||||
|
||||
(def bidi-yada-app
|
||||
(bidi-ring/make-handler
|
||||
["/api/ping"
|
||||
(yada/resource
|
||||
{:produces {:media-type "application/json"}
|
||||
:methods {:get {:response (fn [_] {:ping "pong"})}}})]))
|
||||
|
||||
(def defaults-app
|
||||
(defaults/wrap-defaults
|
||||
(mm/wrap-format
|
||||
(compojure/GET "/api/ping" [] {:status 200, :body {:ping "pong"}}))
|
||||
defaults/site-defaults))
|
||||
|
||||
(def request {:request-method :get, :uri "/api/ping"})
|
||||
|
||||
(comment
|
||||
(defaults-app request)
|
||||
@(bidi-yada-app request)
|
||||
(reitit-app request))
|
||||
|
||||
(comment
|
||||
(slurp (:body (defaults-app request)))
|
||||
(slurp (:body (reitit-app request)))
|
||||
(bs/to-string (:body @(bidi-yada-app request))))
|
||||
|
||||
|
||||
(defn expect! [body]
|
||||
(assert (str/starts-with? body "{\"ping\":\"pong\"}")))
|
||||
|
||||
(defn perf-test []
|
||||
|
||||
;; 176µs
|
||||
(title "compojure + ring-defaults")
|
||||
(let [f (fn [] (defaults-app request))]
|
||||
(expect! (-> (f) :body slurp))
|
||||
(cc/quick-bench (f)))
|
||||
|
||||
;; 60µs
|
||||
(title "bidi + yada")
|
||||
(let [f (fn [] (bidi-yada-app request))]
|
||||
(expect! (-> (f) deref :body bs/to-string))
|
||||
(cc/quick-bench (f)))
|
||||
|
||||
;; 5.0µs
|
||||
(title "reitit-ring")
|
||||
(let [f (fn [] (reitit-app request))]
|
||||
(expect! (-> (f) :body slurp))
|
||||
(cc/quick-bench (f))))
|
||||
|
||||
(comment
|
||||
(perf-test))
|
||||
|
|
@ -53,7 +53,7 @@
|
|||
|
||||
[ring "1.6.3"]
|
||||
[ikitommi/immutant-web "3.0.0-alpha1"]
|
||||
[metosin/muuntaja "0.5.0"]
|
||||
[metosin/muuntaja "0.6.0-SNAPSHOT"]
|
||||
[metosin/ring-swagger-ui "2.2.10"]
|
||||
[metosin/jsonista "0.2.1"]
|
||||
|
||||
|
|
@ -70,6 +70,8 @@
|
|||
[ikitommi/immutant-web "3.0.0-alpha1"]
|
||||
[io.pedestal/pedestal.route "0.5.3"]
|
||||
[org.clojure/core.async "0.4.474"]
|
||||
[yada "1.2.13"]
|
||||
[ring/ring-defaults "0.3.1"]
|
||||
[ataraxy "0.4.0"]
|
||||
[bidi "2.1.3"]]}
|
||||
:analyze {:jvm-opts ^:replace ["-server"
|
||||
|
|
|
|||
Loading…
Reference in a new issue