From 399cbaf6efbb2360da73d2214d65d55185fa99ea Mon Sep 17 00:00:00 2001 From: erdos Date: Mon, 7 Feb 2022 21:55:07 +0100 Subject: [PATCH 1/2] feat: code to measure performance --- src/stencil/perf.clj | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/stencil/perf.clj diff --git a/src/stencil/perf.clj b/src/stencil/perf.clj new file mode 100644 index 00000000..08a55303 --- /dev/null +++ b/src/stencil/perf.clj @@ -0,0 +1,34 @@ +(ns stencil.perf + (:require [stencil.api :as api])) + +(set! *warn-on-reflection* true) +(set! *unchecked-math* :warn-on-boxed) + +(defn- stats [numbers] + (let [n (count numbers) + mean (double (/ (double (reduce + 0.0 numbers)) n)) + stdev (Math/sqrt (/ ^double (reduce + 0.0 (for [a numbers] (Math/pow (- mean ^double a) 2))) (dec n)))] + [mean stdev])) + +(defn- measured [f] + (let [before (System/nanoTime)] + (f) + (quot (double (- (System/nanoTime) before)) 1000000))) + +(defn stats-of [n f] (stats (repeatedly n #(measured f)))) + +(defn black-hole [] + (proxy [java.io.FilterOutputStream] [nil] + (flush []) (close []) (write ([_ _ _] nil) ([_] nil)))) + +(def data {"value" "hello world 99"}) + +(defn main [n template] + (let [n (Long/parseLong (str n)) + [mean stdev] (stats-of n (partial api/prepare template))] + (println "Ran" n "times:") + (println :mean mean :stdev stdev) + (let [prepared (api/prepare template) + [mean stdev] (stats-of n #(api/render! template data :output (black-hole)))] + (println "Rendering:") + (println :mean mean :stdev stdev)))) From 88a9eab6b6c664384bf4ef0bb03af5deb0ecb9f6 Mon Sep 17 00:00:00 2001 From: erdos Date: Tue, 8 Feb 2022 23:37:02 +0100 Subject: [PATCH 2/2] wip --- src/stencil/perf.clj | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/stencil/perf.clj b/src/stencil/perf.clj index 08a55303..f86a7ed9 100644 --- a/src/stencil/perf.clj +++ b/src/stencil/perf.clj @@ -24,11 +24,12 @@ (def data {"value" "hello world 99"}) (defn main [n template] + (println "Start" n "measurements on" template) (let [n (Long/parseLong (str n)) [mean stdev] (stats-of n (partial api/prepare template))] (println "Ran" n "times:") (println :mean mean :stdev stdev) (let [prepared (api/prepare template) - [mean stdev] (stats-of n #(api/render! template data :output (black-hole)))] + [mean stdev] (stats-of n #(api/render! prepared data :output (black-hole)))] (println "Rendering:") (println :mean mean :stdev stdev))))