Skip to content

Add some basic benchmarks #27

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions bench/Bench.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
module Main (main) where
import Data.String.Conversions
import Data.String.Conversions.Monomorphic
import Criterion.Main
import Test.QuickCheck

import qualified Data.ByteString.Lazy

import qualified Data.Text.Encoding
import qualified Data.Text.Encoding.Error
import qualified Data.Text.Lazy


main :: IO ()
main = defaultMain
[ env genStrings $ \ ~(short, medium, large) ->
bgroup "conv"
[ bgroup "cs"
[ bgroup "short" $ benchConvertString short
, bgroup "medium" $ benchConvertString medium
, bgroup "large" $ benchConvertString large
]
, bgroup "alt"
[ bgroup "short" $ benchAlt short
, bgroup "medium" $ benchAlt medium
, bgroup "large" $ benchAlt large
]
]
]


genStrings :: IO ([String], [String], [String])
genStrings = do
short <- generate (vectorOf 100 $ resize 30 $ listOf arbitraryUnicodeChar)
medium <- generate (vectorOf 100 $ resize 100 $ listOf arbitraryUnicodeChar)
large <- generate (vectorOf 100 $ resize 8000 $ listOf arbitraryUnicodeChar)
return (short, medium, large)


benchConvertString :: [String] -> [Benchmark]
benchConvertString strings =
[ bench "StrictByteString-to-LazyByteString" $
nf (map toLazyByteString) (map toStrictByteString strings)
, bench "StrictByteString-to-StrictText" $
nf (map toStrictText) (map toStrictByteString strings)
, bench "StrictByteString-to-LazyText" $
nf (map toLazyText) (map toStrictByteString strings)
, bench "LazyByteString-to-StrictByteString" $
nf (map toStrictByteString) (map toLazyByteString strings)
, bench "LazyByteString-to-StrictText" $
nf (map toStrictText) (map toLazyByteString strings)
, bench "LazyByteString-to-LazyText" $
nf (map toLazyText) (map toLazyByteString strings)
, bench "StrictText-to-StrictByteString" $
nf (map toStrictByteString) (map toStrictText strings)
, bench "StrictText-to-LazyByteString" $
nf (map toLazyByteString) (map toStrictText strings)
, bench "StrictText-to-LazyText" $
nf (map toLazyText) (map toStrictText strings)
, bench "LazyText-to-StrictByteString" $
nf (map toStrictByteString) (map toLazyText strings)
, bench "LazyText-to-LazyByteString" $
nf (map toLazyByteString) (map toLazyText strings)
, bench "LazyText-to-StrictText" $
nf (map toStrictText) (map toLazyText strings)
]


benchAlt :: [String] -> [Benchmark]
benchAlt strings =
[ bench "StrictByteString-to-LazyText" $
nf (map altSBStoLT) (map toSBS strings)

, bench "LazyByteString-to-LazyText" $
nf (map altLBStoLT) (map toLBS strings)
]


altSBStoLT :: StrictByteString -> LazyText
altSBStoLT =
Data.Text.Lazy.fromChunks . pure .
Data.Text.Encoding.decodeUtf8With Data.Text.Encoding.Error.lenientDecode


altLBStoLT :: LazyByteString -> LazyText
altLBStoLT =
Data.Text.Lazy.fromChunks .
map (Data.Text.Encoding.decodeUtf8With Data.Text.Encoding.Error.lenientDecode) .
Data.ByteString.Lazy.toChunks
11 changes: 11 additions & 0 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,14 @@ tests:
- quickcheck-instances
- deepseq
- QuickCheck


benchmarks:
bench:
main: Bench.hs
source-dirs:
- bench
dependencies:
- criterion
- string-conversions
- QuickCheck
52 changes: 37 additions & 15 deletions string-conversions.cabal
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
-- This file has been generated from package.yaml by hpack version 0.15.0.
cabal-version: 1.12

-- This file has been generated from package.yaml by hpack version 0.34.6.
--
-- see: https://github.com/sol/hpack

Expand All @@ -8,14 +10,14 @@ synopsis: Simplifies dealing with different types for strings
description: Provides a simple type class for converting values of different string types into values of other string types.
license: BSD3
license-file: LICENSE
tested-with: GHC == 7.6.3, GHC == 7.8.4, GHC == 7.10.1, GHC == 8.0.1
tested-with:
GHC == 7.6.3, GHC == 7.8.4, GHC == 7.10.1, GHC == 8.0.1
author: Sönke Hahn
maintainer: [email protected]
category: Data
homepage: https://github.com/soenkehahn/string-conversions#readme
bug-reports: https://github.com/soenkehahn/string-conversions/issues
build-type: Simple
cabal-version: >= 1.10

source-repository head
type: git
Expand All @@ -24,34 +26,54 @@ source-repository head
library
hs-source-dirs:
src
ghc-options: -Wall
default-language: Haskell2010
build-depends:
base == 4.*,
bytestring >= 0.9,
text >= 0.11,
utf8-string >= 0.3
base ==4.*,
bytestring >=0.9,
text >=0.11,
utf8-string >=0.3
exposed-modules:
Data.String.Conversions
Data.String.Conversions.Monomorphic
ghc-options: -Wall
other-modules:
Paths_string_conversions

test-suite spec
type: exitcode-stdio-1.0
main-is: Spec.hs
hs-source-dirs:
test,
test
src
build-depends:
base == 4.*,
bytestring >= 0.9,
text >= 0.11,
utf8-string >= 0.3,
QuickCheck,
base ==4.*,
bytestring >=0.9,
deepseq,
hspec,
quickcheck-instances,
deepseq,
QuickCheck
text >=0.11,
utf8-string >=0.3
other-modules:
Data.String.ConversionsSpec
Data.String.Conversions
Data.String.Conversions.Monomorphic
Paths_string_conversions
default-language: Haskell2010

benchmark bench
type: exitcode-stdio-1.0
main-is: Bench.hs
other-modules:
Paths_string_conversions
hs-source-dirs:
bench
build-depends:
QuickCheck,
base ==4.*,
bytestring >=0.9,
criterion,
string-conversions,
text >=0.11,
utf8-string >=0.3
default-language: Haskell2010