aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/test
diff options
context:
space:
mode:
authorEduardo Julian2022-12-01 20:03:21 -0400
committerEduardo Julian2022-12-01 20:03:21 -0400
commitb491dfff00219d5206075ea65468e00ab657075d (patch)
treec026c09cb919fafdd714e8aa5a9b2c1607a299da /stdlib/source/library/lux/test
parentc7f67a85f980db2dab2e2d7df4168af83e9013a8 (diff)
Added simple benchmarking machinery.
Diffstat (limited to 'stdlib/source/library/lux/test')
-rw-r--r--stdlib/source/library/lux/test/benchmark.lux54
1 files changed, 54 insertions, 0 deletions
diff --git a/stdlib/source/library/lux/test/benchmark.lux b/stdlib/source/library/lux/test/benchmark.lux
new file mode 100644
index 000000000..427e62822
--- /dev/null
+++ b/stdlib/source/library/lux/test/benchmark.lux
@@ -0,0 +1,54 @@
+(.require
+ [library
+ [lux (.except)
+ [abstract
+ ["[0]" monad (.only do)]
+ ["[0]" order]]
+ [control
+ ["[0]" io (.only IO) (.use "[1]#[0]" monad)]]
+ [data
+ [collection
+ ["[0]" list (.use "[1]#[0]" mix)]]]
+ [world
+ [time
+ ["[0]" instant]
+ ["[0]" duration (.only Duration)]]]]])
+
+(def .public (time subject)
+ (-> (IO Any)
+ (IO Duration))
+ (do io.monad
+ [before instant.now
+ _ subject
+ after instant.now]
+ (in (instant.span before after))))
+
+(type .public Benchmark
+ (Record
+ [#times Nat
+ #minimum Duration
+ #maximum Duration
+ #average Duration]))
+
+(def empty
+ Benchmark
+ [#times 0
+ #minimum duration.empty
+ #maximum duration.empty
+ #average duration.empty])
+
+(def .public (test times subject)
+ (-> Nat (IO Any)
+ (IO Benchmark))
+ (when times
+ 0 (io#in ..empty)
+ _ (do [! io.monad]
+ [durations (|> subject
+ (list.repeated times)
+ (monad.each ! ..time))]
+ (in [#times times
+ #minimum (list#mix (order.min duration.order) duration.empty durations)
+ #maximum (list#mix (order.max duration.order) duration.empty durations)
+ #average (|> durations
+ (list#mix duration.composite duration.empty)
+ (duration.down times))]))))