From b4fce5db596521f228f28b43985ba7706b6b8374 Mon Sep 17 00:00:00 2001 From: "John J. Camilleri" Date: Wed, 17 Feb 2021 11:44:00 +0100 Subject: [PATCH] Use envvars in benchmark for controlling PGF/LPGF. Add readme. --- testsuite/lpgf/README.md | 34 ++++++++++++++++++++++++++++++++++ testsuite/lpgf/bench.hs | 34 +++++++++++++++++++--------------- 2 files changed, 53 insertions(+), 15 deletions(-) create mode 100644 testsuite/lpgf/README.md diff --git a/testsuite/lpgf/README.md b/testsuite/lpgf/README.md new file mode 100644 index 000000000..67ed82486 --- /dev/null +++ b/testsuite/lpgf/README.md @@ -0,0 +1,34 @@ +# LPGF testsuite & benchmark + +## Test + +LPGF must be equivalent to PGF in terms of linearisation output. + +Possible exceptions: +- No handling of variants (design choice) +- Rendering of missing fucntions + +## Benchmark + +### Compilation + +Comparing PGF, LPGF along following criteria: + +- Time +- Memory +- Binary file size + +### Runtime (linearisation) + +Comparing PGF, PGF2, LPGF along following criteria: + +- Time +- Memory + +### Running + +``` +stack build --test --bench --no-run-tests --no-run-benchmarks && time stack bench +stack build --test --bench --no-run-tests --no-run-benchmarks && time PGF_ONLY=1 stack bench +stack build --test --bench --no-run-tests --no-run-benchmarks && time LPGF_ONLY=1 stack bench +``` diff --git a/testsuite/lpgf/bench.hs b/testsuite/lpgf/bench.hs index b178c6428..16e76b117 100644 --- a/testsuite/lpgf/bench.hs +++ b/testsuite/lpgf/bench.hs @@ -8,12 +8,14 @@ import PGF (PGF) import GF (compileToPGF, compileToLPGF, writePGF, writeLPGF) import GF.Support (Options, Flags (..), Verbosity (..), noOptions, addOptions, modifyFlags) +import Control.Monad (when) import qualified Data.List as L -import Data.Maybe (fromJust) +import Data.Maybe (fromJust, isNothing) import qualified Data.Map as Map import Data.Text (Text) import Data.Time.Clock (getCurrentTime, diffUTCTime) import System.Directory (listDirectory, getFileSize) +import System.Environment (lookupEnv) import System.FilePath ((), takeBaseName, takeExtension) import Text.Printf (printf) @@ -35,27 +37,29 @@ main = do mods <- map (dir ) . filter (\p -> grammarName `L.isPrefixOf` takeBaseName p && takeExtension p == ".gf") <$> listDirectory dir - - -- Compile - (pathPGF, pgf) <- time "compile PGF" (compilePGF mods) - (pathLPGF, lpgf) <- time "compile LPGF" (compileLPGF mods) - - -- Compare filesizes - sizePGF <- getFileSize pathPGF - sizeLPGF <- getFileSize pathLPGF - printf "- PGF size: %s\n" (convertSize sizePGF) - printf "- LPGF size: %s\n" (convertSize sizeLPGF) + printf "Found modules: %s\n" (unwords mods) -- Read trees lns <- lines <$> readFile (dir treesFile) let trees = map (fromJust . PGF.readExpr) lns printf "Read %d trees\n" (length trees) - -- Linearise - time "linearise PGF" (return $ length $ linPGF pgf trees) - time "linearise LPGF" (return $ length $ linLPGF lpgf trees) + doPGF <- isNothing <$> lookupEnv "LPGF_ONLY" + doLPGF <- isNothing <$> lookupEnv "PGF_ONLY" - return () + when doPGF $ do + (path, pgf) <- time "compile PGF" (compilePGF mods) + size <- getFileSize path + printf "- PGF size: %s\n" (convertSize size) + time "linearise PGF" (return $ length $ linPGF pgf trees) + return () + + when doLPGF $ do + (path, lpgf) <- time "compile LPGF" (compileLPGF mods) + size <- getFileSize path + printf "- LPGF size: %s\n" (convertSize size) + time "linearise LPGF" (return $ length $ linLPGF lpgf trees) + return () time :: String -> IO a -> IO a time desc io = do