Skip to content

Commit 46238fb

Browse files
committed
Push overhead estimation into benchmark*
This restores the ability to simply use benchmark and quick-benchmark to obtain raw benchmark results data. Closes #23
1 parent 75b17fe commit 46238fb

File tree

1 file changed

+35
-30
lines changed

1 file changed

+35
-30
lines changed

src/criterium/core.clj

+35-30
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,36 @@ See http://www.ellipticgroup.com/misc/article_supplement.pdf, p17."
699699
(outlier-count 0 0 0 0)
700700
data))
701701

702+
;;; overhead estimation
703+
(declare benchmark*)
704+
705+
(defn estimate-overhead
706+
"Calculate a conservative estimate of the timing loop overhead."
707+
[]
708+
(-> (benchmark*
709+
(fn [] 0)
710+
{:warmup-jit-period (* 10 s-to-ns)
711+
:samples 10
712+
:target-execution-time (* 0.5 s-to-ns)
713+
:overhead 0
714+
:supress-jvm-option-warnings true})
715+
:lower-q
716+
first))
717+
718+
(def estimatated-overhead-cache nil)
719+
720+
(defn estimatated-overhead!
721+
"Sets the estimated overhead."
722+
[]
723+
(progress "Estimating sampling overhead")
724+
(alter-var-root
725+
#'estimatated-overhead-cache (constantly (estimate-overhead))))
726+
727+
(defn estimatated-overhead
728+
[]
729+
(or estimatated-overhead-cache
730+
(estimatated-overhead!)))
731+
702732
;;; options
703733
(defn extract-report-options
704734
"Extract reporting options from the given options vector. Returns a two
@@ -779,7 +809,6 @@ See http://www.ellipticgroup.com/misc/article_supplement.pdf, p17."
779809
"and may lead to unexpected results as JIT C2 compiler may not be active."
780810
"See http://www.slideshare.net/CharlesNutter/javaone-2012-jvm-jit-for-dummies."))))
781811

782-
783812
(defn benchmark*
784813
"Benchmark a function. This tries its best to eliminate sources of error.
785814
This also means that it runs for a while. It will typically take 70s for a
@@ -791,7 +820,9 @@ See http://www.ellipticgroup.com/misc/article_supplement.pdf, p17."
791820
(warn-on-suspicious-jvm-options))
792821
(let [{:keys [samples warmup-jit-period target-execution-time
793822
gc-before-sample overhead] :as opts}
794-
(merge *default-benchmark-opts* options)
823+
(merge *default-benchmark-opts*
824+
{:overhead (or overhead (estimatated-overhead))}
825+
options)
795826
times (run-benchmark
796827
samples warmup-jit-period target-execution-time f opts overhead)]
797828
(benchmark-stats times opts)))
@@ -954,30 +985,6 @@ See http://www.ellipticgroup.com/misc/article_supplement.pdf, p17."
954985
(report-point-estimate "Overhead used" [overhead])))
955986
(report-outliers results)))
956987

957-
(defn estimate-overhead
958-
"Calculate a conservative estimate of the timing loop overhead."
959-
[]
960-
(-> (benchmark 0 {:warmup-jit-period (* 10 s-to-ns)
961-
:samples 10
962-
:target-execution-time (* 0.5 s-to-ns)
963-
:overhead 0})
964-
:lower-q
965-
first))
966-
967-
(def estimatated-overhead-cache nil)
968-
969-
(defn estimatated-overhead!
970-
"Sets the estimated overhead."
971-
[]
972-
(progress "Estimating sampling overhead")
973-
(alter-var-root
974-
#'estimatated-overhead-cache (constantly (estimate-overhead))))
975-
976-
(defn estimatated-overhead
977-
[]
978-
(or estimatated-overhead-cache
979-
(estimatated-overhead!)))
980-
981988
(defmacro bench
982989
"Convenience macro for benchmarking an expression, expr. Results are reported
983990
to *out* in human readable format. Options for report format are: :os,
@@ -987,8 +994,7 @@ See http://www.ellipticgroup.com/misc/article_supplement.pdf, p17."
987994
`(report-result
988995
(benchmark
989996
~expr
990-
(merge {:overhead (estimatated-overhead)}
991-
~(when (seq options) (apply hash-map options))))
997+
~(when (seq options) (apply hash-map options)))
992998
~@report-options)))
993999

9941000
(defmacro quick-bench
@@ -1000,6 +1006,5 @@ to *out* in human readable format. Options for report format are: :os,
10001006
`(report-result
10011007
(quick-benchmark
10021008
~expr
1003-
(merge {:overhead (estimatated-overhead)}
1004-
~(when (seq options) (apply hash-map options))))
1009+
~(when (seq options) (apply hash-map options)))
10051010
~@report-options)))

0 commit comments

Comments
 (0)