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
39
40
41
42
43
44
{-# LANGUAGE TemplateHaskell #-}
module Main where
import Criterion.Main
import Data.Text.Glyphize
import FileEmbedLzma (embedLazyText, embedByteString)
import Data.FileEmbed (makeRelativeToProject)
import System.FilePath ((</>))
import qualified Data.Text.Foreign as Txt
import qualified Data.Text.Lazy as Txt
import Control.Parallel.Strategies (parMap, rdeepseq)
import Data.Word (Word8)
-- Benchmarking these as well...
import Foreign.Marshal.Array (peekArray, copyArray)
import Foreign.ForeignPtr (mallocForeignPtrArray, ForeignPtr)
import Foreign.Ptr (Ptr)
import Foreign.Storable (Storable(..))
import System.IO.Unsafe (unsafePerformIO)
shapeStr txt = shape font defaultBuffer { text = txt } []
where font = createFont $ createFace $(
makeRelativeToProject ("assets" </> "Lora-Regular.ttf") >>=
embedByteString) 0
dracula = $(makeRelativeToProject ("bench" </> "dracula.txt") >>= embedLazyText)
main = defaultMain [
bgroup "Dracula" [
bench "Week-Head" $ whnf shapeStr dracula,
bench "Normal Form" $ nf shapeStr dracula,
bench "Paragraphs" $ nf (map shapeStr) $ Txt.lines dracula,
bench "Parallelised" $ nf (parMap rdeepseq shapeStr) $ Txt.lines dracula
],
bgroup "building blocks" [
bench "peekArray (NF)" $ nfIO $ Txt.useAsPtr (Txt.toStrict dracula) $
\ptr l -> peekArray (fromEnum l) ptr,
bench "peekArray" $ whnfIO $ Txt.useAsPtr (Txt.toStrict dracula) $
\ptr l -> peekArray (fromEnum l) ptr,
bench "alloc foreign ptr" $ whnfIO (mallocForeignPtrArray $
fromEnum $ Txt.length dracula :: IO (ForeignPtr Word8))
]
]