@@ -0,0 +1,96 @@
1+-- |
2+-- Module : Streamly.Benchmark.Data.Mut.Array
3+-- Copyright : (c) 2020 Composewell Technologies
4+--
5+-- License : BSD-3-Clause
6+-- Maintainer : streamly@composewell.com
7+
8+{-# LANGUAGE CPP #-}
9+{-# LANGUAGE FlexibleContexts #-}
10+{-# LANGUAGE ScopedTypeVariables #-}
11+
12+#ifdef __HADDOCK_VERSION__
13+#undef INSPECTION
14+#endif
15+
16+#ifdef INSPECTION
17+{-# LANGUAGE TemplateHaskell #-}
18+{-# OPTIONS_GHC -fplugin Test.Inspection.Plugin #-}
19+#endif
20+
21+module Main
22+ (
23+ main
24+ ) where
25+
26+import Control.DeepSeq (NFData(..))
27+import System.Random (randomRIO)
28+import Prelude hiding ()
29+
30+import qualified Streamly.Prelude as Stream
31+import qualified Streamly.Internal.Data.Fold as Fold
32+import qualified Streamly.Internal.Data.Array.Stream.Mut.Foreign as MArray
33+
34+import Gauge hiding (env)
35+import Streamly.Prelude (SerialT, MonadAsync, IsStream)
36+import Streamly.Benchmark.Common
37+
38+#ifdef INSPECTION
39+import Foreign.Storable (Storable)
40+import Streamly.Internal.Data.Stream.StreamD.Type (Step(..))
41+import Test.Inspection
42+#endif
43+
44+-------------------------------------------------------------------------------
45+-- Utilities
46+-------------------------------------------------------------------------------
47+
48+{-# INLINE sourceUnfoldrM #-}
49+sourceUnfoldrM :: (IsStream t, MonadAsync m) => Int -> Int -> t m Int
50+sourceUnfoldrM value n = Stream.unfoldrM step n
51+ where
52+ step cnt =
53+ if cnt > n + value
54+ then return Nothing
55+ else return (Just (cnt, cnt + 1))
56+
57+{-# INLINE benchIO #-}
58+benchIO
59+ :: NFData b
60+ => String -> (Int -> t IO a) -> (t IO a -> IO b) -> Benchmark
61+benchIO name src sink =
62+ bench name $ nfIO $ randomRIO (1,1) >>= sink . src
63+
64+{-# INLINE partitionBy #-}
65+partitionBy :: Int -> SerialT IO (MArray.Array Int) -> IO ()
66+partitionBy value s = Stream.fold Fold.drain $ do
67+ a <- s
68+ MArray.partitionBy ( < value) a
69+
70+o_n_space_serial_marray ::
71+ Int -> [MArray.Array Int] -> [Benchmark]
72+o_n_space_serial_marray bound arrays =
73+ [ benchIO "partitionBy" (\_ -> Stream.fromList arrays)
74+ $ partitionBy bound
75+ ]
76+
77+-------------------------------------------------------------------------------
78+-- Driver
79+-------------------------------------------------------------------------------
80+
81+moduleName :: String
82+moduleName = "Data.Mut.Array"
83+
84+main :: IO ()
85+main = do
86+ runWithCLIOptsEnv defaultStreamSize alloc allBenchmarks
87+
88+ where
89+
90+ alloc value =
91+ Stream.toList $ MArray.arraysOf value $ sourceUnfoldrM value 0
92+
93+ allBenchmarks arrays value =
94+ [ bgroup (o_1_space_prefix moduleName) $
95+ o_n_space_serial_marray value arrays
96+ ]