on 267f708 help sign in
elk/bench/src/Benchmark.elm
121 lines 2.1 kB history raw
1module Benchmark
2 ( Benchmark
3 , test
4 , suite
5 , run
6 , Statistics
7 , view
8 )where
9{-| A wrapper around benchmark.js that allows benchmarking of pure functions.
11# Creating Benchmarks
12@docs Benchmark, test, suite
14# Run benchmarks
15@docs run, Statistics
16-}
19import Native.Benchmark
20import Native.BenchmarkJS
21import String
22import Task
23import Html exposing (Html, div, ul, li, p, text)
24import Html.Attributes exposing (style)
28-- SET UP BENCHMARK SUITE
31{-| A set of benchmarks. -}
32type Benchmark
33 = Test String Thunk
34 | Suite String (List Benchmark)
37type Thunk = Thunk
40{-| An individual benchmark with a name. It measures how long it takes to
41evaluate the given function when it is passed a `()` value.
42-}
43test : String -> (() -> result) -> Benchmark
44test name thunk =
45 Test name (Native.Benchmark.makeThunk thunk)
48{-| A collection of benchmarks to run together.
49-}
50suite : String -> List Benchmark -> Benchmark
51suite =
52 Suite
56-- RUN THE BENCHMARKS
59{-| Statistics -}
60type Statistics
61 = StatSuite String (List Statistics)
62 | Result String
63 { mean : Float
64 , hz : Float
65 }
68{-| Run a benchmark.
69-}
70run : Benchmark -> Task.Task x Statistics
71run =
72 Native.Benchmark.run
76-- TO HTML
79view : Statistics -> Html
80view statistics =
81 div [ style [] ]
82 [ viewHelp statistics
83 ]
86viewHelp : Statistics -> Html
87viewHelp statistics =
88 case statistics of
89 Result name {hz} ->
90 text (name ++ " - " ++ prettyFloat hz ++ " ops/sec")
92 StatSuite name subStats ->
93 let
94 subResults =
95 List.map viewHelp subStats
96 in
97 p []
98 [ text name
99 , ul [] (List.map (\html -> li [] [html]) subResults)
100 ]
103prettyFloat : Float -> String
104prettyFloat float =
105 String.join "," (numberChunker (round float) [])
108numberChunker : Int -> List String -> List String
109numberChunker n chunks =
110 let
111 next =
112 n // 1000
114 chunk =
115 n % 1000
116 in
117 if next == 0 then
118 toString chunk :: chunks
120 else
121 numberChunker next (String.padLeft 3 '0' (toString chunk) :: chunks)