| 1 |
|
| 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 |
|
| 23 |
|
| 24 |
data HtmlBenchmark = forall a. HtmlBenchmark |
| 25 |
String |
| 26 |
(a -> Html ()) |
| 27 |
a |
| 28 |
(Html ()) |
| 29 |
|
| 30 |
|
| 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 |
|
| 99 |
|
| 100 |
bigTable :: [[Int]] |
| 101 |
-> Html () |
| 102 |
bigTable t = table_ (mapM_ row t) |
| 103 |
|
| 104 |
row :: [Int] -> Html () |
| 105 |
row r = tr_ (mapM_ (td_ . toHtml . show) r) |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
basic :: (Text, Text, [Text]) |
| 110 |
-> Html () |
| 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 |
|
| 123 |
|
| 124 |
wideTree :: [Text] |
| 125 |
-> Html () |
| 126 |
wideTree = div_ . mapM_ ((with p_ [id_ "foo"]) . toHtml) |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
deepTree :: Int |
| 131 |
-> Html () |
| 132 |
deepTree 0 = "foo" |
| 133 |
deepTree n = p_ $ table_ $ tr_ $ td_ $ div_ $ deepTree (n - 1) |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
manyAttributes :: [(T.Text, T.Text)] |
| 138 |
-> Html () |
| 139 |
manyAttributes as = img_ (map (\(key, val) -> makeAttribute key val) as) |
| 140 |
|
| 141 |
duplicateAttributes :: [Text] |
| 142 |
-> Html () |
| 143 |
duplicateAttributes as = img_ (map id_ as) |
| 144 |
|
| 145 |
customAttributes :: [(Text, Text)] |
| 146 |
-> Html () |
| 147 |
customAttributes xs = |
| 148 |
img_ (map (\(key,val) -> makeAttribute key val) xs) |