Skip to content

Commit 37d7d03

Browse files
committed
Benchmark: add built-in comparison with text-icu (tasty-bench only).
1 parent e690ad7 commit 37d7d03

File tree

2 files changed

+61
-14
lines changed

2 files changed

+61
-14
lines changed

benchmark/Benchmark.hs

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@ import Path.IO (listDir)
2020
import System.FilePath (dropExtensions, takeFileName)
2121

2222
import Gauge.Main (Benchmark, bench, bgroup, defaultMain, env, nf)
23+
#ifdef USE_TASTY_BENCH
24+
import Gauge.Main (bcompare)
25+
#endif
2326

2427
import qualified Data.Text as T
2528
import qualified Data.Text.Normalize as UTText
2629

27-
#ifdef BENCH_ICU
30+
#ifdef HAS_ICU
2831

2932
#if MIN_VERSION_text_icu(0,8,0)
3033
import qualified Data.Text.ICU.Normalize2 as ICU
@@ -58,30 +61,73 @@ dataDir = $(mkRelDir "benchmark") </> $(mkRelDir "data")
5861
dataSetSize :: Int
5962
dataSetSize = 1000000
6063

61-
makeBench :: (NFData a, NFData b) => (String, a -> b) -> (String, IO a) -> Benchmark
62-
makeBench (implName, func) (dataName, setup) =
63-
env setup (\txt -> bench (implName ++ "/" ++ dataName) (nf func txt))
64+
-- | Create a benchmark
65+
makeBench
66+
:: (NFData a, NFData b)
67+
=> (String, a -> b)
68+
-> String -- ^ Test name
69+
-> a
70+
-> Benchmark
71+
makeBench (implName, func) dataName =
72+
\txt -> bench (makeTestName implName dataName) (nf func txt)
73+
74+
-- | Format a test name
75+
makeTestName
76+
:: String -- ^ Implementation name
77+
-> String -- ^ Data name
78+
-> String
79+
makeTestName implName dataName = implName ++ "/" ++ dataName
80+
81+
#if defined(HAS_ICU) || !defined(USE_TASTY_BENCH)
82+
-- | Create refence benchmark
83+
makeBenchRef
84+
:: (NFData a, NFData b)
85+
=> (String, a -> b)
86+
-> (String, IO a)
87+
-> Benchmark
88+
makeBenchRef impl (dataName, setup) = env setup (makeBench impl dataName)
89+
#endif
90+
91+
-- | Create a benchmark which compares to the reference benchmark.
92+
makeBenchComp
93+
:: (NFData a, NFData b)
94+
=> String -- ^ Reference implementation
95+
-> (String, a -> b)
96+
-> (String, IO a)
97+
-> Benchmark
98+
#ifdef USE_TASTY_BENCH
99+
makeBenchComp implRef impl (dataName, setup) = env setup
100+
( bcompare ("$NF == \"" <> (makeTestName (fst impl) dataName)
101+
<> "\" && $(NF-1) == \"" <> implRef <> "\"")
102+
. makeBench impl dataName)
103+
#else
104+
makeBenchComp _ = makeBenchRef
105+
#endif
64106

107+
-- [TODO] read as text directly?
108+
-- | Read a file as 'String'.
65109
strInput :: FilePath -> (String, IO String)
66-
strInput file = (dataName file,
67-
fmap (take dataSetSize . cycle) (readFile file))
110+
strInput file =
111+
( dataName file
112+
, take dataSetSize . cycle <$> readFile file )
68113
where dataName = dropExtensions . takeFileName
69114

115+
-- | Read a file as 'T.Text'.
70116
txtInput :: FilePath -> (String, IO Text)
71-
txtInput file = second (fmap T.pack) (strInput file)
72-
where second f (a, b) = (a, f b)
117+
txtInput file = fmap T.pack <$> strInput file
73118

74119
main :: IO ()
75120
main = do
76-
dataFiles <- fmap (map toFilePath . snd) (listDir dataDir)
121+
dataFiles <- map toFilePath . snd <$> listDir dataDir
77122
defaultMain $
78123
[
79-
#ifdef BENCH_ICU
124+
#ifdef HAS_ICU
80125
bgroup "text-icu"
81-
$ makeBench <$> textICUFuncs <*> (map txtInput dataFiles)
126+
$ makeBenchRef <$> textICUFuncs <*> (map txtInput dataFiles)
82127
,
83128
#endif
84129
bgroup "unicode-transforms-text"
85-
$ makeBench <$> unicodeTransformTextFuncs
86-
<*> (map txtInput dataFiles)
130+
$ makeBenchComp "text-icu"
131+
<$> unicodeTransformTextFuncs
132+
<*> (map txtInput dataFiles)
87133
]

unicode-transforms.cabal

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ benchmark bench
188188
if flag(use-gauge)
189189
build-depends: gauge >=0.2.0 && <0.3
190190
else
191+
cpp-options: -DUSE_TASTY_BENCH
191192
build-depends: tasty-bench>= 0.2.5 && <0.4
192193
mixins: tasty-bench (Test.Tasty.Bench as Gauge.Main)
193194
if flag(dev)
@@ -197,7 +198,7 @@ benchmark bench
197198
if flag(has-llvm)
198199
ghc-options: -fllvm
199200
if flag(has-icu)
200-
cpp-options: -DBENCH_ICU
201+
cpp-options: -DHAS_ICU
201202
build-depends:
202203
text-icu
203204
if impl(ghc < 7.10)

0 commit comments

Comments
 (0)