pandoc/benchmark/benchmark-pandoc.hs

113 lines
4.4 KiB
Haskell
Raw Normal View History

{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TupleSections #-}
{-
Copyright (C) 2012-2019 John MacFarlane <jgm@berkeley.edu>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-}
import Prelude
import Text.Pandoc
2019-08-25 22:58:29 +02:00
import Text.Pandoc.MIME
2018-10-14 08:10:27 +02:00
import Text.Pandoc.Error (PandocError(..))
import Control.Monad.Except (throwError)
import qualified Text.Pandoc.UTF8 as UTF8
2017-01-27 11:15:45 +01:00
import qualified Data.ByteString as B
import qualified Data.Text as T
import Criterion.Main
2015-10-10 01:01:08 +02:00
import Criterion.Types (Config(..))
import Data.List (intersect)
import Data.Maybe (mapMaybe)
import System.Environment (getArgs)
2019-08-25 22:58:29 +02:00
import qualified Data.ByteString.Lazy as BL
readerBench :: Pandoc
-> T.Text
-> Maybe Benchmark
readerBench doc name =
case res of
Right (readerFun, inp) ->
Just $ bench (T.unpack $ name <> " reader")
$ nf (\i -> either (error . show) id $ runPure (readerFun i))
inp
Left _ -> Nothing
2019-09-30 03:29:49 +02:00
where res = runPure $ do
(rdr, rexts) <- getReader name
(wtr, wexts) <- getWriter name
case (rdr, wtr) of
(TextReader r, TextWriter w) -> do
setResourcePath ["../test"]
inp <- w def{ writerWrapText = WrapAuto
, writerExtensions = wexts } doc
return $ (r def{ readerExtensions = rexts }, inp)
_ -> throwError $ PandocSomeError $ "not a text format: "
<> name
2019-08-25 22:58:29 +02:00
getImages :: IO [(FilePath, MimeType, BL.ByteString)]
getImages = do
ll <- BL.readFile "test/lalune.jpg"
mv <- BL.readFile "test/movie.jpg"
return [("lalune.jpg", "image/jpg", ll)
,("movie.jpg", "image/jpg", mv)]
writerBench :: Pandoc
-> T.Text
-> Maybe Benchmark
writerBench doc name =
case res of
Right writerFun ->
2019-08-25 22:58:29 +02:00
Just $ env getImages $ \imgs ->
bench (T.unpack $ name <> " writer")
$ nf (\d -> either (error . show) id $
2019-08-25 22:58:29 +02:00
runPure (do mapM_
(\(fp, mt, bs) ->
insertMedia fp (Just mt) bs)
imgs
writerFun d)) doc
2018-10-14 08:10:27 +02:00
Left _ -> Nothing
where res = runPure $ do
2019-09-30 03:29:49 +02:00
(wtr, wexts) <- getWriter name
case wtr of
TextWriter w ->
2018-10-14 08:10:27 +02:00
return $ w def{ writerExtensions = wexts }
_ -> throwError $ PandocSomeError
$ "could not get text writer for " <> name
2012-07-26 19:02:00 +02:00
main :: IO ()
main = do
args <- filter (\x -> T.take 1 x /= "-") . fmap T.pack <$> getArgs
print args
let matchReader (n, TextReader _) =
null args || ("reader" `elem` args && n `elem` args)
matchReader _ = False
let matchWriter (n, TextWriter _) =
null args || ("writer" `elem` args && n `elem` args)
matchWriter _ = False
let matchedReaders = map fst $ (filter matchReader readers
:: [(T.Text, Reader PandocPure)])
let matchedWriters = map fst $ (filter matchWriter writers
:: [(T.Text, Writer PandocPure)])
inp <- UTF8.toText <$> B.readFile "test/testsuite.txt"
2017-01-27 11:15:45 +01:00
let opts = def
let doc = either (error . show) id $ runPure $ readMarkdown opts inp
let readerBs = mapMaybe (readerBench doc)
$ filter (/="haddock")
(matchedReaders `intersect` matchedWriters)
-- we need the corresponding writer to generate
-- input for the reader
let writerBs = mapMaybe (writerBench doc) matchedWriters
2015-10-10 01:01:08 +02:00
defaultMainWith defaultConfig{ timeLimit = 6.0 }
(writerBs ++ readerBs)