File: bench.hs

package info (click to toggle)
haskell-doctemplates 0.11.0.1-1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 288 kB
  • sloc: haskell: 1,223; makefile: 7
file content (38 lines) | stat: -rw-r--r-- 1,149 bytes parent folder | download | duplicates (3)
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
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE OverloadedStrings #-}
import Text.DocTemplates
import qualified Data.Text as T
import Data.Text (Text)
import Criterion.Main
import Criterion.Types (Config (..))
import Control.Monad.Identity
import Data.Semigroup ((<>))
import Data.Aeson (object, (.=), Value)
import Text.DocLayout (render)

main :: IO ()
main = do
  Right bigtextTemplate <- compileTemplate "bigtext.txt" bigtext
  defaultMainWith defaultConfig{ timeLimit = 5.0 } $
   [ bench "applyTemplate" $
      nf (fmap (render Nothing)
          . runIdentity . applyTemplate "bigtext" bigtext
          :: Value -> Either String Text)
        val
   , bench "renderTemplate" $
      nf (render Nothing . renderTemplate bigtextTemplate :: Value -> Text)
        val
   ]

bigtext :: Text
bigtext = T.replicate 150 $
  "Hello there $foo$. This is a big text.\n$for(bar)$$bar.baz$$endfor$\n"
  <> "$if(foo)$Hi $foo$.$endif$\n"

val :: Value
val = object [ "foo" .= (22 :: Int)
             , "bar" .= [ object [ "baz" .= ("Hello"::Text) ]
                        , object [ "baz" .= ("Bye"::Text) ]
                        ]
             ]