Debian Codegraph @

benchmarks/HtmlBenchmarks.hs

raw symbols SCIP
1 -- | This is a collection of HTML benchmarks for BlazeMarkup.
2
3 --
4 {-# LANGUAGE ExtendedDefaultRules #-}
5 {-# LANGUAGE OverloadedStrings, ExistentialQuantification #-}
6 {-# OPTIONS_GHC -fno-warn-unused-do-bind #-}
7 module HtmlBenchmarks where
8
9 import Data.Monoid (Monoid,mappend,mempty)
10 import Data.Text (Text)
11 import qualified Data.Text as T
12 -- import qualified Data.Text.Lazy.Builder as B
13
14 import qualified Prelude as P
15 import Prelude hiding (div, id)
16
17 -- import BenchmarkUtils
18 import Lucid
19 import Lucid.Base
20 -- import qualified BenchmarkUtils as H
21
22 -- | Description of an HTML benchmark
23 --
24 data HtmlBenchmark = forall a. HtmlBenchmark
25 String -- ^ Name.
26 (a -> Html ()) -- ^ Rendering function.
27 a -- ^ Data.
28 (Html ()) -- ^ Longer description.
29
30 -- | List containing all benchmarks.
31 --
32 benchmarks :: [HtmlBenchmark]
33 benchmarks =
34 [ HtmlBenchmark "bigTable" bigTable bigTableData $
35 let h = toHtml $ show $ length bigTableData
36 w = toHtml $ show $ length $ P.head bigTableData
37 in "Rendering of a big (" >> h >> "x" >> w >> ") HTML table"
38 , HtmlBenchmark "basic" basic basicData
39 "A simple, small basic template with a few holes to fill in"
40 , HtmlBenchmark "wideTree" wideTree wideTreeData $
41 "A very wide tree (" >> toHtml (show (length wideTreeData)) >> " elements)"
42 , HtmlBenchmark "wideTreeEscaping" wideTree wideTreeEscapingData $ do
43 "A very wide tree (" >> toHtml (show (length wideTreeEscapingData)) >> " elements)"
44 " with lots of escaping"
45 , HtmlBenchmark "deepTree" deepTree deepTreeData $ do
46 "A really deep tree (" >> toHtml (show deepTreeData) >> " nested templates)"
47 , HtmlBenchmark "manyAttributes" manyAttributes manyAttributesData $ do
48 "A single element with " >> toHtml (show (length manyAttributesData))
49 " distinct attributes."
50 , HtmlBenchmark "duplicateAttributes" duplicateAttributes duplicateAttributesData $ do
51 "A single element with a single attribute and " >> toHtml (show (length duplicateAttributesData))
52 " values."
53 , HtmlBenchmark "customAttribute" customAttributes customAttributesData $
54 "Creating custom attributes (middle ground between manyAttributes and duplicateAttributes)"
55
56 ]
57
58 rows :: Int
59 rows = 1000
60
61 bigTableData :: [[Int]]
62 bigTableData = replicate rows [1..10]
63 {-# NOINLINE bigTableData #-}
64
65 basicData :: (Text, Text, [Text])
66 basicData = ("Just a test", "joe", items)
67 {-# NOINLINE basicData #-}
68
69 items :: [Text]
70 items = map (("Number " `mappend`) . T.pack . show) [1 :: Int .. 14]
71 {-# NOINLINE items #-}
72
73 wideTreeData :: [Text]
74 wideTreeData = take 5000 $
75 cycle ["λf.(λx.fxx)(λx.fxx)", "These old days", "Foobar", "lol", "x ∈ A"]
76 {-# NOINLINE wideTreeData #-}
77
78 wideTreeEscapingData :: [Text]
79 wideTreeEscapingData = take 1000 $
80 cycle ["<><>", "\"lol\"", "<&>", "'>>'"]
81 {-# NOINLINE wideTreeEscapingData #-}
82
83 deepTreeData :: Int
84 deepTreeData = 1000
85 {-# NOINLINE deepTreeData #-}
86
87 manyAttributesData :: [(T.Text, T.Text)]
88 manyAttributesData = zipWith mk [0 ..] wideTreeData where
89 mk :: Int -> T.Text -> (T.Text, T.Text)
90 mk i val = (T.pack ("attr" ++ show i), val)
91
92 duplicateAttributesData :: [Text]
93 duplicateAttributesData = wideTreeData
94
95 customAttributesData :: [(Text, Text)]
96 customAttributesData = zip wideTreeData wideTreeData
97
98 -- | Render the argument matrix as an HTML table.
99 --
100 bigTable :: [[Int]] -- ^ Matrix.
101 -> Html () -- ^ Result.
102 bigTable t = table_ (mapM_ row t)
103
104 row :: [Int] -> Html ()
105 row r = tr_ (mapM_ (td_ . toHtml . show) r)
106
107 -- | Render a simple HTML page with some data.
108 --
109 basic :: (Text, Text, [Text]) -- ^ (Title, User, Items)
110 -> Html () -- ^ Result.
111 basic (title', user, items') = html_ $ do
112 head_ $ title_ $ toHtml title'
113 body_ $ do
114 with div_ [id_ "header"] $ (h1_ $ toHtml title')
115 p_ $ do "Hello, "; toHtml user; "!"
116 p_ $ "Hello, me!"
117 p_ $ "Hello, world!"
118 h2_ $ "loop"
119 ol_ $ mapM_ (li_ . toHtml) items'
120 with div_ [id_ "footer"] mempty
121
122 -- | A benchmark producing a very wide but very shallow tree.
123 --
124 wideTree :: [Text] -- ^ Text to create a tree from.
125 -> Html () -- ^ Result.
126 wideTree = div_ . mapM_ ((with p_ [id_ "foo"]) . toHtml)
127
128 -- | Create a very deep tree.
129 --
130 deepTree :: Int -- ^ Depth of the tree.
131 -> Html () -- ^ Result.
132 deepTree 0 = "foo"
133 deepTree n = p_ $ table_ $ tr_ $ td_ $ div_ $ deepTree (n - 1)
134
135 -- | Create an element with many attributes.
136 --
137 manyAttributes :: [(T.Text, T.Text)] -- ^ List of attribute values.
138 -> Html () -- ^ Result.
139 manyAttributes as = img_ (map (\(key, val) -> makeAttribute key val) as)
140
141 duplicateAttributes :: [Text] -- ^ List of attribute values.
142 -> Html () -- ^ Result.
143 duplicateAttributes as = img_ (map id_ as)
144
145 customAttributes :: [(Text, Text)] -- ^ List of attribute name, value pairs
146 -> Html () -- ^ Result
147 customAttributes xs =
148 img_ (map (\(key,val) -> makeAttribute key val) xs)