Polish perf tests

This commit is contained in:
Tommi Reiman 2017-08-30 08:16:21 +03:00
parent 4e22fd2f53
commit 86acee9098
3 changed files with 57 additions and 40 deletions

View file

@ -1,5 +1,7 @@
(ns reitit.opensensors-routing-test
(:require [clojure.test :refer [deftest testing is]]
[criterium.core :as cc]
[reitit.perf-utils :refer :all]
[cheshire.core :as json]
[clojure.string :as str]
[reitit.core :as reitit]
@ -16,13 +18,20 @@
[io.pedestal.http.route.router :as pedestal]
[io.pedestal.http.route :as route]))
(defn raw-title [color s]
(println (str color (apply str (repeat (count s) "#")) "\u001B[0m"))
(println (str color s "\u001B[0m"))
(println (str color (apply str (repeat (count s) "#")) "\u001B[0m")))
(def title (partial raw-title "\u001B[35m"))
(def suite (partial raw-title "\u001B[32m"))
;;
;; start repl with `lein perf repl`
;; perf measured with the following setup:
;;
;; Model Name: MacBook Pro
;; Model Identifier: MacBookPro11,3
;; 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
;;
;;
;; extract sample routes
@ -77,15 +86,6 @@
avg (int (/ (reduce + times) (count times)))]
[% avg]) urls)))
(defn bench [routes no-paths?]
(let [routes (mapv (fn [[path name]]
(if no-paths?
[(str/replace path #"\:" "") name]
[path name])) routes)
router (reitit/router routes)]
(doseq [[path time] (bench-routes routes #(reitit/match-by-path router %))]
(println path "\t" time))))
(defn bench [routes no-paths?]
(let [routes (mapv (fn [[path name]]
(if no-paths?
@ -471,7 +471,7 @@
(if-not match
(println route)))))
(defn bench! [routes verbose? name f]
(defn bench!! [routes verbose? name f]
(System/gc)
(println)
(suite name)
@ -493,14 +493,23 @@
compojure-api-f #(opensensors-compojure-api-routes {:uri % :request-method :get})
pedestal-f #(pedestal/find-route opensensors-pedestal-routes {:path-info % :request-method :get})]
(bench! routes true "reitit" reitit-f) ;; 2538ns -> 2028ns
(bench! routes true "reitit-ring" reitit-ring-f) ;; 2845ns -> 2299ns
(bench! routes true "pedestal" pedestal-f) ;; 2737ns
(bench! routes true "compojure-api" compojure-api-f) ;; 9823ns
(bench! routes true "bidi" bidi-f) ;; 16716ns
(bench! routes true "ataraxy" ataraxy-f) ;; 24467ns
;; 2538ns -> 2028ns
(bench!! routes true "reitit" reitit-f)
))
;; 2845ns -> 2299ns
(bench!! routes true "reitit-ring" reitit-ring-f)
;; 2737ns
(bench!! routes true "pedestal" pedestal-f)
;; 9823ns
(bench!! routes true "compojure-api" compojure-api-f)
;; 16716ns
(bench!! routes true "bidi" bidi-f)
;; 24467ns
(bench!! routes true "ataraxy" ataraxy-f)))
(comment
(bench-rest!))
@ -548,16 +557,15 @@
;; 125ns
;; 62ns (fast-map)
(bench! routes false "reitit" reitit-f)
(bench!! routes false "reitit" reitit-f)
;; 272ns
;; 219ns (fast-assoc)
;; 171ns (fast-map)
(bench! routes false "reitit-ring" reitit-ring-f)
(bench!! routes false "reitit-ring" reitit-ring-f)
;; 172ns
(bench! routes false "pedestal" pedestal-f)))
(bench!! routes false "pedestal" pedestal-f)))
(comment
(bench-cqrs!))

View file

@ -1,6 +1,7 @@
(ns reitit.perf-test
(:require [criterium.core :as cc]
[reitit.core :as reitit]
[reitit.perf-utils :refer :all]
[bidi.bidi :as bidi]
[compojure.api.sweet :refer [api routes GET]]
@ -27,14 +28,6 @@
;; Memory: 16 GB
;;
(defn raw-title [color s]
(println (str color (apply str (repeat (count s) "#")) "\u001B[0m"))
(println (str color s "\u001B[0m"))
(println (str color (apply str (repeat (count s) "#")) "\u001B[0m")))
(def title (partial raw-title "\u001B[35m"))
(def suite (partial raw-title "\u001B[32m"))
(def bidi-routes
["/" [["auth/login" :auth/login]
[["auth/recovery/token/" :token] :auth/recovery]
@ -106,7 +99,7 @@
(call)))
;; 1.0µs (-94%)
;; 770ns (-95%, -23%)
;; 690ns (-96%)
(title "reitit")
(let [call #(reitit/match-by-path reitit-routes "/workspace/1/1")]
(assert (call))
@ -117,7 +110,7 @@
(suite "reverse routing")
;; 2.2µs (-56%)
;; 2.0µs (-59%)
(title "bidi")
(let [call #(bidi/path-for bidi-routes :workspace/page :project "1" :page "1")]
(assert (= "/workspace/1/1" (call)))
@ -126,14 +119,14 @@
(title "ataraxy doesn't support reverse routing :(")
;; 3.8µs (-25%)
;; 3.8µs (-22%)
(title "pedestal - map-tree => prefix-tree")
(let [call #(pedestal-url-for :workspace/page :path-params {:project "1" :page "1"})]
(assert (= "/workspace/1/1" (call)))
(cc/quick-bench
(call)))
;; 5.1µs
;; 4.9µs
(title "compojure-api")
(let [call #(routes/path-for* :workspace/page compojure-api-request {:project "1", :page "1"})]
(assert (= "/workspace/1/1" (call)))

View file

@ -0,0 +1,16 @@
(ns reitit.perf-utils
(:require [criterium.core :as cc]))
(defn raw-title [color s]
(println (str color (apply str (repeat (count s) "#")) "\u001B[0m"))
(println (str color s "\u001B[0m"))
(println (str color (apply str (repeat (count s) "#")) "\u001B[0m")))
(def title (partial raw-title "\u001B[35m"))
(def suite (partial raw-title "\u001B[32m"))
(defmacro bench! [name & body]
`(do
(title ~name)
(println ~@body)
(cc/quick-bench ~@body)))