on efecb26 help sign in
107 lines 3.2 kB history raw
1-- |
2-- Module : Streamly.Benchmark.Data.Parser
3-- Copyright : (c) 2020 Composewell Technologies
4--
5-- License : BSD-3-Clause
6-- Maintainer : streamly@composewell.com
8{-# LANGUAGE CPP #-}
9{-# LANGUAGE FlexibleContexts #-}
10{-# LANGUAGE ScopedTypeVariables #-}
11{-# OPTIONS_GHC -Wno-orphans #-}
13#undef FUSION_CHECK
14#ifdef FUSION_CHECK
15{-# OPTIONS_GHC -ddump-simpl -ddump-to-file -dsuppress-all #-}
16#endif
18module Main
19 (
20 main
21 ) where
23import Control.DeepSeq (NFData(..))
24import Control.Monad (replicateM_)
25import Streamly.Internal.Data.Parser (ParseError(..))
26import Streamly.Internal.Data.Stream (Stream)
27import Prelude hiding
28 (any, all, take, sequence, sequence_, sequenceA, takeWhile, dropWhile)
30import qualified Streamly.Data.Array as Array
31import qualified Streamly.Data.Stream as Stream
32import qualified Streamly.Internal.Unicode.Parser as PRU
34import Gauge hiding (env)
35import Streamly.Benchmark.Common
37runParser :: Int -> (Stream IO Char -> IO a) -> IO ()
38runParser count p = do
39 let v = "+123456789.123456789e-123"
40 let !(arr :: Array.Array Char) = Array.fromListN (length v) v
41 let s = Stream.unfold Array.reader arr
42 replicateM_ count (p s)
44-- | Takes a fold method, and uses it with a default source.
45{-# INLINE benchIOSink #-}
46benchIOSink :: Int -> String -> (Stream IO Char -> IO b) -> Benchmark
47benchIOSink value name f = bench name $ nfIO $ runParser value f
49{-# INLINE doubleParser #-}
50doubleParser :: Monad m => Stream m Char -> m (Either ParseError (Int, Int))
51doubleParser = Stream.parse PRU.doubleParser
53{-# INLINE number #-}
54number :: Monad m => Stream m Char -> m (Either ParseError (Integer, Int))
55number = Stream.parse PRU.number
57{-# INLINE double #-}
58double :: Monad m => Stream m Char -> m (Either ParseError Double)
59double = Stream.parse PRU.double
61{-# INLINE numberDouble #-}
62numberDouble :: Monad m => Stream m Char -> m (Either ParseError Double)
63numberDouble = Stream.parse p
64 where p = uncurry PRU.mkDouble <$> PRU.number
66-------------------------------------------------------------------------------
67-- Benchmarks
68-------------------------------------------------------------------------------
70moduleName :: String
71moduleName = "Unicode.Parser"
73instance NFData ParseError where
74 {-# INLINE rnf #-}
75 rnf (ParseError x) = rnf x
77o_n_heap_serial :: Int -> [Benchmark]
78o_n_heap_serial value =
79 [
80 benchIOSink value "doubleParser" doubleParser
81 , benchIOSink value "number" number
82 , benchIOSink value "double (doubleParser)" double
83 , benchIOSink value "double (number)" numberDouble
84 ]
86-------------------------------------------------------------------------------
87-- Driver
88-------------------------------------------------------------------------------
90main :: IO ()
91main = do
92#ifndef FUSION_CHECK
93 runWithCLIOpts defaultStreamSize allBenchmarks
95 where
97 allBenchmarks value =
98 [ bgroup (o_n_heap_prefix moduleName) (o_n_heap_serial value)
99 ]
100#else
101 -- Enable FUSION_CHECK macro at the beginning of the file
102 -- Enable one benchmark below, and run the benchmark
103 -- Check the .dump-simpl output
104 let value = 100000
105 runParser value double
106 return ()
107#endif