1 -- | This is a collection of HTML benchmarks for BlazeMarkup.
3 {-# LANGUAGE OverloadedStrings, ExistentialQuantification, RankNTypes #-}
4 {-# OPTIONS_GHC -fno-warn-unused-do-bind #-}
5 module Benchmarks.BlazeTBenchmarks where
7 import Data.Monoid (Monoid, mempty, mconcat, mappend)
8 import Prelude hiding (div, id,map)
9 import qualified Prelude as P
12 import Text.BlazeT.Html5
13 import qualified Text.BlazeT.Html5 as H
14 import qualified Text.BlazeT.Html5.Attributes as A
16 -- | Description of an HTML benchmark
18 data HtmlBenchmark = forall a. HtmlBenchmark
20 (a -> Html) -- ^ Rendering function.
22 Html -- ^ Longer description.
24 -- | List containing all benchmarks.
26 benchmarks :: [HtmlBenchmark]
28 [ HtmlBenchmark "bigTable" bigTable bigTableData $
29 let h = toHtml $ length bigTableData
30 w = toHtml $ length $ P.head bigTableData
31 in (string "Rendering of a big (") >> h >> (string "x") >> w >> ") HTML table"
32 , HtmlBenchmark "basic" basic basicData
33 "A simple, small basic template with a few holes to fill in"
34 , HtmlBenchmark "wideTree" wideTree wideTreeData $
35 (string "A very wide tree (") >> toHtml (length wideTreeData) >> " elements)"
36 , HtmlBenchmark "wideTreeEscaping" wideTree wideTreeEscapingData $ do
37 (string "A very wide tree (") >> toHtml (length wideTreeData) >> (string " elements)")
38 " with lots of escaping"
39 , HtmlBenchmark "deepTree" deepTree deepTreeData $ do
40 (string "A really deep tree (") >> toHtml deepTreeData >> " nested templates)"
41 , HtmlBenchmark "manyAttributes" manyAttributes manyAttributesData $ do
42 (string "A single element with ") >> toHtml (length manyAttributesData)
44 , HtmlBenchmark "customAttribute" customAttributes customAttributesData $
45 "Creating custom attributes"
51 bigTableData :: [[Int]]
52 bigTableData = replicate rows [1..10]
53 {-# NOINLINE bigTableData #-}
55 basicData :: (String, String, [String])
56 basicData = ("Just a test", "joe", items)
57 {-# NOINLINE basicData #-}
60 items = fmap (("Number " `mappend`) . show) [1 :: Int .. 14]
61 {-# NOINLINE items #-}
63 wideTreeData :: [String]
64 wideTreeData = take 5000 $
65 cycle ["λf.(λx.fxx)(λx.fxx)", "These old days", "Foobar", "lol", "x ∈ A"]
66 {-# NOINLINE wideTreeData #-}
68 wideTreeEscapingData :: [String]
69 wideTreeEscapingData = take 1000 $
70 cycle ["<><>", "\"lol\"", "<&>", "'>>'"]
71 {-# NOINLINE wideTreeEscapingData #-}
75 {-# NOINLINE deepTreeData #-}
77 manyAttributesData :: [String]
78 manyAttributesData = wideTreeData
80 customAttributesData :: [(String, String)]
81 customAttributesData = zip wideTreeData wideTreeData
83 -- | Render the argument matrix as an HTML table.
85 bigTable :: [[Int]] -- ^ Matrix.
87 bigTable t = table $ mconcat $ fmap row t
89 row r = tr $ mconcat $ fmap (td . toHtml) r
91 -- | Render a simple HTML page with some data.
93 basic :: (String, String, [String]) -- ^ (Title, User, Items)
95 basic (title', user, items') = html $ do
96 H.head $ title $ toHtml title'
98 div ! A.id "header" $ (h1 $ toHtml title')
99 p $ "Hello, " `mappend` toHtml user `mappend` "!"
103 ol $ mconcat $ fmap (li . toHtml) items'
104 div ! A.id "footer" $ mempty
106 -- | A benchmark producing a very wide but very shallow tree.
108 wideTree :: [String] -- ^ Text to create a tree from.
110 wideTree = div . mapM_ ((p ! A.id "foo") . toHtml)
112 -- | Create a very deep tree.
114 deepTree :: Int -- ^ Depth of the tree.
117 deepTree n = p $ table $ tr $ td $ div $ deepTree (n - 1)
119 -- | Create an element with many attributes.
121 manyAttributes :: [String] -- ^ List of attribute values.
123 manyAttributes = foldl setAttribute img
125 setAttribute html' value' = html' ! A.id (toValue value')
126 {-# INLINE setAttribute #-}
128 customAttributes :: [(String, String)] -- ^ List of attribute name, value pairs
130 customAttributes = foldl setAttribute img
132 setAttribute html' (name, value') =
133 html' ! customAttribute (stringTag name) (toValue value')