From 277faf48f5da4ca355417094c431eb0f3b09c381 Mon Sep 17 00:00:00 2001 From: Tommi Reiman Date: Sat, 9 Feb 2019 21:18:02 +0200 Subject: [PATCH] Server benchmarks --- perf-test/clj/reitit/json_perf.cljc | 47 +++++++++++++++++------ perf-test/clj/reitit/nodejs_perf_test.clj | 16 ++++++++ 2 files changed, 51 insertions(+), 12 deletions(-) diff --git a/perf-test/clj/reitit/json_perf.cljc b/perf-test/clj/reitit/json_perf.cljc index b9d81dfd..5c26dd0a 100644 --- a/perf-test/clj/reitit/json_perf.cljc +++ b/perf-test/clj/reitit/json_perf.cljc @@ -2,9 +2,13 @@ (:require [criterium.core :as cc] [reitit.perf-utils :refer :all] + ;; aleph + [aleph.http :as http] + ;; reitit [reitit.ring :as ring] - [muuntaja.middleware :as mm] + [muuntaja.core :as m] + [reitit.ring.middleware.muuntaja :as rm] ;; bidi-yada [yada.yada :as yada] @@ -14,7 +18,8 @@ ;; defaults [ring.middleware.defaults :as defaults] [compojure.core :as compojure] - [clojure.string :as str])) + [clojure.string :as str] + [muuntaja.middleware :as mm])) ;; ;; start repl with `lein perf repl` @@ -31,16 +36,16 @@ ;; Memory: 16 GB ;; -;; TODO: naive implementation (defn- with-security-headers [response] - (update + (assoc response :headers - (fn [headers] - (-> headers - (assoc "x-frame-options" "SAMEORIGIN") - (assoc "x-xss-protection" "1; mode=block") - (assoc "x-content-type-options" "nosniff"))))) + (reduce-kv + assoc + {"x-frame-options" "SAMEORIGIN" + "x-xss-protection" "1; mode=block" + "x-content-type-options" "nosniff"} + (:headers response)))) (def security-middleware {:name ::security @@ -53,7 +58,8 @@ (ring/router ["/api/ping" {:get {:handler (fn [_] {:status 200, :body {:ping "pong"}})}}] - {:data {:middleware [mm/wrap-format + {:data {:muuntaja (m/create (assoc m/default-options :return :bytes)) + :middleware [rm/format-middleware security-middleware]}}))) (def bidi-yada-app @@ -87,7 +93,7 @@ (defn perf-test [] - ;; 176µs + ;; 206µs (title "compojure + ring-defaults") (let [f (fn [] (defaults-app request))] (expect! (-> (f) :body slurp)) @@ -99,7 +105,7 @@ (expect! (-> (f) deref :body bs/to-string)) (cc/quick-bench (f))) - ;; 5.0µs + ;; 6.0µs (title "reitit-ring") (let [f (fn [] (reitit-app request))] (expect! (-> (f) :body slurp)) @@ -107,3 +113,20 @@ (comment (perf-test)) + +(comment + + ;; 10198 + ;; http :3000/api/ping + ;; wrk -d ${DURATION:="30s"} http://127.0.0.1:3000/api/ping + (http/start-server defaults-app {:port 3000}) + + ;; 16230 + ;; http :3001/api/ping + ;; wrk -d ${DURATION:="30s"} http://127.0.0.1:3001/api/ping + (http/start-server bidi-yada-app {:port 3001}) + + ;; 48084 + ;; http :3002/api/ping + ;; wrk -d ${DURATION:="30s"} http://127.0.0.1:3002/api/ping + (http/start-server reitit-app {:port 3002})) diff --git a/perf-test/clj/reitit/nodejs_perf_test.clj b/perf-test/clj/reitit/nodejs_perf_test.clj index 7b30f118..5a427e1c 100644 --- a/perf-test/clj/reitit/nodejs_perf_test.clj +++ b/perf-test/clj/reitit/nodejs_perf_test.clj @@ -29,6 +29,9 @@ (for [name ["product" "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "twenty"]] [(str "/" name "/:id") {:get (partial h name)}])))) +(for [name ["product" "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "twenty"]] + [(str "/" name "/:id") {:get (partial h name)}]) + (app {:request-method :get, :uri "/product/foo"}) (defn routing-test [] @@ -79,3 +82,16 @@ (comment (web/run app {:port 2048, :dispatch? false, :server {:always-set-keep-alive false}}) (routing-test)) + +(comment + (require '[compojure.core :as c]) + (def app (apply + c/routes + (for [name ["product" "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "twenty"]] + (eval `(c/GET ~(str "/" name "/:id") [~'id] (str "Got " ~name " id " ~'id)))))) + + (require '[ring.adapter.jetty :as jetty]) + ;; 57862 / 54290 + ;; wrk -d ${DURATION:="30s"} http://127.0.0.1:8080/product/foo + ;; wrk -d ${DURATION:="30s"} http://127.0.0.1:8080/twenty/bar + (jetty/run-jetty app {:port 8080}))