Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
/.psc*
/.purs*
/.psa*
package-lock.json
15 changes: 14 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,22 @@ language: node_js
dist: trusty
sudo: required
node_js: stable
env:
- PATH=$HOME/purescript:$PATH
install:
- TAG=$(wget -q -O - https://github.com/purescript/purescript/releases/latest --server-response --max-redirect 0 2>&1 | sed -n -e 's/.*Location:.*tag\///p')
- wget -O $HOME/purescript.tar.gz https://github.com/purescript/purescript/releases/download/$TAG/linux64.tar.gz
- tar -xvf $HOME/purescript.tar.gz -C $HOME/
- chmod a+x $HOME/purescript
- npm install -g bower
- npm install
- bower install
- bower install --production
script:
- npm run -s build
- bower install
- npm run -s test
after_success:
- >-
test $TRAVIS_TAG &&
echo $GITHUB_TOKEN | pulp login &&
echo y | pulp publish --no-push
38 changes: 22 additions & 16 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
The MIT License (MIT)
Copyright 2018 PureScript

Copyright (c) 2017 Phil Freeman
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 changes: 19 additions & 12 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
{
"name": "purescript-minibench",
"homepage": "https://github.com/purescript/purescript-minibench",
"license": "BSD-3-Clause",
"repository": {
"type": "git",
"url": "git://github.com/purescript/purescript-minibench.git"
},
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"output"
"node_modules",
"output",
"test",
"bower.json",
"package.json"
],
"license": "MIT",
"repository": {
"type": "git",
"url": "git://github.com/paf31/purescript-minibench.git"
},
"dependencies": {
"purescript-prelude": "^3.0.0",
"purescript-console": "^3.0.0",
"purescript-integers": "^3.0.0",
"purescript-st": "^3.0.0",
"purescript-partial": "^1.2.0"
"purescript-console": "^4.0.0",
"purescript-effect": "^2.0.0",
"purescript-globals": "^4.0.0",
"purescript-integers": "^4.0.0",
"purescript-math": "^2.1.1",
"purescript-partial": "^2.0.0",
"purescript-prelude": "^4.0.0",
"purescript-refs": "^4.0.0"
}
}
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
"private": true,
"scripts": {
"clean": "rimraf output && rimraf .pulp-cache",
"build": "pulp build && pulp test"
"build": "pulp build -- --censor-lib --strict",
"test": "pulp test"
},
"devDependencies": {
"pulp": "^11.0.0",
"purescript-psa": "^0.5.0",
"purescript": "^0.11.1",
"rimraf": "^2.5.4"
"pulp": "^12.2.0",
"purescript-psa": "^0.6.0",
"rimraf": "^2.6.2"
}
}
86 changes: 54 additions & 32 deletions src/Performance/Minibench.purs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,28 @@
module Performance.Minibench
( bench
, benchWith
, benchWith'
, BenchResult
, withUnits
) where

import Control.Monad.Eff (Eff, forE)
import Control.Monad.Eff.Console (CONSOLE, log)
import Control.Monad.Eff.Uncurried (EffFn1, runEffFn1)
import Control.Monad.ST (modifySTRef, newSTRef, readSTRef, runST)
import Prelude hiding (min,max)

import Data.Int (toNumber)
import Effect (Effect, forE)
import Effect.Console (log)
import Effect.Ref as Ref
import Effect.Uncurried (EffectFn1, runEffectFn1)
import Global (infinity)
import Math (max, min, sqrt)
import Partial.Unsafe (unsafeCrashWith)
import Prelude hiding (min,max)

-- | A wrapper around the Node `process.hrtime()` function.
foreign import hrTime :: forall eff. EffFn1 eff (Array Int) (Array Int)
foreign import hrTime :: EffectFn1 (Array Int) (Array Int)

-- | Force garbage collection.
-- | Requires node to be run with the --force-gc flag.
foreign import gc :: forall eff. Eff eff Unit
foreign import gc :: Effect Unit

foreign import toFixed :: Number -> String

Expand All @@ -46,37 +50,58 @@ withUnits t
-- | To increase benchmark accuracy by forcing garbage collection before the
-- | benchmark is run, node should be invoked with the '--expose-gc' flag.
benchWith
:: forall eff a
:: forall a
. Int
-> (Unit -> a)
-> Effect Unit
benchWith n f = do
res <- benchWith' n f
log ("mean = " <> withUnits res.mean)
log ("stddev = " <> withUnits res.stdDev)
log ("min = " <> withUnits res.min)
log ("max = " <> withUnits res.max)

type BenchResult =
{ mean :: Number
, stdDev :: Number
, min :: Number
, max :: Number
}

benchWith'
:: forall a
. Int
-> (Unit -> a)
-> Eff (console :: CONSOLE | eff) Unit
benchWith n f = runST do
sumRef <- newSTRef 0.0
sum2Ref <- newSTRef 0.0
minRef <- newSTRef infinity
maxRef <- newSTRef 0.0
-> Effect BenchResult
benchWith' n f = do
sumRef <- Ref.new 0.0
sum2Ref <- Ref.new 0.0
minRef <- Ref.new infinity
maxRef <- Ref.new 0.0
gc
forE 0 n \_ -> do
t1 <- runEffFn1 hrTime [0, 0]
t2 <- const (runEffFn1 hrTime t1) (f unit)
t1 <- runEffectFn1 hrTime [0, 0]
t2 <- const (runEffectFn1 hrTime t1) (f unit)
let ns = fromHrTime t2
square = ns * ns
_ <- modifySTRef sumRef (_ + ns)
_ <- modifySTRef sum2Ref (_ + square)
_ <- modifySTRef minRef (_ `min` ns)
_ <- modifySTRef maxRef (_ `max` ns)
_ <- Ref.modify (_ + ns) sumRef
_ <- Ref.modify (_ + square) sum2Ref
_ <- Ref.modify (_ `min` ns) minRef
_ <- Ref.modify (_ `max` ns) maxRef
pure unit
sum <- readSTRef sumRef
sum2 <- readSTRef sum2Ref
min' <- readSTRef minRef
max' <- readSTRef maxRef
sum <- Ref.read sumRef
sum2 <- Ref.read sum2Ref
min' <- Ref.read minRef
max' <- Ref.read maxRef
let n' = toNumber n
mean = sum / n'
stdDev = sqrt ((sum2 - n' * mean * mean) / (n' - 1.0))
log ("mean = " <> withUnits mean)
log ("stddev = " <> withUnits stdDev)
log ("min = " <> withUnits min')
log ("max = " <> withUnits max')
pure
{ mean
, stdDev
, min: min'
, max: max'
}

-- | Estimate the running time of a function and print a summary to the console,
-- | by running the function 1000 times.
Expand All @@ -92,8 +117,5 @@ benchWith n f = runST do
-- | mean = 414.00 μs
-- | stddev = 494.82 μs
-- | ```
bench
:: forall eff a
. (Unit -> a)
-> Eff (console :: CONSOLE | eff) Unit
bench :: forall a. (Unit -> a) -> Effect Unit
bench = benchWith 1000
7 changes: 4 additions & 3 deletions test/Main.purs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
module Test.Main where

import Prelude
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, log)

import Effect (Effect)
import Effect.Console (log)
import Performance.Minibench (bench, benchWith)

main :: forall e. Eff (console :: CONSOLE | e) Unit
main :: Effect Unit
main = do
let loop 0 = 0
loop n = loop (n - 1)
Expand Down