Coercion performance

This commit is contained in:
Tommi Reiman 2019-12-28 10:44:21 +02:00
parent fa0c00c5b2
commit 716c845a05

View file

@ -6,11 +6,11 @@
[spec-tools.core :as st] [spec-tools.core :as st]
[muuntaja.middleware :as mm] [muuntaja.middleware :as mm]
[muuntaja.core :as m] [muuntaja.core :as m]
[muuntaja.format.jsonista :as jsonista-format]
[jsonista.core :as j] [jsonista.core :as j]
[reitit.ring.coercion :as rrc] [reitit.ring.coercion :as rrc]
[reitit.coercion.spec :as spec] [reitit.coercion.spec :as spec]
[reitit.coercion.schema :as schema] [reitit.coercion.schema :as schema]
[reitit.coercion.malli :as malli]
[reitit.coercion :as coercion] [reitit.coercion :as coercion]
[reitit.ring :as ring])) [reitit.ring :as ring]))
@ -173,15 +173,14 @@
(defn json-perf-test [] (defn json-perf-test []
(title "json") (title "json")
(let [m (m/create (jsonista-format/with-json-format m/default-options)) (let [app (ring/ring-handler
app (ring/ring-handler
(ring/router (ring/router
["/plus" {:post {:handler (fn [request] ["/plus" {:post {:handler (fn [request]
(let [body (:body-params request) (let [body (:body-params request)
x (:x body) x (:x body)
y (:y body)] y (:y body)]
{:status 200, :body {:result (+ x y)}}))}}] {:status 200, :body {:result (+ x y)}}))}}]
{:data {:middleware [[mm/wrap-format m]]}})) {:data {:middleware [mm/wrap-format]}}))
request {:request-method :post request {:request-method :post
:uri "/plus" :uri "/plus"
:headers {"content-type" "application/json"} :headers {"content-type" "application/json"}
@ -196,15 +195,14 @@
(defn schema-json-perf-test [] (defn schema-json-perf-test []
(title "schema-json") (title "schema-json")
(let [m (m/create (jsonista-format/with-json-format m/default-options)) (let [app (ring/ring-handler
app (ring/ring-handler
(ring/router (ring/router
["/plus" {:post {:responses {200 {:body {:result Long}}} ["/plus" {:post {:responses {200 {:body {:result Long}}}
:parameters {:body {:x Long, :y Long}} :parameters {:body {:x Long, :y Long}}
:handler (fn [request] :handler (fn [request]
(let [body (-> request :parameters :body)] (let [body (-> request :parameters :body)]
{:status 200, :body {:result (+ (:x body) (:y body))}}))}}] {:status 200, :body {:result (+ (:x body) (:y body))}}))}}]
{:data {:middleware [[mm/wrap-format m] {:data {:middleware [mm/wrap-format
rrc/coerce-request-middleware rrc/coerce-request-middleware
rrc/coerce-response-middleware] rrc/coerce-response-middleware]
:coercion schema/coercion}})) :coercion schema/coercion}}))
@ -234,6 +232,7 @@
:coercion schema/coercion}})) :coercion schema/coercion}}))
request {:request-method :post request {:request-method :post
:uri "/plus" :uri "/plus"
:muuntaja/request {:format "application/json"}
:body-params {:x 1, :y 2}} :body-params {:x 1, :y 2}}
call (fn [] (-> request app :body))] call (fn [] (-> request app :body))]
(assert (= {:result 3} (call))) (assert (= {:result 3} (call)))
@ -241,6 +240,7 @@
;; 0.23µs (no coercion) ;; 0.23µs (no coercion)
;; 12.8µs ;; 12.8µs
;; 1.9µs (cached coercers) ;; 1.9µs (cached coercers)
;; 2.5µs (real json)
(cc/quick-bench (cc/quick-bench
(call)))) (call))))
@ -258,11 +258,13 @@
:coercion spec/coercion}})) :coercion spec/coercion}}))
request {:request-method :post request {:request-method :post
:uri "/plus" :uri "/plus"
:muuntaja/request {:format "application/json"}
:body-params {:x 1, :y 2}} :body-params {:x 1, :y 2}}
call (fn [] (-> request app :body))] call (fn [] (-> request app :body))]
(assert (= {:result 3} (call))) (assert (= {:result 3} (call)))
;; 6.0µs ;; 6.0µs
;; 30.0µs (real json)
(cc/quick-bench (cc/quick-bench
(call)))) (call))))
@ -287,17 +289,45 @@
:coercion spec/coercion}})) :coercion spec/coercion}}))
request {:request-method :post request {:request-method :post
:uri "/plus" :uri "/plus"
:muuntaja/request {:format "application/json"}
:body-params {:x 1, :y 2}} :body-params {:x 1, :y 2}}
call (fn [] (-> request app :body))] call (fn [] (-> request app :body))]
(assert (= {:result 3} (call))) (assert (= {:result 3} (call)))
;; 3.2µs ;; 3.2µs
;; 13.0µs (real json)
(cc/quick-bench
(call))))
(defn malli-perf-test []
(title "malli")
(let [app (ring/ring-handler
(ring/router
["/plus" {:post {:responses {200 {:body [:map [:result int?]]}}
:parameters {:body [:map [:x int?] [:y int?]]}
:handler (fn [request]
(let [body (-> request :parameters :body)]
{:status 200, :body {:result (+ (:x body) (:y body))}}))}}]
{:data {:middleware [rrc/coerce-request-middleware
rrc/coerce-response-middleware]
:coercion malli/coercion}}))
request {:request-method :post
:uri "/plus"
:muuntaja/request {:format "application/json"}
:body-params {:x 1, :y 2}}
call (fn [] (-> request app :body))]
(assert (= {:result 3} (call)))
;; 1.2µs (real json)
(cc/quick-bench (cc/quick-bench
(call)))) (call))))
(comment (comment
(json-perf-test) (json-perf-test)
(schema-json-perf-test) (schema-json-perf-test)
(do
(schema-perf-test) (schema-perf-test)
(data-spec-perf-test) (data-spec-perf-test)
(spec-perf-test)) (spec-perf-test)
(malli-perf-test)))