on 5bb0a49 help sign in
streamly/benchmark/LinearAsync.hs
92 lines 3.8 kB history raw
1-- |
2-- Module : Main
3-- Copyright : (c) 2018 Harendra Kumar
4--
5-- License : BSD3
6-- Maintainer : harendra.kumar@gmail.com
8import Control.DeepSeq (NFData)
9-- import Data.Functor.Identity (Identity, runIdentity)
10import System.Random (randomRIO)
11import qualified LinearOps as Ops
13import Streamly
14import Gauge
16-- We need a monadic bind here to make sure that the function f does not get
17-- completely optimized out by the compiler in some cases.
18--
19-- | Takes a fold method, and uses it with a default source.
20{-# INLINE benchIO #-}
21benchIO :: (IsStream t, NFData b) => String -> (t IO Int -> IO b) -> Benchmark
22benchIO name f = bench name $ nfIO $ randomRIO (1,1) >>= f . Ops.source
24-- | Takes a source, and uses it with a default drain/fold method.
25{-# INLINE benchSrcIO #-}
26benchSrcIO
27 :: (t IO Int -> SerialT IO Int)
28 -> String
29 -> (Int -> t IO Int)
30 -> Benchmark
31benchSrcIO t name f
32 = bench name $ nfIO $ randomRIO (1,1) >>= Ops.toNull t . f
34{-
35_benchId :: NFData b => String -> (Ops.Stream m Int -> Identity b) -> Benchmark
36_benchId name f = bench name $ nf (runIdentity . f) (Ops.source 10)
37-}
39main :: IO ()
40main =
41 defaultMain
42 [ bgroup "asyncly"
43 [ -- benchIO "unfoldr" $ Ops.toNull asyncly
44 benchSrcIO asyncly "unfoldrM" Ops.sourceUnfoldrM
45 -- , benchSrcIO asyncly "fromFoldable" Ops.sourceFromFoldable
46 , benchSrcIO asyncly "fromFoldableM" Ops.sourceFromFoldableM
47 -- , benchSrcIO asyncly "foldMapWith" Ops.sourceFoldMapWith
48 , benchSrcIO asyncly "foldMapWithM" Ops.sourceFoldMapWithM
49 , benchIO "mapM" $ Ops.mapM asyncly 1
50 , benchSrcIO asyncly "unfoldrM maxThreads 1"
51 (maxThreads 1 . Ops.sourceUnfoldrM)
52 , benchSrcIO asyncly "unfoldrM maxBuffer 1 (1000 ops)"
53 (maxBuffer 1 . Ops.sourceUnfoldrMN 1000)
54 ]
55 , bgroup "wAsyncly"
56 [ -- benchIO "unfoldr" $ Ops.toNull wAsyncly
57 benchSrcIO wAsyncly "unfoldrM" Ops.sourceUnfoldrM
58 -- , benchSrcIO wAsyncly "fromFoldable" Ops.sourceFromFoldable
59 , benchSrcIO wAsyncly "fromFoldableM" Ops.sourceFromFoldableM
60 -- , benchSrcIO wAsyncly "foldMapWith" Ops.sourceFoldMapWith
61 , benchSrcIO wAsyncly "foldMapWithM" Ops.sourceFoldMapWithM
62 , benchIO "mapM" $ Ops.mapM wAsyncly 1
63 ]
64 -- unfoldr and fromFoldable are always serial and thereofore the same for
65 -- all stream types.
66 , bgroup "aheadly"
67 [ -- benchIO "unfoldr" $ Ops.toNull aheadly
68 benchSrcIO aheadly "unfoldrM" Ops.sourceUnfoldrM
69 , benchSrcIO aheadly "fromFoldableM" Ops.sourceFromFoldableM
70 -- , benchSrcIO aheadly "foldMapWith" Ops.sourceFoldMapWith
71 , benchSrcIO aheadly "foldMapWithM" Ops.sourceFoldMapWithM
72 , benchIO "mapM" $ Ops.mapM aheadly 1
73 , benchSrcIO aheadly "unfoldrM maxThreads 1"
74 (maxThreads 1 . Ops.sourceUnfoldrM)
75 , benchSrcIO aheadly "unfoldrM maxBuffer 1 (1000 ops)"
76 (maxBuffer 1 . Ops.sourceUnfoldrMN 1000)
77 -- , benchSrcIO aheadly "fromFoldable" Ops.sourceFromFoldable
78 ]
79 -- XXX need to use smaller streams to finish in reasonable time
80 , bgroup "parallely"
81 [ --benchIO "unfoldr" $ Ops.toNull parallely
82 benchSrcIO parallely "unfoldrM" Ops.sourceUnfoldrM
83 --, benchSrcIO parallely "fromFoldable" Ops.sourceFromFoldable
84 , benchSrcIO parallely "fromFoldableM" Ops.sourceFromFoldableM
85 -- , benchSrcIO parallely "foldMapWith" Ops.sourceFoldMapWith
86 , benchSrcIO parallely "foldMapWithM" Ops.sourceFoldMapWithM
87 , benchIO "mapM" $ Ops.mapM parallely 1
88 -- Zip has only one parallel flavor
89 , benchIO "zip" Ops.zipAsync
90 , benchIO "zipM" Ops.zipAsyncM
91 ]
92 ]