-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
sample.clj
100 lines (84 loc) · 2.32 KB
/
sample.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
(ns jmh.sample
"Example fns used by the 'sample.jmh.edn' file.
Benchmark and state fns are annotated with metadata in this file for
clarity. This is not required for functionality."
(:require [clojure.edn :as edn])
(:import [java.io File]
[java.util Arrays Date]
[io.github.jgpc42.jmh.sample Counters]
[org.openjdk.jmh.infra BenchmarkParams Blackhole]))
(defprotocol Api
"An HTTP endpoint."
(fetch [api payload]))
(defprotocol Service
"A mutable lifecycle."
(dispose! [service])
(start! [service])
(stop! [service]))
;;;
(defn ^:bench add
"Simple arithmetic."
(^long [^long n]
(unchecked-add 1 n))
([a b]
(+ a b)))
(defn ^:bench hashcode
"Java hashing."
[^ints arr]
(Arrays/hashCode arr))
(defn ^:bench hasheq
"Clojure hashing."
[^ints arr]
(hash arr))
(defn ^:bench spin
"Sleep for a short period."
[]
(Thread/sleep 1))
(defn ^:bench sum
"Sum a non-empty sequence of numbers."
[& xs]
{:pre [(seq xs)]}
(apply + xs))
(defn ^:bench consume
"Discard arguments via a blackhole."
[^Blackhole hole
cnt
api
[^BenchmarkParams params, edn, ^File temp-file, rand-bytes :as composite]
^Counters counters]
(let [discard (fn [expr]
{:pre [expr]}
(set! (. counters metric) (inc (.metric counters)))
(.consume hole expr))]
(discard (.contains (.getBenchmark params) "consume"))
(discard (some #{(str cnt)} (map #(.getParam params %)
(.getParamsKeys params))))
(discard (= 200 (:status (fetch api nil))))
(discard (or (instance? Date edn) (:magic edn)))
(discard (.exists temp-file))
(discard (pos? (alength rand-bytes)))))
;;;
(defn ^:state random-bytes
"Generate a random byte-array."
^bytes [size]
(let [sample (vec (range -128 128))]
(byte-array (take size (repeatedly #(rand-nth sample))))))
(defn ^:state random-nums
"Generate a random number sequence."
[]
(take 100 (repeatedly #(rand-int Integer/MAX_VALUE))))
(defn ^:state temp-file
"Return a new temporary file."
[]
(File/createTempFile "sample" ".tmp"))
;;;
(defrecord ^:state MockApi []
Api
(fetch [api payload]
{:status 200
:headers {"Content-Type" "text/plain"}
:body "Hello, world."})
Service
(dispose! [api])
(start! [api])
(stop! [api]))