Skip to content

Commit

Permalink
added Data.Text.IO.Utf8.hPutStr to benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
BebeSparkelSparkel committed Apr 25, 2024
1 parent 7ebc8b9 commit 43e6883
Showing 1 changed file with 26 additions and 23 deletions.
49 changes: 26 additions & 23 deletions benchmarks/haskell/Benchmarks/FileWrite.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,22 @@ module Benchmarks.FileWrite
import System.IO
import Data.String (fromString)
import qualified Data.Text.Lazy as LT
import Test.Tasty.Bench (Benchmark, bgroup, bench, nfIO)
import Test.Tasty.Bench (Benchmark, bgroup, bench, nfAppIO)
import qualified Data.Text.IO as T
import qualified Data.Text.Lazy.IO as LT
import Control.DeepSeq (deepseq)
import Control.DeepSeq (NFData, deepseq)
import Data.Functor ((<&>))
import Data.Text (StrictText)
import Data.Text.Lazy (LazyText)
import qualified Data.Text.IO.Utf8 as Utf8
import Data.Bifunctor (first)

mkFileWriteBenchmarks :: IO (Handle, IO ()) -> IO (Benchmark, IO ())
mkFileWriteBenchmarks mkSinkNRemove = do
let writeDate = LT.cycle $ fromString [minBound..maxBound]
lengths = [0..5] <> [10,20..100] <> [1000,10000,100000]
newlineSelect nl = do
testGroup :: NFData text => String -> (Handle -> text -> IO ()) -> ((StrictText,LazyText) -> text) -> Newline -> IO (Benchmark, IO ())
testGroup groupName hPutStr select nl = do

Check warning on line 32 in benchmarks/haskell/Benchmarks/FileWrite.hs

View workflow job for this annotation

GitHub Actions / build (windows-latest, latest)

This binding for ‘hPutStr’ shadows the existing binding

Check warning on line 32 in benchmarks/haskell/Benchmarks/FileWrite.hs

View workflow job for this annotation

GitHub Actions / build (macOS-latest, latest)

This binding for ‘hPutStr’ shadows the existing binding
let nlm = NewlineMode nl nl
(!noBufH, noBufRm) <- mkSinkNRemove
hSetBuffering noBufH NoBuffering
Expand All @@ -37,31 +42,29 @@ mkFileWriteBenchmarks mkSinkNRemove = do
hSetNewlineMode blockBufH nlm

return
( bgroup ("Newline " <> show nl) $ lengths <&> \n -> let
( bgroup (groupName <> " " <> show nl) $ lengths <&> \n -> let
st = LT.toStrict lt
lt = LT.take n writeDate
in bgroup ("length " <> show n)
[ deepseq st $ bgroup "StrictText"
[ bench "NoBuffering" $ nfIO $ T.hPutStr noBufH st
, bench "LineBuffering" $ nfIO $ T.hPutStr lineBufH st
, bench "BlockBuffering" $ nfIO $ T.hPutStr blockBufH st
]
, deepseq lt $ bgroup "LazyText"
[ bench "NoBuffering" $ nfIO $ LT.hPutStr noBufH lt
, bench "LineBuffering" $ nfIO $ LT.hPutStr lineBufH lt
, bench "BlockBuffering" $ nfIO $ LT.hPutStr blockBufH lt
]
]
t = select (st, lt)
in bgroup ("length " <> show n) $ deepseq t
[ bench "NoBuffering" $ nfAppIO (hPutStr noBufH) t
, bench "LineBuffering" $ nfAppIO (hPutStr lineBufH) t
, bench "BlockBuffering" $ nfAppIO (hPutStr blockBufH) t
]
, do
noBufRm
lineBufRm
blockBufRm
)
first (bgroup "FileWrite")
. foldr (\(b,r) (bs,rs) -> (b:bs,r>>rs)) ([], return ())
<$> sequence
[ testGroup "Strict hPutStr" T.hPutStr strict LF
, testGroup "Lazy hPutStr" LT.hPutStr lazy LF
, testGroup "Strict hPutStr" T.hPutStr strict CRLF
, testGroup "Lazy hPutStr" LT.hPutStr lazy CRLF
, testGroup "Utf-8 hPutStr" Utf8.hPutStr strict LF
]

(lfB, lfR) <- newlineSelect LF
(crlfB, crlfR) <- newlineSelect CRLF
return
( bgroup "FileWrite" [lfB, crlfB]
, lfR >> crlfR
)

strict = fst

Check warning on line 69 in benchmarks/haskell/Benchmarks/FileWrite.hs

View workflow job for this annotation

GitHub Actions / build (windows-latest, latest)

Top-level binding with no type signature: strict :: (a, b) -> a

Check warning on line 69 in benchmarks/haskell/Benchmarks/FileWrite.hs

View workflow job for this annotation

GitHub Actions / build (macOS-latest, latest)

Top-level binding with no type signature: strict :: (a, b) -> a
lazy = snd

Check warning on line 70 in benchmarks/haskell/Benchmarks/FileWrite.hs

View workflow job for this annotation

GitHub Actions / build (windows-latest, latest)

Top-level binding with no type signature: lazy :: (a, b) -> b

Check warning on line 70 in benchmarks/haskell/Benchmarks/FileWrite.hs

View workflow job for this annotation

GitHub Actions / build (macOS-latest, latest)

Top-level binding with no type signature: lazy :: (a, b) -> b

0 comments on commit 43e6883

Please sign in to comment.