11(ns aux
2- (:require [clojure.string :as str])
3- (:import [org.slf4j LoggerFactory]))
2+ (:require [clojure.core.async :as async]
3+ [clojure.string :as str])
4+ (:import [java.io ByteArrayInputStream ByteArrayOutputStream]
5+ [org.fressian FressianReader FressianWriter]
6+ [org.slf4j LoggerFactory]))
47
58(defn get-fressian-version []
69 (let [libs (-> (System/getProperty " java.class.path" )
1215 (re-find #".*fressian-(.*)\. jar" )
1316 (second ))))
1417
15-
16-
1718(defn- log [level msg]
1819 `(let [logger# (LoggerFactory/getLogger " fressian" )]
1920 (. logger# ~level (str ~msg))))
2829 (log 'warn msg))
2930
3031(defmacro error [msg]
31- (log 'error msg))
32+ (log 'error msg))
33+
34+ (defn gen-vec [size]
35+ (into [] (for [_ (range size)]
36+ (str (random-uuid )))))
37+
38+ (defn gen-map [size]
39+ (into {} (for [_ (range size)]
40+ {(str (random-uuid )) (str (random-uuid ))})))
41+
42+ (defn fressian [data]
43+ (with-open [baos (ByteArrayOutputStream. )
44+ fw (FressianWriter. baos)]
45+ (.writeObject fw data)
46+ (.toByteArray baos)))
47+
48+ (defn defressian [data & [handlers]]
49+ (with-open [bais (ByteArrayInputStream. data)
50+ fr (FressianReader. bais handlers)]
51+ (.readObject fr)))
52+
53+ (defmacro measure-duration [& body]
54+ `(let [st# (System/nanoTime )]
55+ ~@body
56+ (- (System/nanoTime ) st#)))
57+
58+ (defn stress-test [{:keys [struct-type struct-size convert-list?] :as argmap}
59+ read-handlers]
60+ (let [gen-struct-fn (case (str struct-type)
61+ " map" gen-map
62+ " vec" gen-vec)
63+ fr-handlers (when convert-list? read-handlers)]
64+ (info (merge {:event :starting-stress-test } argmap))
65+ (let [work-chan (async/chan 1 )
66+ timeout-chan (async/timeout (* 1000 60 20 ))
67+ ; avoid submillisecond measurement
68+ struct-count (* 1000 (if (< struct-size 9 )
69+ 100 1 ))]
70+ (loop [runs 0
71+ proc-time 0 ]
72+ (let [structs (repeatedly struct-count #(gen-struct-fn struct-size))
73+ ; put generated structs fressianed into the work channel as a single coll
74+ _ (async/put! work-chan (into [] (for [st structs] (fressian st))))
75+ [val port] (async/alts!! [work-chan timeout-chan])]
76+ (cond
77+ (= port timeout-chan)
78+ (info (merge {:event :stress-test-complete :runs runs :processing-time proc-time} argmap))
79+
80+ (= port work-chan)
81+ ; measure time to read all structs in work chan
82+ (let [dur (measure-duration (doseq [fs val]
83+ (defressian fs fr-handlers )))]
84+ (recur (inc runs) (+ proc-time dur)))))))))
0 commit comments