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,
|
|
|
|
-- found in the LICENSE file. An additional grant of patent rights can
|
|
|
|
-- be found in the PATENTS file.
|
|
|
|
|
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
|
|
|
|
import Data.Time.Clock
|
|
|
|
import System.Environment
|
|
|
|
import System.Exit
|
|
|
|
import System.IO
|
|
|
|
import Text.Printf
|
|
|
|
|
|
|
|
import Haxl.Prelude as Haxl
|
|
|
|
import Prelude()
|
|
|
|
|
|
|
|
import Haxl.Core
|
|
|
|
|
|
|
|
import ExampleDataSource
|
|
|
|
|
|
|
|
testEnv :: IO (Env ())
|
|
|
|
testEnv = do
|
|
|
|
exstate <- ExampleDataSource.initGlobalState
|
|
|
|
let st = stateSet exstate stateEmpty
|
|
|
|
initEnv st ()
|
|
|
|
|
|
|
|
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
|
|
|
|
foldl andThen (return []) (map listWombats [1.. fromIntegral n])
|
|
|
|
return ()
|
|
|
|
"tree" -> runHaxl env $ void $ tree n
|
|
|
|
_ -> do
|
|
|
|
hPutStrLn stderr "syntax: monadbench par1|par2|seqr|seql NUM"
|
|
|
|
exitWith (ExitFailure 1)
|
|
|
|
t1 <- getCurrentTime
|
|
|
|
printf "%d reqs: %.2fs\n" n (realToFrac (t1 `diffUTCTime` t0) :: Double)
|
|
|
|
where
|
|
|
|
-- can't use >>, it is aliased to *> and we want the real bind here
|
|
|
|
andThen x y = x >>= const y
|
|
|
|
|
|
|
|
tree :: Int -> GenHaxl () [Id]
|
|
|
|
tree 0 = listWombats 0
|
|
|
|
tree n = concat <$> Haxl.sequence
|
|
|
|
[ tree (n-1)
|
|
|
|
, listWombats (fromIntegral n), tree (n-1)
|
|
|
|
]
|