From 68282a3dc7a79f0432c2c226b65151b142d9099a Mon Sep 17 00:00:00 2001 From: Tommi Reiman Date: Sun, 27 May 2018 09:47:15 +0300 Subject: [PATCH] Run simple json perf test (defaults, yada, reitit) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit … not testing exactly the same thing, so the numbers are not correct in any way. Still, looking good. --- perf-test/clj/reitit/json_perf.cljc | 109 ++++++++++++++++++++++++++++ project.clj | 4 +- 2 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 perf-test/clj/reitit/json_perf.cljc diff --git a/perf-test/clj/reitit/json_perf.cljc b/perf-test/clj/reitit/json_perf.cljc new file mode 100644 index 00000000..b9d81dfd --- /dev/null +++ b/perf-test/clj/reitit/json_perf.cljc @@ -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)) diff --git a/project.clj b/project.clj index 7a682cc8..c4a82e6c 100644 --- a/project.clj +++ b/project.clj @@ -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"