File: blaze.hs

package info (click to toggle)
haskell-conduit-extra 1.3.6-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 264 kB
  • sloc: haskell: 2,590; makefile: 5
file content (65 lines) | stat: -rw-r--r-- 1,882 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
{-# LANGUAGE OverloadedStrings #-}
import Data.Conduit
import qualified Data.Conduit.List as CL
import Data.Conduit.ByteString.Builder
import Gauge.Main
import Data.Monoid
import Data.ByteString.Builder

count :: Int
count = 100000

single :: Builder
single = shortByteString "Hello World!\n"

oneBuilderLeft :: Builder
oneBuilderLeft =
    loop count mempty
  where
    loop 0 b = b
    loop i b = loop (i - 1) (b <> single)

oneBuilderRight :: Builder
oneBuilderRight =
    loop count mempty
  where
    loop 0 b = b
    loop i b = loop (i - 1) (b <> single)

builderSource :: Monad m => ConduitT i Builder m ()
builderSource = CL.replicate count single

oneBSBuilderLeft :: Builder
oneBSBuilderLeft =
    loop count mempty
  where
    loop 0 b = b
    loop i b = loop (i - 1) (b <> single)

oneBSBuilderRight :: Builder
oneBSBuilderRight =
    loop count mempty
  where
    loop 0 b = b
    loop i b = loop (i - 1) (b <> single)

builderBSSource :: Monad m => ConduitT i Builder m ()
builderBSSource = CL.replicate count single

main :: IO ()
main = defaultMain
    [ bench "conduit, strict, safe" $ whnfIO $ runConduit $
        builderSource .| builderToByteString .| CL.sinkNull
    , bench "conduit, strict, unsafe" $ whnfIO $ runConduit $
        builderSource .| unsafeBuilderToByteString .| CL.sinkNull

    , bench "one builder, left" $ nf toLazyByteString oneBuilderLeft
    , bench "one builder, right" $ nf toLazyByteString oneBuilderRight
    , bench "conduit, lazy" $ flip nf builderSource $ \src ->
        toLazyByteString $ runConduitPure $ src .| CL.fold (<>) mempty

    , bench "one bs builder, left" $ nf toLazyByteString oneBSBuilderLeft
    , bench "one bs builder, right" $ nf toLazyByteString oneBSBuilderRight
    , bench "conduit BS, lazy" $ flip nf builderBSSource $ \src ->
        toLazyByteString $ runConduitPure $ src .| CL.fold (<>) mempty
    ]