Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: code to help measure performance #112

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: code to measure performance
  • Loading branch information
erdos committed Feb 7, 2022
commit 399cbaf6efbb2360da73d2214d65d55185fa99ea
34 changes: 34 additions & 0 deletions src/stencil/perf.clj
Original file line number Diff line number Diff line change
@@ -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))))