|
| 1 | +(ns compojure.api.perf-test |
| 2 | + (:require [compojure.api.sweet :refer :all] |
| 3 | + [compojure.api.test-utils :refer :all] |
| 4 | + [criterium.core :as cc] |
| 5 | + [ring.util.http-response :refer :all] |
| 6 | + [schema.core :as s])) |
| 7 | + |
| 8 | +;; |
| 9 | +;; start repl with `lein perf repl` |
| 10 | +;; perf measured with the following setup: |
| 11 | +;; |
| 12 | +;; Model Name: MacBook Pro |
| 13 | +;; Model Identifier: MacBookPro11,3 |
| 14 | +;; Processor Name: Intel Core i7 |
| 15 | +;; Processor Speed: 2,5 GHz |
| 16 | +;; Number of Processors: 1 |
| 17 | +;; Total Number of Cores: 4 |
| 18 | +;; L2 Cache (per Core): 256 KB |
| 19 | +;; L3 Cache: 6 MB |
| 20 | +;; Memory: 16 GB |
| 21 | +;; |
| 22 | + |
| 23 | +(defn title [s] |
| 24 | + (println |
| 25 | + (str "\n\u001B[35m" |
| 26 | + (apply str (repeat (+ 6 (count s)) "#")) |
| 27 | + "\n## " s " ##\n" |
| 28 | + (apply str (repeat (+ 6 (count s)) "#")) |
| 29 | + "\u001B[0m\n"))) |
| 30 | + |
| 31 | +(s/defschema Order {:id s/Str |
| 32 | + :name s/Str |
| 33 | + (s/optional-key :description) s/Str |
| 34 | + :address (s/maybe {:street s/Str |
| 35 | + :country (s/enum "FI" "PO")}) |
| 36 | + :orders [{:name #"^k" |
| 37 | + :price s/Any |
| 38 | + :shipping s/Bool}]}) |
| 39 | + |
| 40 | +(defn bench [] |
| 41 | + |
| 42 | + |
| 43 | + (let [app (api |
| 44 | + (GET* "/30" [] |
| 45 | + (ok {:result 30}))) |
| 46 | + call #(get* app "/30")] |
| 47 | + |
| 48 | + (title "GET JSON") |
| 49 | + |
| 50 | + (assert (= {:result 30} (second (call)))) |
| 51 | + (cc/bench (call))) |
| 52 | + |
| 53 | + ; 26µs => 26µs (-0%) |
| 54 | + |
| 55 | + (let [app (api |
| 56 | + (POST* "/plus" [] |
| 57 | + :return {:result s/Int} |
| 58 | + :body-params [x :- s/Int, y :- s/Int] |
| 59 | + (ok {:result (+ x y)}))) |
| 60 | + data (json {:x 10, :y 20}) |
| 61 | + call #(post* app "/plus" data)] |
| 62 | + |
| 63 | + (title "JSON POST with 2-way coercion") |
| 64 | + |
| 65 | + (assert (= {:result 30} (second (call)))) |
| 66 | + (cc/bench (call))) |
| 67 | + |
| 68 | + ;; 87µs => 65µs (-25%) |
| 69 | + |
| 70 | + (let [app (api |
| 71 | + (context* "/a" [] |
| 72 | + (context* "/b" [] |
| 73 | + (context* "/c" [] |
| 74 | + (POST* "/plus" [] |
| 75 | + :return {:result s/Int} |
| 76 | + :body-params [x :- s/Int, y :- s/Int] |
| 77 | + (ok {:result (+ x y)})))))) |
| 78 | + data (json {:x 10, :y 20}) |
| 79 | + call #(post* app "/a/b/c/plus" data)] |
| 80 | + |
| 81 | + (title "JSON POST with 2-way coercion + contexts") |
| 82 | + |
| 83 | + (assert (= {:result 30} (second (call)))) |
| 84 | + (cc/bench (call))) |
| 85 | + |
| 86 | + ;; 102µs => 78µs (-24%) |
| 87 | + |
| 88 | + (let [app (api |
| 89 | + (POST* "/echo" [] |
| 90 | + :return Order |
| 91 | + :body [order Order] |
| 92 | + (ok order))) |
| 93 | + data (json {:id "123" |
| 94 | + :name "Tommi's order" |
| 95 | + :description "Totally great order" |
| 96 | + :address {:street "Randomstreet 123" |
| 97 | + :country "FI"} |
| 98 | + :orders [{:name "k1" |
| 99 | + :price 123.0 |
| 100 | + :shipping true} |
| 101 | + {:name "k2" |
| 102 | + :price 42.0 |
| 103 | + :shipping false}]}) |
| 104 | + call #(post* app "/echo" data)] |
| 105 | + |
| 106 | + (title "JSON POST with nested data") |
| 107 | + |
| 108 | + (s/check Order (second (call))) |
| 109 | + (cc/bench (call))) |
| 110 | + |
| 111 | + ;; 311µs => 194µs (-38%) |
| 112 | + |
| 113 | + ) |
| 114 | + |
| 115 | +(comment |
| 116 | + (bench)) |
0 commit comments