git.iko.soy/typeable/pandoc
on 0.10 help sign in
typeable/pandoc/benchmark/benchmark-pandoc.hs
46 lines 1.8 kB history raw
1import Text.Pandoc
2import Text.Pandoc.Shared (normalize)
3import Criterion.Main
4import Criterion.Config
5import Text.JSON.Generic
6import System.Environment (getArgs)
7import Data.Monoid
9readerBench :: Pandoc
10 -> (String, ReaderOptions -> String -> IO Pandoc)
11 -> Benchmark
12readerBench doc (name, reader) =
13 let writer = case lookup name writers of
14 Just (PureStringWriter w) -> w
15 _ -> error $ "Could not find writer for " ++ name
16 inp = writer def{ writerWrapText = True } doc
17 -- we compute the length to force full evaluation
18 getLength (Pandoc (Meta a b c) d) =
19 length a + length b + length c + length d
20 in bench (name ++ " reader") $ whnfIO $ getLength `fmap`
21 (reader def{ readerSmart = True }) inp
23writerBench :: Pandoc
24 -> (String, WriterOptions -> Pandoc -> String)
25 -> Benchmark
26writerBench doc (name, writer) = bench (name ++ " writer") $ nf
27 (writer def{ writerWrapText = True }) doc
29normalizeBench :: Pandoc -> [Benchmark]
30normalizeBench doc = [ bench "normalize - with" $ nf (encodeJSON . normalize) doc
31 , bench "normalize - without" $ nf encodeJSON doc
32 ]
34main :: IO ()
35main = do
36 args <- getArgs
37 (conf,_) <- parseArgs defaultConfig{ cfgSamples = Last $ Just 20 } defaultOptions args
38 inp <- readFile "README"
39 inp2 <- readFile "tests/testsuite.txt"
40 let opts = def{ readerSmart = True }
41 let doc = readMarkdown opts $ inp ++ unlines (drop 3 $ lines inp2)
42 let readerBs = map (readerBench doc) readers
43 let writers' = [(n,w) | (n, PureStringWriter w) <- writers]
44 defaultMainWith conf (return ()) $
45 map (writerBench doc) writers' ++ readerBs ++ normalizeBench doc