|
| 1 | +module Bench.Main where |
| 2 | + |
| 3 | +import Prelude |
| 4 | + |
| 5 | +import Control.Monad.Eff (Eff) |
| 6 | +import Control.Monad.Aff (Aff, launchAff_) |
| 7 | +import Control.Monad.Eff.Class (class MonadEff, liftEff) |
| 8 | +import Control.Monad.Eff.Console (CONSOLE) |
| 9 | +import Control.Monad.Eff.Unsafe (unsafePerformEff) |
| 10 | +import Data.Traversable (for_, intercalate) |
| 11 | +import Performance.Minibench (BenchResult, benchWith', withUnits) |
| 12 | + |
| 13 | + |
| 14 | +type BenchEff = (console :: CONSOLE) |
| 15 | + |
| 16 | +testApply :: forall m. MonadEff BenchEff m => Int -> m Unit |
| 17 | +testApply n' = do |
| 18 | + arr <- liftEff mkArr |
| 19 | + applyLoop (void <<< liftEff <<< pushToArr arr) n' |
| 20 | + where |
| 21 | + applyLoop :: Monad m => (Int -> m Unit) -> Int -> m Unit |
| 22 | + applyLoop eff max = go (pure unit) 0 |
| 23 | + where |
| 24 | + go acc n | n == max = acc |
| 25 | + go acc n = go (acc <* eff n) (n + 1) |
| 26 | + |
| 27 | + |
| 28 | +testBindRight :: forall m. MonadEff BenchEff m => Int -> m Unit |
| 29 | +testBindRight n' = do |
| 30 | + arr <- liftEff mkArr |
| 31 | + bindRightLoop (void <<< liftEff <<< pushToArr arr) n' |
| 32 | + where |
| 33 | + bindRightLoop :: Monad m => (Int -> m Unit) -> Int -> m Unit |
| 34 | + bindRightLoop eff max = go (pure unit) 0 |
| 35 | + where |
| 36 | + go acc n | n == max = acc |
| 37 | + go acc n = go (eff (max - n - 1) >>= const acc) (n + 1) |
| 38 | + |
| 39 | + |
| 40 | +testBindLeft :: forall m. MonadEff BenchEff m => Int -> m Unit |
| 41 | +testBindLeft n' = do |
| 42 | + arr <- liftEff mkArr |
| 43 | + bindLeftLoop (void <<< liftEff <<< pushToArr arr) n' |
| 44 | + where |
| 45 | + bindLeftLoop :: Monad m => (Int -> m Unit) -> Int -> m Unit |
| 46 | + bindLeftLoop eff max = go (pure unit) 0 |
| 47 | + where |
| 48 | + go acc n | n == max = acc |
| 49 | + go acc n = go (acc >>= const (eff n)) (n + 1) |
| 50 | + |
| 51 | + |
| 52 | +testMap :: forall m. MonadEff BenchEff m => Int -> m Unit |
| 53 | +testMap n = do |
| 54 | + arr <- liftEff mkArr |
| 55 | + res <- mapLoop n (liftEff $ pushToArr arr 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 "| bench | type | n | mean | stddev | min | max |" |
| 68 | + log "| ----- | ---- | - | ---- | ------ | --- | --- |" |
| 69 | + bench 1000 ">>=R" testBindRight testBindRight [100, 1000, 5000] |
| 70 | + bench 1000 ">>=L" testBindLeft testBindLeft [100, 1000, 5000] |
| 71 | + bench 1000 "map" testMap testMap [100, 1000, 5000] |
| 72 | + bench 1000 "apply" testApply testApply [100, 1000, 5000] |
| 73 | + log "| - | - | - | - | - | - | - |" |
| 74 | + bench 5 ">>=R" testBindRight testBindRight [10000, 50000, 100000, 1000000] |
| 75 | + bench 5 ">>=L" testBindLeft testBindLeft [10000, 50000, 100000, 1000000] |
| 76 | + bench 5 "map" testMap testMap [10000, 50000, 100000, 1000000, 350000, 700000] |
| 77 | + bench 5 "apply" testApply testApply [10000, 50000, 100000, 1000000] |
| 78 | + |
| 79 | +bench |
| 80 | + :: Int |
| 81 | + -> String |
| 82 | + -> (Int -> Eff BenchEff Unit) |
| 83 | + -> (Int -> Aff BenchEff Unit) |
| 84 | + -> Array Int |
| 85 | + -> Eff BenchEff Unit |
| 86 | +bench n name buildEff buildAff vals = for_ vals \val -> do |
| 87 | + logBench [name <> " build", "Eff", show val] $ benchWith' n \_ -> buildEff val |
| 88 | + logBench' id [name <> " build", "Aff", show val] $ benchWith' n \_ -> buildAff val |
| 89 | + let eff = liftEff $ buildEff val |
| 90 | + logBench [name <> " run", "Eff", show val] $ benchWith' n \_ -> unsafePerformEff eff |
| 91 | + let aff = launchAff_ $ buildAff val |
| 92 | + logBench' id [name <> " run", "Aff", show val] $ benchWith' n \_ -> unsafePerformEff aff |
| 93 | + |
| 94 | +logBench' :: (String -> String) -> Array String -> Eff BenchEff BenchResult -> Eff BenchEff Unit |
| 95 | +logBench' f msg benchEff = do |
| 96 | + res <- benchEff |
| 97 | + let |
| 98 | + logStr = intercalate " | " |
| 99 | + $ append msg |
| 100 | + $ map (f <<< withUnits) [res.mean, res.stdDev, res.min, res.max] |
| 101 | + log $ "| " <> logStr <> " |" |
| 102 | + |
| 103 | +logBench :: Array String -> Eff BenchEff BenchResult -> Eff BenchEff Unit |
| 104 | +logBench = logBench' \s -> "**" <> s <> "**" |
| 105 | + |
| 106 | +foreign import data Arr :: Type -> Type |
| 107 | +foreign import mkArr :: forall e a. Eff e (Arr a) |
| 108 | +foreign import pushToArr :: forall e a. Arr a -> a -> Eff e a |
| 109 | +foreign import log :: forall e a. a -> Eff e Unit |
| 110 | + |
0 commit comments