aboutsummaryrefslogtreecommitdiff
path: root/benchmark/weigh-pandoc.hs
blob: ad4c83ad73eedf9ae9e25d12298fa570a28e5dd7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{- |
   Module      : Main
   Copyright   : © 2016-2021 John MacFarlane <jgm@berkeley.edu>
   License     : GNU GPL, version 2 or above

   Maintainer  : John MacFarlane <jgm@berkeley.edu>
   Stability   : alpha
   Portability : portable

Benchmarks to determine resource use of readers and writers.
-}
import Prelude
import Weigh
import Text.Pandoc
import Data.Text (Text, unpack)

main :: IO ()
main = do
  doc <- read <$> readFile "test/testsuite.native"
  mainWith $ do
    func "Pandoc document" id doc
    mapM_
      (\(n,r) -> weighReader doc n (either (error . show) id . runPure . r def{readerExtensions = pandocExtensions}))
      [("markdown", readMarkdown)
      ,("html", readHtml)
      ,("docbook", readDocBook)
      ,("latex", readLaTeX)
      ,("commonmark", readCommonMark)
      ]
    mapM_
      (\(n,w) -> weighWriter doc n (either (error . show) id . runPure . w def))
      [("markdown", writeMarkdown)
      ,("html", writeHtml5String)
      ,("docbook", writeDocbook5)
      ,("latex", writeLaTeX)
      ,("commonmark", writeCommonMark)
      ]

weighWriter :: Pandoc -> String -> (Pandoc -> Text) -> Weigh ()
weighWriter doc name writer = func (name ++ " writer") writer doc

weighReader :: Pandoc -> Text -> (Text -> Pandoc) -> Weigh ()
weighReader doc name reader =
  case lookup name writers of
       Just (TextWriter writer) ->
         let inp = either (error . show) id $ runPure $ writer def{ writerWrapText = WrapAuto} doc
         in func (unpack $ name <> " reader") reader inp
       _ -> return () -- no writer for reader