Skip to content

Commit 59fb5b5

Browse files
committed
Rerun perf-tests without response-parsing
1 parent 8258f28 commit 59fb5b5

1 file changed

Lines changed: 45 additions & 33 deletions

File tree

test/compojure/api/perf_test.clj

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
(ns compojure.api.perf-test
22
(:require [compojure.api.sweet :refer :all]
3-
[compojure.api.test-utils :refer :all]
3+
[compojure.api.test-utils :as h]
44
[criterium.core :as cc]
55
[ring.util.http-response :refer :all]
6-
[schema.core :as s]))
6+
[schema.core :as s]
7+
[clojure.java.io :as io]
8+
[cheshire.core :as json]))
79

810
;;
911
;; start repl with `lein perf repl`
1012
;; perf measured with the following setup:
1113
;;
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
14+
;; Model Name: MacBook Pro
15+
;; Model Identifier: MacBookPro11,3
16+
;; Processor Name: Intel Core i7
17+
;; Processor Speed: 2,5 GHz
18+
;; Number of Processors: 1
19+
;; Total Number of Cores: 4
20+
;; L2 Cache (per Core): 256 KB
21+
;; L3 Cache: 6 MB
22+
;; Memory: 16 GB
2123
;;
2224

2325
(defn title [s]
@@ -28,6 +30,17 @@
2830
(apply str (repeat (+ 6 (count s)) "#"))
2931
"\u001B[0m\n")))
3032

33+
(defn post* [app uri json]
34+
(->
35+
(app {:uri uri
36+
:request-method :post
37+
:content-type "application/json"
38+
:body (io/input-stream (.getBytes json))})
39+
:body
40+
slurp))
41+
42+
(defn parse [s] (json/parse-string s true))
43+
3144
(s/defschema Order {:id s/Str
3245
:name s/Str
3346
(s/optional-key :description) s/Str
@@ -39,33 +52,32 @@
3952

4053
(defn bench []
4154

42-
4355
(let [app (api
4456
(GET* "/30" []
4557
(ok {:result 30})))
46-
call #(get* app "/30")]
58+
call #(h/get* app "/30")]
4759

4860
(title "GET JSON")
4961

5062
(assert (= {:result 30} (second (call))))
5163
(cc/bench (call)))
5264

53-
; 26µs => 26µs (-0%)
65+
; 27µs => 27µs (-0%)
5466

5567
(let [app (api
5668
(POST* "/plus" []
5769
:return {:result s/Int}
5870
:body-params [x :- s/Int, y :- s/Int]
5971
(ok {:result (+ x y)})))
60-
data (json {:x 10, :y 20})
72+
data (h/json {:x 10, :y 20})
6173
call #(post* app "/plus" data)]
6274

6375
(title "JSON POST with 2-way coercion")
6476

65-
(assert (= {:result 30} (second (call))))
77+
(assert (= {:result 30} (parse (call))))
6678
(cc/bench (call)))
6779

68-
;; 87µs => 65µs (-25%)
80+
;; 73µs => 53µs (-27%)
6981

7082
(let [app (api
7183
(context* "/a" []
@@ -75,40 +87,40 @@
7587
:return {:result s/Int}
7688
:body-params [x :- s/Int, y :- s/Int]
7789
(ok {:result (+ x y)}))))))
78-
data (json {:x 10, :y 20})
90+
data (h/json {:x 10, :y 20})
7991
call #(post* app "/a/b/c/plus" data)]
8092

8193
(title "JSON POST with 2-way coercion + contexts")
8294

83-
(assert (= {:result 30} (second (call))))
95+
(assert (= {:result 30} (parse (call))))
8496
(cc/bench (call)))
8597

86-
;; 102µs => 78µs (-24%)
98+
;; 85µs => 67µs (-21%)
8799

88100
(let [app (api
89101
(POST* "/echo" []
90102
:return Order
91103
:body [order Order]
92104
(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}]})
105+
data (h/json {:id "123"
106+
:name "Tommi's order"
107+
:description "Totally great order"
108+
:address {:street "Randomstreet 123"
109+
:country "FI"}
110+
:orders [{:name "k1"
111+
:price 123.0
112+
:shipping true}
113+
{:name "k2"
114+
:price 42.0
115+
:shipping false}]})
104116
call #(post* app "/echo" data)]
105117

106118
(title "JSON POST with nested data")
107119

108-
(s/check Order (second (call)))
120+
(s/validate Order (parse (call)))
109121
(cc/bench (call)))
110122

111-
;; 311µs => 194µs (-38%)
123+
;; 266µs => 156µs (-41%)
112124

113125
)
114126

0 commit comments

Comments
 (0)