Skip to content

Commit 2ea2351

Browse files
Improving PaymentRate parser
1 parent adf079f commit 2ea2351

File tree

1 file changed

+57
-4
lines changed

1 file changed

+57
-4
lines changed

bench/macro/lsm-tree-bench-unions.hs

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import Control.Applicative ((<**>))
4040
import Control.Concurrent (getNumCapabilities)
4141
import Control.Concurrent.Async
4242
import Control.Concurrent.MVar
43-
import Control.Monad (forM, forM_, void, when)
43+
import Control.Monad (forM, forM_, guard, void, when)
4444
import Control.Monad.ST (ST, runST)
4545
import Control.Monad.Trans.State.Strict (runState, state)
4646
import qualified Data.ByteString.Short as BS
@@ -72,12 +72,17 @@ import System.Directory (createDirectoryIfMissing)
7272
import System.IO
7373
import System.Mem (performMajorGC)
7474
import qualified System.Random as Random
75+
import qualified Text.ParserCombinators.ReadPrec as Lex
7576
import Text.Printf (printf)
7677
import Text.Read (Read (..))
78+
import qualified Text.Read as Lex
79+
import qualified Text.Read.Lex as Lex
7780

7881
import Database.LSMTree.Extras (groupsOfN)
7982
import Database.LSMTree.Internal.ByteString (byteArrayToSBS)
8083

84+
import System.Environment
85+
8186
-- We should be able to write this benchmark
8287
-- using only use public lsm-tree interface
8388
import qualified Database.LSMTree.Simple as LSM
@@ -172,7 +177,15 @@ instance Show PaymentRate where
172177

173178
instance Read PaymentRate where
174179

175-
readPrec = PaymentRate . toRational <$> (readPrec @Double)
180+
readPrec =
181+
let pFloat = toRational <$> (readPrec @Double)
182+
pFract = do
183+
num <- Lex.readP_to_Prec $ const Lex.readDecP
184+
Lex.Symbol sep <- Lex.lexP
185+
guard $ head sep `elem` ("/:%" :: String)
186+
den <- Lex.readP_to_Prec $ const Lex.readDecP
187+
pure $ num % den
188+
in PaymentRate <$> Lex.choice [ pFract, pFloat ]
176189

177190
-------------------------------------------------------------------------------
178191
-- command line interface
@@ -456,12 +469,18 @@ doDryRun' gopts opts = do
456469
-- |
457470
-- From StackOverflow: https://stackoverflow.com/a/30938328
458471
renderRational :: Int -> Rational -> String
459-
renderRational len rat = sign <> shows d ("." ++ take len (go next))
472+
renderRational len rat = sign <> shows prefix ("." ++ suffix)
460473
where
461474
sign
462475
| num < 0 = "-"
463476
| otherwise = ""
464-
(d, next) = abs num `quotRem` den
477+
478+
(prefix, next) = abs num `quotRem` den
479+
480+
suffix = case next of
481+
0 -> "0"
482+
n -> take len $ go n
483+
465484
num = numerator rat
466485
den = denominator rat
467486
go 0 = ""
@@ -541,6 +560,8 @@ doRun gopts opts = do
541560
| pipelined opts = pipelinedIterations h
542561
| otherwise = sequentialIterations h
543562

563+
print $ deriveFileNameForPlot gopts opts
564+
544565
refRNG <- newIORef $ initGen
545566
(initialSize gopts)
546567
(batchSize opts)
@@ -619,6 +640,37 @@ fillBetween title vs = Plot.liftEC $ do
619640
Plot.plot_fillbetween_style .= Plot.solidFillStyle color
620641
Plot.plot_fillbetween_values .= vs
621642

643+
deriveFileNameForPlot :: GlobalOpts -> RunOpts -> FilePath
644+
deriveFileNameForPlot gOpts rOpts =
645+
let partTable = show $ tableCount gOpts
646+
partWidth = List.intercalate "_" . fmap Fold.toList . groupsOfN 3 . reverse . show $ initialSize gOpts
647+
in Fold.fold
648+
[ "benchmark"
649+
, partTable
650+
, "×"
651+
, partWidth
652+
, ".png"
653+
]
654+
655+
{-
656+
data GlobalOpts = GlobalOpts
657+
{ rootDir :: !FilePath -- ^ session directory.
658+
, tableCount :: !Int -- ^ Number of tables in the benchmark
659+
, initialSize :: !Int
660+
}
661+
deriving stock Show
662+
663+
data RunOpts = RunOpts
664+
{ batchCount :: !Int
665+
, batchSize :: !Int
666+
, check :: !Bool
667+
, seed :: !Word64
668+
, pipelined :: !Bool
669+
, payRate :: !PaymentRate
670+
}
671+
deriving stock Show
672+
-}
673+
622674
-------------------------------------------------------------------------------
623675
-- sequential
624676
-------------------------------------------------------------------------------
@@ -913,6 +965,7 @@ main = do
913965
putStrLn " To benchmark in release mode, pass:"
914966
putStrLn " --project-file=cabal.project.release"
915967
#endif
968+
getArgs >>= Fold.traverse_ print
916969
(gopts, cmd) <- O.customExecParser prefs cliP
917970
print gopts
918971
print cmd

0 commit comments

Comments
 (0)