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