@@ -0,0 +1,96 @@
2+-- Module : Streamly.Benchmark.Data.Mut.Array
3+-- Copyright : (c) 2020 Composewell Technologies
5+-- License : BSD-3-Clause
6+-- Maintainer : streamly@composewell.com
9+{-# LANGUAGE FlexibleContexts #-}
10+{-# LANGUAGE ScopedTypeVariables #-}
12+#ifdef __HADDOCK_VERSION__
17+{-# LANGUAGE TemplateHaskell #-}
18+{-# OPTIONS_GHC -fplugin Test.Inspection.Plugin #-}
26+import Control.DeepSeq (NFData(..))
27+import System.Random (randomRIO)
28+import Prelude hiding ()
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
34+import Gauge hiding (env)
35+import Streamly.Prelude (SerialT, MonadAsync, IsStream)
36+import Streamly.Benchmark.Common
39+import Foreign.Storable (Storable)
40+import Streamly.Internal.Data.Stream.StreamD.Type (Step(..))
41+import Test.Inspection
44+-------------------------------------------------------------------------------
46+-------------------------------------------------------------------------------
48+{-# INLINE sourceUnfoldrM #-}
49+sourceUnfoldrM :: (IsStream t, MonadAsync m) => Int -> Int -> t m Int
50+sourceUnfoldrM value n = Stream.unfoldrM step n
55+ else return (Just (cnt, cnt + 1))
57+{-# INLINE benchIO #-}
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
64+{-# INLINE partitionBy #-}
65+partitionBy :: Int -> SerialT IO (MArray.Array Int) -> IO ()
66+partitionBy value s = Stream.fold Fold.drain $ do
68+ MArray.partitionBy ( < value) a
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)
77+-------------------------------------------------------------------------------
79+-------------------------------------------------------------------------------
82+moduleName = "Data.Mut.Array"
86+ runWithCLIOptsEnv defaultStreamSize alloc allBenchmarks
91+ Stream.toList $ MArray.arraysOf value $ sourceUnfoldrM value 0
93+ allBenchmarks arrays value =
94+ [ bgroup (o_1_space_prefix moduleName) $
95+ o_n_space_serial_marray value arrays