on master help sign in
Add machine benchmark and fix others
commitcc4743cb5ffa12f4276a11d85600da8946bd111d authorHarendra Kumar <harendra.kumar@gmail.com> dateSat 22 Jul 2017, 19:49 (9 years ago) parent4d4894c
2 files changed+25−12
asyncly.cabal+2 benchmark/Main.hs+23−12
asyncly.cabal+2
@@ -68,6 +68,7 @@ benchmark bench
6868 ghc-options: -Wall -fwarn-identities -fwarn-incomplete-record-updates
6969 -fwarn-incomplete-uni-patterns -fwarn-tabs
7070 -O2 -funbox-strict-fields
71+ -fno-ignore-asserts
7172 build-depends:
7273 asyncly
7374 , atomic-primops
@@ -76,6 +77,7 @@ benchmark bench
7677 , conduit-combinators
7778 , criterion
7879 , io-streams
80+ , machines
7981 , mtl
8082 , pipes
8183 , simple-conduit
benchmark/Main.hs+23−12
@@ -24,6 +24,7 @@ import System.IO.Unsafe (unsafePerformIO)
2424import Transient.Internals as T
2525import Transient.Indeterminism as T
2626
27+import Data.Machine as M
2728import Asyncly as A
2829
2930main :: IO ()
@@ -31,11 +32,12 @@ main = do
3132 defaultMain [
3233 bgroup "basic" [ bench "asyncly" $ nfIO asyncly_basic
3334 , bench "transient" $ nfIO transient_basic
35+ , bench "machines" $ nfIO machines_basic
3436 , bench "stream" $ nfIO stream_basic
35− , bench "iostreams" $ nfIO iostreams_basic
36− , bench "pipes" $ nfIO pipes_basic
3737 , bench "conduit" $ nfIO conduit_basic
38− -- , bench "simple-conduit" $ nfIO simple_conduit_basic
38+ , bench "pipes" $ nfIO pipes_basic
39+ , bench "simple-conduit" $ nfIO simple_conduit_basic
40+ , bench "iostreams" $ nfIO iostreams_basic
3941 -- , bench "fusion" $ nfIO fusion_basic
4042
4143 ]
@@ -47,7 +49,7 @@ map f x = return $ f x
4749
4850{-# INLINABLE filter #-}
4951filter :: (Monad m, Alternative m) => (a -> Bool) -> a -> m a
50−filter cond x = guard (cond x) >> return x
52+filter cond x = guard (not $ cond x) >> return x
5153
5254{-# NOINLINE count #-}
5355count :: IORef Int
@@ -57,8 +59,7 @@ drop :: (MonadIO m, Alternative m) => Int -> Int -> m Int
5759drop num x = do
5860
5961 mn <- liftIO $ atomicModifyIORefCAS count $ \n ->
60− let n' = n + 1
61− in if n' < num then (n + 1, False) else (n + 1, True)
62+ if n < num then (n + 1, False) else (n, True)
6263 guard mn
6364 return x
6465
@@ -73,13 +74,13 @@ tdrop = Main.drop
7374
7475transient_basic :: IO Int
7576transient_basic = T.keep' $ T.threads 0 $ do
76− xs <- T.group 499000 $ do
77− liftIO $ writeIORef count 0
78− T.choose [1..1000000 :: Int]
77+ liftIO $ writeIORef count 0
78+ xs <- T.group 499001 $ do
79+ T.choose [1..1000001 :: Int]
7980 >>= tfilter even
8081 >>= tmap (+1)
8182 >>= tdrop 1000
82− >>= tmap (+1)
83+ >>= tmap (+1)
8384 >>= tfilter (\x -> x `mod` 2 == 0)
8485
8586 assert (Prelude.length xs == 499000) $
@@ -104,10 +105,20 @@ asyncly_basic = do
104105 >>= adrop 1000
105106 >>= amap (+1)
106107 >>= afilter (\x -> x `mod` 2 == 0)
107−
108108 assert (Prelude.length xs == 499000) $
109109 return (Prelude.length xs)
110110
111+machines_basic :: IO Int
112+machines_basic = do
113+ xs <- M.runT $ M.source [1..1000000]
114+ M.~> M.filtered even
115+ M.~> M.mapping (+1)
116+ M.~> M.dropping 1000
117+ M.~> M.mapping (+1)
118+ M.~> M.filtered (\x -> x `mod` 2 == 0)
119+ assert (Prelude.length xs == 499000) $
120+ return (Prelude.length (xs ::[Int]))
121+
111122pipes_basic :: IO Int
112123pipes_basic = do
113124 xs <- P.toListM $ P.each [1..1000000]
@@ -159,7 +170,7 @@ simple_conduit_basic = do
159170stream_basic :: IO Int
160171stream_basic = do
161172 let ns = [1..1000000] :: [Int]
162− xs <- Str.toList $ Str.each ns
173+ xs Str.:> _ <- Str.toList $ Str.each ns
163174 & Str.filter even
164175 & Str.map (+1)
165176 & Str.drop 1000