|
| 1 | +module Bench.Main where |
| 2 | + |
| 3 | +import Prelude |
| 4 | + |
| 5 | +import Control.Monad.Ef (Ef) |
| 6 | +import Control.Monad.Ef.Class (liftEf) |
| 7 | +import Control.Monad.Eff (Eff) |
| 8 | +import Control.Monad.Eff.Class (class MonadEff, liftEff) |
| 9 | +import Control.Monad.Eff.Console (CONSOLE) |
| 10 | +import Control.Monad.Eff.Unsafe (unsafePerformEff) |
| 11 | +import Data.Traversable (for_, intercalate) |
| 12 | +import Performance.Minibench (BenchResult, benchWith', withUnits) |
| 13 | + |
| 14 | + |
| 15 | +type BenchEff = (console :: CONSOLE) |
| 16 | + |
| 17 | +testApply :: forall m. MonadEff BenchEff m => Int -> m Unit |
| 18 | +testApply n' = do |
| 19 | + arr <- liftEff mkArr |
| 20 | + applyLoop (void <<< liftEff <<< pushToArr arr) n' |
| 21 | + where |
| 22 | + applyLoop :: Monad m => (Int -> m Unit) -> Int -> m Unit |
| 23 | + applyLoop eff max = go (pure unit) 0 |
| 24 | + where |
| 25 | + go acc n | n == max = acc |
| 26 | + go acc n = go (acc <* eff n) (n + 1) |
| 27 | + |
| 28 | + |
| 29 | +testBindRight :: forall m. MonadEff BenchEff m => Int -> m Unit |
| 30 | +testBindRight n' = do |
| 31 | + arr <- liftEff mkArr |
| 32 | + bindRightLoop (void <<< liftEff <<< pushToArr arr) n' |
| 33 | + where |
| 34 | + bindRightLoop :: Monad m => (Int -> m Unit) -> Int -> m Unit |
| 35 | + bindRightLoop eff max = go (pure unit) 0 |
| 36 | + where |
| 37 | + go acc n | n == max = acc |
| 38 | + go acc n = go (eff (max - n - 1) >>= const acc) (n + 1) |
| 39 | + |
| 40 | + |
| 41 | +testBindLeft :: forall m. MonadEff BenchEff m => Int -> m Unit |
| 42 | +testBindLeft n' = do |
| 43 | + arr <- liftEff mkArr |
| 44 | + bindLeftLoop (void <<< liftEff <<< pushToArr arr) n' |
| 45 | + where |
| 46 | + bindLeftLoop :: Monad m => (Int -> m Unit) -> Int -> m Unit |
| 47 | + bindLeftLoop eff max = go (pure unit) 0 |
| 48 | + where |
| 49 | + go acc n | n == max = acc |
| 50 | + go acc n = go (acc >>= const (eff n)) (n + 1) |
| 51 | + |
| 52 | + |
| 53 | +testMap :: forall m. MonadEff BenchEff m => Int -> m Unit |
| 54 | +testMap n = do |
| 55 | + res <- mapLoop n (pure 0) |
| 56 | + pure unit |
| 57 | + where |
| 58 | + mapLoop :: Monad m => Int -> m Int -> m Int |
| 59 | + mapLoop max i = |
| 60 | + if max == 0 |
| 61 | + then i |
| 62 | + else mapLoop (max - 1) (map (_ + 1) i) |
| 63 | + |
| 64 | + |
| 65 | +main :: Eff BenchEff Unit |
| 66 | +main = do |
| 67 | + log header |
| 68 | + bench "bind assocR" testBindRight testBindRight [100, 500, 1000, 2000, 4000, 8000, 10000] |
| 69 | + bench "bind assocL" testMap testMap [100, 500, 1000, 2000, 4000, 8000] |
| 70 | + bench "map" testMap testMap [100, 500, 1000, 2000, 4000, 5000] |
| 71 | + bench "apply" testApply testApply [100, 500, 1000, 2000, 4000, 5000] |
| 72 | + |
| 73 | +extended :: Eff BenchEff Unit |
| 74 | +extended = do |
| 75 | + log header |
| 76 | + timed ["bind assocR", "Ef", "100000"] $ testBindRight 100000 |
| 77 | + timed ["bind assocR", "Ef", "1000000"] $ testBindRight 1000000 -- ~ 1 sec |
| 78 | +--timed ["bind assocR", "Ef", "10000000"] $ testBindRight 10000000 -- ~ 10 sec |
| 79 | +--timed ["bind assocR", "Ef", "100000000"] $ testBindRight 100000000 -- JavaScript heap out of memory |
| 80 | + timed ["bind assocL", "Ef", "20000"] $ testBindLeft 20000 |
| 81 | + timed ["bind assocL", "Ef", "40000"] $ testBindLeft 40000 |
| 82 | + timed ["bind assocL", "Ef", "80000"] $ testBindLeft 80000 |
| 83 | + timed ["map", "Ef", "10000"] $ testMap 10000 |
| 84 | + timed ["map", "Ef", "20000"] $ testMap 20000 |
| 85 | + timed ["map", "Ef", "40000"] $ testMap 40000 |
| 86 | + timed ["map", "Ef", "80000"] $ testMap 80000 |
| 87 | + timed ["apply", "Ef", "10000"] $ testApply 10000 |
| 88 | + timed ["apply", "Ef", "20000"] $ testApply 20000 |
| 89 | + timed ["apply", "Ef", "40000"] $ testApply 40000 |
| 90 | + timed ["apply", "Ef", "80000"] $ testApply 80000 |
| 91 | + |
| 92 | +header :: String |
| 93 | +header = |
| 94 | + "| bench | type | n | mean | stddev | min | max |\n" <> |
| 95 | + "| ----- | ---- | - | ---- | ------ | --- | --- |" |
| 96 | + |
| 97 | +bench |
| 98 | + :: String |
| 99 | + -> (Int -> Eff BenchEff Unit) |
| 100 | + -> (Int -> Ef BenchEff Unit) |
| 101 | + -> Array Int |
| 102 | + -> Eff BenchEff Unit |
| 103 | +bench name buildEff buildEf vals = for_ vals \val -> do |
| 104 | + logBench [name <> " build", "Eff", show val] $ benchWith' 2000 \_ -> buildEff val |
| 105 | + logBench' [name <> " build", "Ef", show val] $ benchWith' 2000 \_ -> buildEf val |
| 106 | + let eff = liftEff $ buildEff val |
| 107 | + logBench [name <> " run", "Eff", show val] $ benchWith' 2000 \_ -> unsafePerformEff eff |
| 108 | + let ef = liftEf $ buildEf val |
| 109 | + logBench' [name <> " run", "Ef", show val] $ benchWith' 2000 \_ -> unsafePerformEff ef |
| 110 | + |
| 111 | + |
| 112 | +timed :: Array String -> Ef BenchEff Unit -> Eff BenchEff Unit |
| 113 | +timed msg eff = |
| 114 | + logBench' msg $ benchWith' 5 \_ -> unsafePerformEff $ liftEf eff |
| 115 | + |
| 116 | +logBench'' :: (String -> String) -> Array String -> Eff BenchEff BenchResult -> Eff BenchEff Unit |
| 117 | +logBench'' f msg benchEff = do |
| 118 | + res <- benchEff |
| 119 | + let |
| 120 | + logStr = intercalate " | " |
| 121 | + $ map f |
| 122 | + $ append msg |
| 123 | + $ map withUnits [res.mean, res.stdDev, res.min, res.max] |
| 124 | + log $ "| " <> logStr <> " |" |
| 125 | + |
| 126 | +logBench :: Array String -> Eff BenchEff BenchResult -> Eff BenchEff Unit |
| 127 | +logBench = logBench'' id |
| 128 | + |
| 129 | +logBench' :: Array String -> Eff BenchEff BenchResult -> Eff BenchEff Unit |
| 130 | +logBench' = logBench'' \s -> "**" <> s <> "**" |
| 131 | + |
| 132 | +foreign import data Arr :: Type -> Type |
| 133 | + |
| 134 | +foreign import mkArr :: forall e a. Eff e (Arr a) |
| 135 | +foreign import pushToArr :: forall e a. Arr a -> a -> Eff e a |
| 136 | +foreign import log :: forall e a. a -> Eff e Unit |
| 137 | + |
0 commit comments