1{-# LANGUAGE OverloadedStrings #-}
2
3module Main (main) where
4
5import Criterion.Main
6import Data.ByteString (ByteString)
7import Data.Maybe (fromJust)
8import Data.String (IsString (..))
9import Data.Text (Text)
10import Data.Void
11import Text.Megaparsec
12import Text.URI (URI)
13import qualified Text.URI as URI
14
15main :: IO ()
16main = defaultMain
17  [ bparser    turiStr
18  , bparserBs  turiStr
19  , brender    turi
20  , brenderBs  turi
21  , brenderStr turi ]
22
23----------------------------------------------------------------------------
24-- Helpers
25
26-- | Test 'URI' as a polymorphic string.
27
28turiStr :: IsString a => a
29turiStr = "https://mark:secret@github.com:443/mrkkrp/modern-uri?foo=bar#fragment"
30
31-- | Test 'URI' in parsed form.
32
33turi :: URI
34turi = fromJust (URI.mkURI turiStr)
35
36-- | Benchmark speed of the 'URI' parser with given input.
37
38bparser :: Text -> Benchmark
39bparser txt = env (return txt) (bench "text parser" . nf p)
40  where
41    p = parse (URI.parser <* eof :: Parsec Void Text URI) ""
42
43-- | Like 'bparser' but accepts a 'ByteString'.
44
45bparserBs :: ByteString -> Benchmark
46bparserBs bs = env (return bs) (bench "bs parser" . nf p)
47  where
48    p = parse (URI.parserBs <* eof :: Parsec Void ByteString URI) ""
49
50-- | Benchmark speed of the 'URI' render with given input.
51
52brender :: URI -> Benchmark
53brender uri = env (return uri) (bench "text render" . nf URI.render)
54
55-- | The same as 'brender' but for the 'URI.renderBs' render.
56
57brenderBs :: URI -> Benchmark
58brenderBs uri = env (return uri) (bench "bs render" . nf URI.renderBs)
59
60-- | The same as 'brender' but for the 'URI.renderString' render.
61
62brenderStr :: URI -> Benchmark
63brenderStr uri = env (return uri) (bench "str render" . nf URI.renderStr)
64