@@ -0,0 +1,94 @@
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.Text.Encoding as T
18+import qualified Data.Text.Encoding.Error as E
19+import qualified Data.Text.Encoding.Fusion as T
20+import qualified Data.Text.Encoding.Fusion.Common as F
21+import qualified Data.Text.Fusion as T
22+import qualified Data.Text.IO as T
23+import qualified Data.Text.Lazy.Encoding as TL
24+import qualified Data.Text.Lazy.Encoding.Fusion as TL
25+import qualified Data.Text.Lazy.Fusion as TL
26+import qualified Data.Text.Lazy.IO as TL
27+
28+instance NFData a => NFData (Stream a) where
29+ -- Currently, this implementation does not force evaluation of the size hint
30+ rnf (Stream next s0 _) = go s0
31+ where
32+ go !s = case next s of
33+ Done -> ()
34+ Skip s' -> go s'
35+ Yield x s' -> rnf x `seq` go s'
36+
37+benchmark :: FilePath -> IO Benchmark
38+benchmark fp = do
39+ -- Different formats
40+ t <- T.readFile fp
41+ let !utf8 = T.encodeUtf8 t
42+ !utf16le = T.encodeUtf16LE t
43+ !utf16be = T.encodeUtf16BE t
44+ !utf32le = T.encodeUtf32LE t
45+ !utf32be = T.encodeUtf32BE t
46+
47+ -- Once again for the lazy variants
48+ tl <- TL.readFile fp
49+ let !utf8L = TL.encodeUtf8 tl
50+ !utf16leL = TL.encodeUtf16LE tl
51+ !utf16beL = TL.encodeUtf16BE tl
52+ !utf32leL = TL.encodeUtf32LE tl
53+ !utf32beL = TL.encodeUtf32BE tl
54+
55+ -- For the functions which operate on streams
56+ let !s = T.stream t
57+
58+ return $ bgroup "Stream"
59+
60+ -- Fusion
61+ [ bgroup "stream" $
62+ [ bench "Text" $ nf T.stream t
63+ , bench "LazyText" $ nf TL.stream tl
64+ ]
65+
66+ -- Encoding.Fusion
67+ , bgroup "streamUtf8"
68+ [ bench "Text" $ nf (T.streamUtf8 E.lenientDecode) utf8
69+ , bench "LazyText" $ nf (TL.streamUtf8 E.lenientDecode) utf8L
70+ ]
71+ , bgroup "streamUtf16LE"
72+ [ bench "Text" $ nf (T.streamUtf16LE E.lenientDecode) utf16le
73+ , bench "LazyText" $ nf (TL.streamUtf16LE E.lenientDecode) utf16leL
74+ ]
75+ , bgroup "streamUtf16BE"
76+ [ bench "Text" $ nf (T.streamUtf16BE E.lenientDecode) utf16be
77+ , bench "LazyText" $ nf (TL.streamUtf16BE E.lenientDecode) utf16beL
78+ ]
79+ , bgroup "streamUtf32LE"
80+ [ bench "Text" $ nf (T.streamUtf32LE E.lenientDecode) utf32le
81+ , bench "LazyText" $ nf (TL.streamUtf32LE E.lenientDecode) utf32leL
82+ ]
83+ , bgroup "streamUtf32BE"
84+ [ bench "Text" $ nf (T.streamUtf32BE E.lenientDecode) utf32be
85+ , bench "LazyText" $ nf (TL.streamUtf32BE E.lenientDecode) utf32beL
86+ ]
87+
88+ -- Encoding.Fusion.Common
89+ , bench "restreamUtf8" $ nf F.restreamUtf8 s
90+ , bench "restreamUtf16LE" $ nf F.restreamUtf16LE s
91+ , bench "restreamUtf16BE" $ nf F.restreamUtf16BE s
92+ , bench "restreamUtf32LE" $ nf F.restreamUtf32LE s
93+ , bench "restreamUtf32BE" $ nf F.restreamUtf32BE s
94+ ]