-
Notifications
You must be signed in to change notification settings - Fork 158
Added file write benchmarks #585
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7ebc8b9
Added file write benchmarks
BebeSparkelSparkel 43e6883
added Data.Text.IO.Utf8.hPutStr to benchmarks
BebeSparkelSparkel 067b43d
minor improvements
BebeSparkelSparkel 3817d2c
defined <&> for old versions of base
BebeSparkelSparkel 2d41b16
import <>
BebeSparkelSparkel 5af6f5f
added small chunks for hPutStr benchmarks
BebeSparkelSparkel e1992d0
more flexible FileWrite benchmarks
BebeSparkelSparkel 1ea9f72
whnfAppIO
BebeSparkelSparkel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
-- | Benchmarks simple file writing | ||
-- | ||
-- Tested in this benchmark: | ||
-- | ||
-- * Writing a file to the disk | ||
-- | ||
|
||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE CPP #-} | ||
|
||
module Benchmarks.FileWrite | ||
( mkFileWriteBenchmarks | ||
) where | ||
|
||
import Control.DeepSeq (NFData, deepseq) | ||
import Data.Bifunctor (first) | ||
import Data.List (intercalate, intersperse) | ||
import Data.Semigroup ((<>)) | ||
import Data.String (fromString) | ||
import Data.Text (StrictText) | ||
import Data.Text.Internal.Lazy (LazyText, defaultChunkSize) | ||
import System.IO (Handle, Newline(CRLF,LF), NewlineMode(NewlineMode), BufferMode(NoBuffering,LineBuffering,BlockBuffering), hSetBuffering, hSetNewlineMode) | ||
import Test.Tasty.Bench (Benchmark, bgroup, bench, nfAppIO) | ||
import qualified Data.Text as T | ||
import qualified Data.Text.IO as T | ||
import qualified Data.Text.IO.Utf8 as Utf8 | ||
import qualified Data.Text.Lazy as L | ||
import qualified Data.Text.Lazy.IO as L | ||
|
||
mkFileWriteBenchmarks :: IO (Handle, IO ()) -> IO (Benchmark, IO ()) | ||
mkFileWriteBenchmarks mkSinkNRemove = do | ||
let writeData = L.cycle $ fromString [minBound..maxBound] | ||
|
||
#ifdef ExtendedBenchmarks | ||
lengths = [0..5] <> [10,20..100] <> [1000,3000,10000,100000] | ||
#else | ||
lengths = [0,1,100,3000,10000,100000] | ||
#endif | ||
|
||
testGroup :: NFData text => (Handle -> text -> IO ()) -> ((String, StrictText -> text)) -> Newline -> BufferMode -> IO (Benchmark, IO ()) | ||
testGroup hPutStr (textCharacteristics, select) nl mode = do | ||
(h, removeFile) <- mkSinkNRemove | ||
hSetBuffering h mode | ||
hSetNewlineMode h $ NewlineMode nl nl | ||
pure | ||
( bgroup (intercalate " " [textCharacteristics, show nl, show mode]) $ | ||
lengths <&> \n -> let | ||
t = select $ L.toStrict $ L.take n writeData | ||
in bench ("length " <> show n) | ||
$ deepseq t | ||
$ nfAppIO (hPutStr h) t | ||
, removeFile | ||
) | ||
|
||
sequenceGroup "FileWrite hPutStr" | ||
#ifdef ExtendedBenchmarks | ||
[ testGroup T.hPutStr strict LF NoBuffering | ||
, testGroup L.hPutStr lazy LF NoBuffering | ||
|
||
, testGroup T.hPutStr strict LF LineBuffering | ||
, testGroup T.hPutStr strict CRLF LineBuffering | ||
, testGroup T.hPutStr strictNewlines LF LineBuffering | ||
, testGroup T.hPutStr strictNewlines CRLF LineBuffering | ||
|
||
, testGroup L.hPutStr lazy LF LineBuffering | ||
, testGroup L.hPutStr lazy CRLF LineBuffering | ||
, testGroup L.hPutStr lazySmallChunks LF LineBuffering | ||
, testGroup L.hPutStr lazySmallChunks CRLF LineBuffering | ||
, testGroup L.hPutStr lazyNewlines LF LineBuffering | ||
, testGroup L.hPutStr lazyNewlines CRLF LineBuffering | ||
, testGroup L.hPutStr lazySmallChunksNewlines LF LineBuffering | ||
, testGroup L.hPutStr lazySmallChunksNewlines CRLF LineBuffering | ||
|
||
, testGroup T.hPutStr strict LF (BlockBuffering Nothing) | ||
, testGroup T.hPutStr strict CRLF (BlockBuffering Nothing) | ||
, testGroup T.hPutStr strictNewlines LF (BlockBuffering Nothing) | ||
, testGroup T.hPutStr strictNewlines CRLF (BlockBuffering Nothing) | ||
|
||
, testGroup L.hPutStr lazy LF (BlockBuffering Nothing) | ||
, testGroup L.hPutStr lazy CRLF (BlockBuffering Nothing) | ||
, testGroup L.hPutStr lazySmallChunks LF (BlockBuffering Nothing) | ||
, testGroup L.hPutStr lazySmallChunks CRLF (BlockBuffering Nothing) | ||
, testGroup L.hPutStr lazyNewlines LF (BlockBuffering Nothing) | ||
, testGroup L.hPutStr lazyNewlines CRLF (BlockBuffering Nothing) | ||
, testGroup L.hPutStr lazySmallChunksNewlines LF (BlockBuffering Nothing) | ||
, testGroup L.hPutStr lazySmallChunksNewlines CRLF (BlockBuffering Nothing) | ||
|
||
, sequenceGroup "UTF-8" | ||
[ testGroup Utf8.hPutStr strict LF NoBuffering | ||
, testGroup Utf8.hPutStr strict LF LineBuffering | ||
, testGroup Utf8.hPutStr strict LF (BlockBuffering Nothing) | ||
] | ||
] | ||
#else | ||
[ testGroup T.hPutStr strictNewlines LF LineBuffering | ||
, testGroup T.hPutStr strictNewlines CRLF LineBuffering | ||
|
||
, testGroup T.hPutStr strict LF (BlockBuffering Nothing) | ||
, testGroup T.hPutStr strictNewlines CRLF (BlockBuffering Nothing) | ||
|
||
, testGroup L.hPutStr lazyNewlines LF LineBuffering | ||
, testGroup L.hPutStr lazyNewlines CRLF LineBuffering | ||
|
||
, testGroup L.hPutStr lazy LF (BlockBuffering Nothing) | ||
, testGroup L.hPutStr lazyNewlines CRLF (BlockBuffering Nothing) | ||
|
||
, sequenceGroup "UTF-8" | ||
[ testGroup Utf8.hPutStr strict LF LineBuffering | ||
, testGroup Utf8.hPutStr strict LF (BlockBuffering Nothing) | ||
] | ||
] | ||
#endif | ||
|
||
where | ||
lazy, lazyNewlines, lazySmallChunks, lazySmallChunksNewlines :: (String, StrictText -> LazyText) | ||
lazy = ("lazy", L.fromChunks . T.chunksOf defaultChunkSize) | ||
lazyNewlines = ("lazy many newlines", snd lazy . snd strictNewlines) | ||
lazySmallChunks = ("lazy small chunks", L.fromChunks . T.chunksOf 10) | ||
lazySmallChunksNewlines = ("lazy small chunks many newlines", snd lazySmallChunks . snd strictNewlines) | ||
|
||
strict, strictNewlines :: (String, StrictText -> StrictText) | ||
strict = ("strict", id) | ||
strictNewlines = ("strict many newlines", mconcat . intersperse "\n" . T.chunksOf 5) | ||
|
||
sequenceGroup groupName tgs | ||
= first (bgroup groupName) | ||
. foldr (\(b,r) (bs,rs) -> (b:bs,r>>rs)) ([], return ()) | ||
<$> sequence tgs | ||
|
||
(<&>) :: Functor f => f a -> (a -> b) -> f b | ||
(<&>) = flip fmap | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.