Jasper Van der Jeugt avatar Jasper Van der Jeugt committed 777f381

Add Streaming benchmarks

Comments (0)

Files changed (2)

tests/benchmarks/src/Data/Text/Benchmarks.hs

 import qualified Data.Text.Benchmarks.ReadNumbers as ReadNumbers
 import qualified Data.Text.Benchmarks.Replace as Replace
 import qualified Data.Text.Benchmarks.Search as Search
+import qualified Data.Text.Benchmarks.Stream as Stream
 import qualified Data.Text.Benchmarks.WordFrequencies as WordFrequencies
 
 import qualified Data.Text.Benchmarks.Programs.BigTable as Programs.BigTable
         , ReadNumbers.benchmark (tf "numbers.txt")
         , Replace.benchmark (tf "russian.txt") "принимая" "своем"
         , Search.benchmark (tf "russian.txt") "принимая"
+        , Stream.benchmark (tf "russian.txt")
         , WordFrequencies.benchmark (tf "russian.txt")
         ]
 

tests/benchmarks/src/Data/Text/Benchmarks/Stream.hs

+-- | This module contains a number of benchmarks for the different streaming
+-- functions
+--
+-- Tested in this benchmark:
+--
+-- * Most streaming functions
+--
+{-# LANGUAGE BangPatterns #-}
+{-# OPTIONS_GHC -fno-warn-orphans #-}
+module Data.Text.Benchmarks.Stream
+    ( benchmark
+    ) where
+
+import Control.DeepSeq (NFData (..))
+import Criterion (Benchmark, bgroup, bench, nf)
+import Data.Text.Fusion.Internal (Step (..), Stream (..))
+import qualified Data.ByteString as B
+import qualified Data.ByteString.Lazy as BL
+import qualified Data.Text.Encoding.Error as E
+import qualified Data.Text.Encoding.Fusion as T
+import qualified Data.Text.Lazy.Encoding.Fusion as TL
+
+instance NFData a => NFData (Stream a) where
+    -- Currently, this implementation does not force evaluation of the size hint
+    rnf (Stream next s0 _) = go s0
+      where
+        go !s = case next s of
+            Done       -> ()
+            Skip s'    -> go s'
+            Yield x s' -> rnf x `seq` go s'
+
+benchmark :: FilePath -> IO Benchmark
+benchmark fp = do
+    -- Load data
+    bs  <- B.readFile fp
+    lbs <- BL.readFile fp
+
+    return $ bgroup "Stream"
+        [ bgroup "streamUtf8"
+            [ bench "Text"     $ nf (T.streamUtf8 E.lenientDecode) bs
+            , bench "LazyText" $ nf (TL.streamUtf8 E.lenientDecode) lbs
+            ]
+        ]
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.