Skip to content
This repository was archived by the owner on Oct 4, 2020. It is now read-only.

Commit 9e35b46

Browse files
committed
add bench project
1 parent 993e2c3 commit 9e35b46

File tree

5 files changed

+150
-3
lines changed

5 files changed

+150
-3
lines changed

.travis.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ install:
1111
- chmod a+x $HOME/purescript
1212
- npm install -g bower
1313
- npm install
14-
- bower install
14+
- npm run install
1515
script:
1616
- npm run -s build
17+
- npm run -s bench:build
1718
- npm run -s test
1819
after_success:
1920
- >-

bench/bower.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "purescript-eff-aff-bench",
3+
"dependencies": {
4+
"purescript-foldable-traversable": "^3.6.1",
5+
"purescript-minibench": "safareli/purescript-minibench#un-log",
6+
"purescript-eff": "safareli/purescript-eff#fast",
7+
"purescript-aff": "^4.0.1"
8+
},
9+
"resolutions": {
10+
"purescript-eff": "fast"
11+
}
12+
}

bench/src/Bench/Main.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"use strict";
2+
3+
exports.mkArr = function(){
4+
return { count: 0 };
5+
};
6+
7+
exports.pushToArr = function(xs) {
8+
return function(x) {
9+
return function() {
10+
xs.count += 1
11+
return xs;
12+
};
13+
};
14+
};
15+
16+
exports.log = function(x) {
17+
return function(){
18+
console.log(x)
19+
}
20+
};

bench/src/Bench/Main.purs

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+

package.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
{
22
"private": true,
33
"scripts": {
4-
"clean": "rimraf output && rimraf .pulp-cache",
4+
"clean": "rimraf output && rimraf .pulp-cache && rimraf bench/output && rimraf bench/.pulp-cache",
5+
"install": "bower i && cd bench && bower i",
6+
"bench": "cd bench && npm run bench:build && npm run bench:run",
7+
"bench:run": "cd bench && node --expose-gc -e 'require(\"./output/Bench.Main/index.js\").main()'",
8+
"bench:build": "cd bench && pulp build",
59
"test": "pulp test",
610
"build": "eslint src && pulp build -- --censor-lib --strict"
711
},
@@ -11,4 +15,4 @@
1115
"purescript-psa": "^0.5.1",
1216
"rimraf": "^2.6.1"
1317
}
14-
}
18+
}

0 commit comments

Comments
 (0)