2015-10-09 18:27:17 +03:00
|
|
|
-- Copyright (c) 2014-present, Facebook, Inc.
|
|
|
|
-- All rights reserved.
|
|
|
|
--
|
|
|
|
-- This source code is distributed under the terms of a BSD license,
|
2017-12-08 15:26:13 +03:00
|
|
|
-- found in the LICENSE file.
|
2015-10-09 18:27:17 +03:00
|
|
|
|
2016-06-20 12:37:33 +03:00
|
|
|
-- | Benchmarking tool for core performance characteristics of the Haxl monad.
|
2016-10-05 15:36:22 +03:00
|
|
|
{-# LANGUAGE CPP #-}
|
Add a benchmark for basic Haxl monad patterns
Summary:
I'm using this to test variants of the monad. In particular, the
current monad displays O(n^2) performance with the seql version of
this benchmark. This is a well-studied problem, see for example
"Reflection without remorse: revealing a hidden sequence to speed up
monadic reflection" (van der Ploeg / Kiselyov, Haskell '14)
Test Plan:
Built it and ran it a few times
unit tests still work
Reviewed By: bnitka@fb.com
Subscribers: ldbrandy, kjm, jlengyel, memo, watashi, smarlow, akr, bnitka, jcoens
FB internal diff: D2419419
Signature: t1:2419419:1441640727:9f5f82212c829fcbf2e8c063d4dbd0db495b0ba2
2015-09-07 19:00:27 +03:00
|
|
|
module MonadBench (main) where
|
|
|
|
|
|
|
|
import Control.Monad
|
2016-06-20 12:37:33 +03:00
|
|
|
import Data.List as List
|
Add a benchmark for basic Haxl monad patterns
Summary:
I'm using this to test variants of the monad. In particular, the
current monad displays O(n^2) performance with the seql version of
this benchmark. This is a well-studied problem, see for example
"Reflection without remorse: revealing a hidden sequence to speed up
monadic reflection" (van der Ploeg / Kiselyov, Haskell '14)
Test Plan:
Built it and ran it a few times
unit tests still work
Reviewed By: bnitka@fb.com
Subscribers: ldbrandy, kjm, jlengyel, memo, watashi, smarlow, akr, bnitka, jcoens
FB internal diff: D2419419
Signature: t1:2419419:1441640727:9f5f82212c829fcbf2e8c063d4dbd0db495b0ba2
2015-09-07 19:00:27 +03:00
|
|
|
import Data.Time.Clock
|
|
|
|
import System.Environment
|
|
|
|
import System.Exit
|
|
|
|
import System.IO
|
|
|
|
import Text.Printf
|
|
|
|
|
|
|
|
import Haxl.Prelude as Haxl
|
|
|
|
import Prelude()
|
|
|
|
|
2017-10-27 13:31:57 +03:00
|
|
|
import Haxl.Core.Memo (newMemoWith, runMemo)
|
2016-07-25 16:13:14 +03:00
|
|
|
|
Add a benchmark for basic Haxl monad patterns
Summary:
I'm using this to test variants of the monad. In particular, the
current monad displays O(n^2) performance with the seql version of
this benchmark. This is a well-studied problem, see for example
"Reflection without remorse: revealing a hidden sequence to speed up
monadic reflection" (van der Ploeg / Kiselyov, Haskell '14)
Test Plan:
Built it and ran it a few times
unit tests still work
Reviewed By: bnitka@fb.com
Subscribers: ldbrandy, kjm, jlengyel, memo, watashi, smarlow, akr, bnitka, jcoens
FB internal diff: D2419419
Signature: t1:2419419:1441640727:9f5f82212c829fcbf2e8c063d4dbd0db495b0ba2
2015-09-07 19:00:27 +03:00
|
|
|
import Haxl.Core
|
|
|
|
|
|
|
|
import ExampleDataSource
|
|
|
|
|
2019-04-10 19:45:58 +03:00
|
|
|
testEnv :: IO (Env () ())
|
Add a benchmark for basic Haxl monad patterns
Summary:
I'm using this to test variants of the monad. In particular, the
current monad displays O(n^2) performance with the seql version of
this benchmark. This is a well-studied problem, see for example
"Reflection without remorse: revealing a hidden sequence to speed up
monadic reflection" (van der Ploeg / Kiselyov, Haskell '14)
Test Plan:
Built it and ran it a few times
unit tests still work
Reviewed By: bnitka@fb.com
Subscribers: ldbrandy, kjm, jlengyel, memo, watashi, smarlow, akr, bnitka, jcoens
FB internal diff: D2419419
Signature: t1:2419419:1441640727:9f5f82212c829fcbf2e8c063d4dbd0db495b0ba2
2015-09-07 19:00:27 +03:00
|
|
|
testEnv = do
|
|
|
|
exstate <- ExampleDataSource.initGlobalState
|
|
|
|
let st = stateSet exstate stateEmpty
|
|
|
|
initEnv st ()
|
|
|
|
|
2016-10-05 15:36:22 +03:00
|
|
|
main :: IO ()
|
Add a benchmark for basic Haxl monad patterns
Summary:
I'm using this to test variants of the monad. In particular, the
current monad displays O(n^2) performance with the seql version of
this benchmark. This is a well-studied problem, see for example
"Reflection without remorse: revealing a hidden sequence to speed up
monadic reflection" (van der Ploeg / Kiselyov, Haskell '14)
Test Plan:
Built it and ran it a few times
unit tests still work
Reviewed By: bnitka@fb.com
Subscribers: ldbrandy, kjm, jlengyel, memo, watashi, smarlow, akr, bnitka, jcoens
FB internal diff: D2419419
Signature: t1:2419419:1441640727:9f5f82212c829fcbf2e8c063d4dbd0db495b0ba2
2015-09-07 19:00:27 +03:00
|
|
|
main = do
|
|
|
|
[test,n_] <- getArgs
|
|
|
|
let n = read n_
|
|
|
|
env <- testEnv
|
|
|
|
t0 <- getCurrentTime
|
|
|
|
case test of
|
|
|
|
-- parallel, identical queries
|
|
|
|
"par1" -> runHaxl env $
|
|
|
|
Haxl.sequence_ (replicate n (listWombats 3))
|
|
|
|
-- parallel, distinct queries
|
|
|
|
"par2" -> runHaxl env $
|
|
|
|
Haxl.sequence_ (map listWombats [1..fromIntegral n])
|
|
|
|
-- sequential, identical queries
|
|
|
|
"seqr" -> runHaxl env $
|
|
|
|
foldr andThen (return ()) (replicate n (listWombats 3))
|
|
|
|
-- sequential, left-associated, distinct queries
|
|
|
|
"seql" -> runHaxl env $ do
|
2016-10-05 15:36:22 +03:00
|
|
|
_ <- foldl andThen (return []) (map listWombats [1.. fromIntegral n])
|
Add a benchmark for basic Haxl monad patterns
Summary:
I'm using this to test variants of the monad. In particular, the
current monad displays O(n^2) performance with the seql version of
this benchmark. This is a well-studied problem, see for example
"Reflection without remorse: revealing a hidden sequence to speed up
monadic reflection" (van der Ploeg / Kiselyov, Haskell '14)
Test Plan:
Built it and ran it a few times
unit tests still work
Reviewed By: bnitka@fb.com
Subscribers: ldbrandy, kjm, jlengyel, memo, watashi, smarlow, akr, bnitka, jcoens
FB internal diff: D2419419
Signature: t1:2419419:1441640727:9f5f82212c829fcbf2e8c063d4dbd0db495b0ba2
2015-09-07 19:00:27 +03:00
|
|
|
return ()
|
|
|
|
"tree" -> runHaxl env $ void $ tree n
|
2016-06-20 12:37:33 +03:00
|
|
|
-- No memoization
|
|
|
|
"memo0" -> runHaxl env $
|
|
|
|
Haxl.sequence_ [unionWombats | _ <- [1..n]]
|
|
|
|
-- One put, N gets.
|
|
|
|
"memo1" -> runHaxl env $
|
|
|
|
Haxl.sequence_ [memo (42 :: Int) unionWombats | _ <- [1..n]]
|
|
|
|
-- N puts, N gets.
|
|
|
|
"memo2" -> runHaxl env $
|
|
|
|
Haxl.sequence_ [memo (i :: Int) unionWombats | i <- [1..n]]
|
2016-07-01 18:18:37 +03:00
|
|
|
"memo3" ->
|
|
|
|
runHaxl env $ do
|
|
|
|
ref <- newMemoWith unionWombats
|
|
|
|
let c = runMemo ref
|
|
|
|
Haxl.sequence_ [c | _ <- [1..n]]
|
2016-07-25 16:13:14 +03:00
|
|
|
"memo4" ->
|
|
|
|
runHaxl env $ do
|
|
|
|
let f = unionWombatsTo
|
|
|
|
Haxl.sequence_ [f x | x <- take n $ cycle [100, 200 .. 1000]]
|
|
|
|
"memo5" ->
|
|
|
|
runHaxl env $ do
|
|
|
|
f <- memoize1 unionWombatsTo
|
|
|
|
Haxl.sequence_ [f x | x <- take n $ cycle [100, 200 .. 1000]]
|
|
|
|
"memo6" ->
|
|
|
|
runHaxl env $ do
|
|
|
|
let f = unionWombatsFromTo
|
|
|
|
Haxl.sequence_ [ f x y
|
|
|
|
| x <- take n $ cycle [100, 200 .. 1000]
|
|
|
|
, let y = x + 1000
|
|
|
|
]
|
|
|
|
"memo7" ->
|
|
|
|
runHaxl env $ do
|
|
|
|
f <- memoize2 unionWombatsFromTo
|
|
|
|
Haxl.sequence_ [ f x y
|
|
|
|
| x <- take n $ cycle [100, 200 .. 1000]
|
|
|
|
, let y = x + 1000
|
|
|
|
]
|
|
|
|
|
2016-07-06 13:29:36 +03:00
|
|
|
"cc1" -> runHaxl env $
|
2016-07-25 16:13:14 +03:00
|
|
|
Haxl.sequence_ [ cachedComputation (ListWombats 1000) unionWombats
|
|
|
|
| _ <- [1..n]
|
|
|
|
]
|
2016-07-01 18:18:37 +03:00
|
|
|
|
Add a benchmark for basic Haxl monad patterns
Summary:
I'm using this to test variants of the monad. In particular, the
current monad displays O(n^2) performance with the seql version of
this benchmark. This is a well-studied problem, see for example
"Reflection without remorse: revealing a hidden sequence to speed up
monadic reflection" (van der Ploeg / Kiselyov, Haskell '14)
Test Plan:
Built it and ran it a few times
unit tests still work
Reviewed By: bnitka@fb.com
Subscribers: ldbrandy, kjm, jlengyel, memo, watashi, smarlow, akr, bnitka, jcoens
FB internal diff: D2419419
Signature: t1:2419419:1441640727:9f5f82212c829fcbf2e8c063d4dbd0db495b0ba2
2015-09-07 19:00:27 +03:00
|
|
|
_ -> do
|
2016-07-25 16:13:14 +03:00
|
|
|
hPutStrLn stderr $ "syntax: monadbench " ++ concat
|
|
|
|
[ "par1"
|
|
|
|
, "par2"
|
|
|
|
, "seqr"
|
|
|
|
, "seql"
|
|
|
|
, "memo0"
|
|
|
|
, "memo1"
|
|
|
|
, "memo2"
|
|
|
|
, "memo3"
|
|
|
|
, "memo4"
|
|
|
|
, "memo5"
|
|
|
|
, "memo6"
|
|
|
|
, "memo7"
|
|
|
|
, "cc1"
|
|
|
|
]
|
Add a benchmark for basic Haxl monad patterns
Summary:
I'm using this to test variants of the monad. In particular, the
current monad displays O(n^2) performance with the seql version of
this benchmark. This is a well-studied problem, see for example
"Reflection without remorse: revealing a hidden sequence to speed up
monadic reflection" (van der Ploeg / Kiselyov, Haskell '14)
Test Plan:
Built it and ran it a few times
unit tests still work
Reviewed By: bnitka@fb.com
Subscribers: ldbrandy, kjm, jlengyel, memo, watashi, smarlow, akr, bnitka, jcoens
FB internal diff: D2419419
Signature: t1:2419419:1441640727:9f5f82212c829fcbf2e8c063d4dbd0db495b0ba2
2015-09-07 19:00:27 +03:00
|
|
|
exitWith (ExitFailure 1)
|
|
|
|
t1 <- getCurrentTime
|
2017-10-03 10:14:24 +03:00
|
|
|
printf "%10s: %10d reqs: %.2fs\n"
|
|
|
|
test n (realToFrac (t1 `diffUTCTime` t0) :: Double)
|
Add a benchmark for basic Haxl monad patterns
Summary:
I'm using this to test variants of the monad. In particular, the
current monad displays O(n^2) performance with the seql version of
this benchmark. This is a well-studied problem, see for example
"Reflection without remorse: revealing a hidden sequence to speed up
monadic reflection" (van der Ploeg / Kiselyov, Haskell '14)
Test Plan:
Built it and ran it a few times
unit tests still work
Reviewed By: bnitka@fb.com
Subscribers: ldbrandy, kjm, jlengyel, memo, watashi, smarlow, akr, bnitka, jcoens
FB internal diff: D2419419
Signature: t1:2419419:1441640727:9f5f82212c829fcbf2e8c063d4dbd0db495b0ba2
2015-09-07 19:00:27 +03:00
|
|
|
where
|
|
|
|
-- can't use >>, it is aliased to *> and we want the real bind here
|
|
|
|
andThen x y = x >>= const y
|
|
|
|
|
2019-04-10 19:45:58 +03:00
|
|
|
tree :: Int -> GenHaxl () () [Id]
|
Add a benchmark for basic Haxl monad patterns
Summary:
I'm using this to test variants of the monad. In particular, the
current monad displays O(n^2) performance with the seql version of
this benchmark. This is a well-studied problem, see for example
"Reflection without remorse: revealing a hidden sequence to speed up
monadic reflection" (van der Ploeg / Kiselyov, Haskell '14)
Test Plan:
Built it and ran it a few times
unit tests still work
Reviewed By: bnitka@fb.com
Subscribers: ldbrandy, kjm, jlengyel, memo, watashi, smarlow, akr, bnitka, jcoens
FB internal diff: D2419419
Signature: t1:2419419:1441640727:9f5f82212c829fcbf2e8c063d4dbd0db495b0ba2
2015-09-07 19:00:27 +03:00
|
|
|
tree 0 = listWombats 0
|
|
|
|
tree n = concat <$> Haxl.sequence
|
|
|
|
[ tree (n-1)
|
|
|
|
, listWombats (fromIntegral n), tree (n-1)
|
|
|
|
]
|
2016-06-20 12:37:33 +03:00
|
|
|
|
2019-04-10 19:45:58 +03:00
|
|
|
unionWombats :: GenHaxl () () [Id]
|
2016-07-01 18:18:37 +03:00
|
|
|
unionWombats = foldl List.union [] <$> Haxl.mapM listWombats [1..1000]
|
2016-07-25 16:13:14 +03:00
|
|
|
|
2019-04-10 19:45:58 +03:00
|
|
|
unionWombatsTo :: Id -> GenHaxl () () [Id]
|
2016-07-25 16:13:14 +03:00
|
|
|
unionWombatsTo x = foldl List.union [] <$> Haxl.mapM listWombats [1..x]
|
|
|
|
|
2019-04-10 19:45:58 +03:00
|
|
|
unionWombatsFromTo :: Id -> Id -> GenHaxl () () [Id]
|
2016-07-25 16:13:14 +03:00
|
|
|
unionWombatsFromTo x y = foldl List.union [] <$> Haxl.mapM listWombats [x..y]
|