packages feed

string-interpolate-0.3.0.1 / bench / bench.hs

{-# LANGUAGE CPP               #-}{-# LANGUAGE ConstraintKinds   #-}{-# LANGUAGE FlexibleContexts  #-}{-# LANGUAGE GADTs             #-}{-# LANGUAGE KindSignatures    #-}{-# LANGUAGE OverloadedStrings #-}{-# LANGUAGE PackageImports    #-}{-# LANGUAGE QuasiQuotes       #-}{-# LANGUAGE RankNTypes        #-}{-# LANGUAGE TypeApplications  #-}import Criterion      ( Benchmark, bench, bgroup, env, nf )import Criterion.Main ( defaultMain )import qualified Data.ByteString      as Bimport qualified Data.ByteString.Lazy as LBimport qualified Data.String          as Simport qualified Data.Text            as Timport qualified Data.Text.Lazy       as LTimport qualified "string-interpolate" Data.String.Interpolate            as SIimport qualified "string-interpolate" Data.String.Interpolate.Conversion as SIimport qualified "interpolate" Data.String.Interpolate.IsString          as Iimport           "formatting" Formatting                                 ( (%) )import qualified "formatting" Formatting                                 as Fimport qualified "formatting" Formatting.ShortFormatters                 as Fimport qualified "neat-interpolation" NeatInterpolation                  as NIimport Control.DeepSeqimport Test.QuickCheck#ifdef EXTENDED_BENCHMARKSimport "Interpolation" Data.String.Interpolation                as Nimport "interpolatedstring-perl6" Text.InterpolatedString.Perl6 as P#endiftype SIInterpolatable str flag =  ( SI.IsCustomSink str ~ flag  , SI.InterpSink flag str  , SI.Interpolatable flag str str  , SI.Interpolatable flag Int str  , SI.Interpolatable flag Bool str  )type AllInterpolatable str flag =  ( SIInterpolatable str flag  , Show str  , S.IsString str  , Monoid str  )---------------------- string-interpolate--------------------singleInterpSI :: SIInterpolatable str flag => str -> strsingleInterpSI str = [SI.i|A fine day to die, #{str}.|]multiInterpSI :: SIInterpolatable str flag => (Int, str, Bool) -> strmultiInterpSI (x, y, z) = [SI.i| foo #{x} bar #{y} baz #{z} quux |]---------------------- interpolate--------------------singleInterpI :: (Show str, S.IsString str) => str -> strsingleInterpI str = [I.i|A fine day to die, #{str}.|]multiInterpI :: (Show str, S.IsString str) => (Int, str, Bool) -> strmultiInterpI (x, y, z) = [I.i| foo #{x} bar #{y} baz #{z} quux |]---------------------- formatting--------------------stringF :: String -> StringstringF = F.formatToString ("A fine day to die, " % F.s % ".")multiStringF :: (Int, String, Bool) -> StringmultiStringF (x, y, z) =  F.formatToString (" foo " % F.d % " bar " % F.s % " baz " % F.sh % " quux ") x y ztextF :: T.Text -> T.TexttextF = F.sformat ("A fine day to die, " % F.st % ".")multiTextF :: (Int, T.Text, Bool) -> T.TextmultiTextF (x, y, z) =  F.sformat (" foo " % F.d % " bar " % F.st % " baz " % F.sh % " quux ") x y zlazyTextF :: LT.Text -> LT.TextlazyTextF = F.format ("A find day to die, " % F.t % ".")multiLazyTextF :: (Int, LT.Text, Bool) -> LT.TextmultiLazyTextF (x, y, z) =  F.format (" foo " % F.d % " bar " % F.t % " baz " % F.sh % " quux ") x y z---------------------- neat-interpolation--------------------textNI :: T.Text -> T.TexttextNI t = [NI.text|A fine day to die, $t.|]multiTextNI :: (Int, T.Text, Bool) -> T.TextmultiTextNI (x, y, z) =  let x' = T.pack $ show x      z' = T.pack $ show z  in [NI.text| foo $x' bar $y baz $z' quux |]#ifdef EXTENDED_BENCHMARKS---------------------- Interpolation--------------------singleInterpN :: (Monoid str, S.IsString str) => str -> strsingleInterpN t = [str|A fine day to die, $t$.|]multiInterpN ::(Monoid str, S.IsString str) => (Int, str, Bool) -> strmultiInterpN (x, y, z) = [str| foo $:x$ bar $y$ baz $:z$ quux |]---------------------- interpolatedstring-perl6--------------------singleInterpP :: (Monoid str, S.IsString str) => str -> strsingleInterpP t = [qc|A fine day to die, {t}.|]multiInterpP :: (Monoid str, S.IsString str) => (Int, str, Bool) -> strmultiInterpP (x, y, z) = [qc| foo {x} bar {y} baz {z} quux |]#endif---------------------- BENCHMARK GROUPS--------------------singleInterpBenches :: AllInterpolatable str flag                    => [(String, (str -> str))]singleInterpBenches =  [ ("string-interpolate"      , singleInterpSI)  , ("interpolate"             , singleInterpI)#ifdef EXTENDED_BENCHMARKS  , ("interpolatedstring-perl6", singleInterpP)  , ("Interpolation"           , singleInterpN)#endif  ]multiInterpBenches :: AllInterpolatable str flag                   => [(String, ((Int, str, Bool) -> str))]multiInterpBenches =  [ ("string-interpolate"      , multiInterpSI)  , ("interpolate"             , multiInterpI)#ifdef EXTENDED_BENCHMARKS  , ("interpolatedstring-perl6", multiInterpP)  , ("Interpolation"           , multiInterpN)#endif  ]main :: IO ()main = defaultMain $  [ benches @String "Small Strings Bench" "William" $      singleInterpBenches ++        [ ("formatting", stringF) ]  , benches @T.Text "Small Text Bench" "William" $      singleInterpBenches ++        [ ("formatting"        , textF)        , ("neat-interpolation", textNI)        ]  , benches @LT.Text "Small Lazy Text Bench" "William" $      singleInterpBenches ++        [ ("formatting", lazyTextF) ]  , benches @B.ByteString "Small ByteStrings Bench" "William" $      singleInterpBenches  , benches @LB.ByteString "Small Lazy ByteStrings Bench" "William" $      singleInterpBenches  , benches @String "Multiple Interpolations String Bench" (42, "CATALLAXY", True) $      multiInterpBenches ++        [ ("formatting", multiStringF) ]  , benches @T.Text "Multiple Interpolations Text Bench" (42, "CATALLAXY", True) $      multiInterpBenches ++        [ ("formatting"        , multiTextF)        , ("neat-interpolation", multiTextNI)        ]  , benches @LT.Text "Multiple Interpolations Lazy Text Bench" (42, "CATALLAXY", True) $      multiInterpBenches ++        [ ("formatting", multiLazyTextF) ]  , benches @B.ByteString "Multiple Interpolations ByteString Bench" (42, "CATALLAXY", True) $      multiInterpBenches  , benches @LB.ByteString "Multiple Interpolations Lazy ByteString Bench" (42, "CATALLAXY", True) $      multiInterpBenches  , env largeishText $ \ ~t -> benches @T.Text "Largeish Text Bench" t $      singleInterpBenches ++        [ ("formatting"        , textF)        , ("neat-interpolation", textNI)        ]  , env largeishLazyText $ \ ~lt -> benches @LT.Text "Largeish Lazy Text Bench" lt $      singleInterpBenches ++        [ ("formatting", lazyTextF) ]  , env largeishByteString $ \ ~bs -> benches @B.ByteString "Largeish ByteString Bench" bs $      singleInterpBenches  , env largeishLazyByteString $ \ ~lbs -> benches @LB.ByteString "Largeish Lazy ByteString Bench" lbs $      singleInterpBenches  ]largeishText :: IO T.TextlargeishText =  generate $ T.pack <$> Prelude.take 100000 <$> infiniteListOf arbitrarylargeishLazyText :: IO LT.TextlargeishLazyText =  generate $ LT.pack <$> Prelude.take 100000 <$> infiniteListOf arbitrarylargeishByteString :: IO B.ByteStringlargeishByteString =  generate $ B.pack <$> Prelude.take 100000 <$> infiniteListOf arbitrarylargeishLazyByteString :: IO LB.ByteStringlargeishLazyByteString =  generate $ LB.pack <$> Prelude.take 100000 <$> infiniteListOf arbitrary---------------------- BENCHMARK UTIL--------------------benches :: forall b a. NFData b => String -> a -> [(String, a -> b)] -> Benchmarkbenches groupname arg fs = bgroup groupname (fmap benchF fs)  where benchF (bname, f) = bench bname $ nf f arg