@@ -0,0 +1,43 @@
1+-- | This module contains a number of benchmarks for the different streaming
2+-- functions
3+--
4+-- Tested in this benchmark:
5+--
6+-- * Most streaming functions
7+--
8+{-# LANGUAGE BangPatterns #-}
9+{-# OPTIONS_GHC -fno-warn-orphans #-}
10+module Data.Text.Benchmarks.Stream
11+ ( benchmark
12+ ) where
13+
14+import Control.DeepSeq (NFData (..))
15+import Criterion (Benchmark, bgroup, bench, nf)
16+import Data.Text.Fusion.Internal (Step (..), Stream (..))
17+import qualified Data.ByteString as B
18+import qualified Data.ByteString.Lazy as BL
19+import qualified Data.Text.Encoding.Error as E
20+import qualified Data.Text.Encoding.Fusion as T
21+import qualified Data.Text.Lazy.Encoding.Fusion as TL
22+
23+instance NFData a => NFData (Stream a) where
24+ -- Currently, this implementation does not force evaluation of the size hint
25+ rnf (Stream next s0 _) = go s0
26+ where
27+ go !s = case next s of
28+ Done -> ()
29+ Skip s' -> go s'
30+ Yield x s' -> rnf x `seq` go s'
31+
32+benchmark :: FilePath -> IO Benchmark
33+benchmark fp = do
34+ -- Load data
35+ bs <- B.readFile fp
36+ lbs <- BL.readFile fp
37+
38+ return $ bgroup "Stream"
39+ [ bgroup "streamUtf8"
40+ [ bench "Text" $ nf (T.streamUtf8 E.lenientDecode) bs
41+ , bench "LazyText" $ nf (TL.streamUtf8 E.lenientDecode) lbs
42+ ]
43+ ]