1-- |
2-- Module      : Compression
3-- Copyright   : (c) 2010 Simon Meier
4-- License     : BSD3-style (see LICENSE)
5--
6-- Maintainer  : Leon P Smith <leon@melding-monads.com>
7-- Stability   : experimental
8-- Portability : tested on GHC only
9--
10-- Benchmark the effect of first compacting the input stream for the 'zlib'
11-- compression package.
12--
13-- On a Core2 Duo T7500 with Linux 2.6.32-24 i686 and GHC 6.12.3 compacting
14-- first is worth its price up to chunks of 2kb size. Hence, in most
15-- serialization scenarios it is better to first use a builder and only then
16-- compress the output.
17--
18module Compression where
19
20import Data.Int
21import Data.Monoid (mconcat, mappend)
22
23import Criterion.Main
24import qualified Data.ByteString.Lazy as L
25import qualified Data.ByteString.Char8 as S
26
27import qualified Blaze.ByteString.Builder as B
28import Codec.Compression.GZip
29
30main = defaultMain
31    [ bench "compress directly (chunksize 10)" $
32        whnf benchCompressDirectly byteString10
33    , bench "compress compacted (chunksize 10)" $
34        whnf benchCompressCompacted byteString10
35    , bench "compress directly (chunksize 2kb)" $
36        whnf benchCompressDirectly byteString2kb
37    , bench "compress compacted (chunksize 2kb)" $
38        whnf benchCompressCompacted byteString2kb
39    ]
40  where
41    n = 100000
42
43    byteString10 = L.fromChunks $ replicate n $ S.pack $ take 10 ['\x0'..]
44    {-# NOINLINE byteString10 #-}
45
46    byteString2kb = L.fromChunks $ replicate (n `div` 200) $ S.pack $ take 2048 ['\x0'..]
47    {-# NOINLINE byteString2kb #-}
48
49
50benchCompressDirectly :: L.ByteString -> Int64
51benchCompressDirectly = L.length . compress
52
53benchCompressCompacted :: L.ByteString -> Int64
54benchCompressCompacted =
55  L.length . compress . B.toLazyByteString . B.fromLazyByteString
56