{-# OPTIONS_GHC -fno-warn-orphans #-}{-# LANGUAGE OverloadedStrings #-}module Main whereimport Control.DeepSeqimport Criterion.Mainimport qualified Criterion.Main as Cimport Data.Text (unlines, Text, replicate, unpack)import Prelude hiding (unlines)import qualified Yi.Rope as FlongText :: TextlongText = force . Data.Text.unlines $ Prelude.replicate 1000 "Lorem Спасибопожалусто dolor 中文測試 amet"{-# NOINLINE longText #-}longTextTree :: F.YiStringlongTextTree = force . F.fromText . Data.Text.unlines $ Prelude.replicate 1000 "Lorem Спасибопожалусто dolor 中文測試 amet"{-# NOINLINE longTextTree #-}longFRope :: F.YiStringlongFRope = force (F.fromText longText){-# NOINLINE longFRope #-}wideText :: TextwideText = force . unlines $ Prelude.replicate 10 $ Data.Text.replicate 100 "Lorem Спасибопожалусто dolor 中文測試 amet "{-# NOINLINE wideText #-}shortText :: TextshortText = force . unlines $ Prelude.replicate 3 "Lorem Спасибопожалусто dolor 中文測試 amet"{-# NOINLINE shortText #-}tinyText :: TexttinyText = force $ "Lorem Спасибопожалусто dolor 中文測試 amet"{-# NOINLINE tinyText #-}wideFRope :: F.YiStringwideFRope = force (F.fromText wideText){-# NOINLINE wideFRope #-}benchOnText :: NFData b => a -> String -> (a -> b) -> BenchmarkbenchOnText text name f = C.bench name $ C.nf f textbenchSplitAt :: NFData a => a -> String -> (Int -> a -> (a, a)) -> C.BenchmarkbenchSplitAt text name f = C.bench name $ C.nf (\x -> Prelude.foldr ((fst .) . f) x [1000, 999 .. 1]) textbenchTakeDrop :: NFData a => a -> String -> (Int -> a -> a) -> C.BenchmarkbenchTakeDrop text name f = C.bench name $ C.nf (\x -> foldr f x [1000, 999 .. 1]) text-- | Chunk sizes to test with.chunkSizes :: [Int]chunkSizes = [1200]wideTexts :: (Int -> String, [(Int, F.YiString)])wideTexts = (\x -> "wide " ++ show x, mkTextSample wideText)longTexts :: (Int -> String, [(Int, F.YiString)])longTexts = (\x -> "long " ++ show x, mkTextSample longText)shortTexts :: (Int -> [Char], [(Int, F.YiString)])shortTexts = (\x -> "short " ++ show x, mkTextSample shortText)tinyTexts :: (Int -> String, [(Int, F.YiString)])tinyTexts = (\x -> "tiny " ++ show x, mkTextSample tinyText)mkTextSample :: Text -> [(Int, F.YiString)]mkTextSample s = force $ zipWith mkTexts chunkSizes (Prelude.repeat s) where mkTexts :: Int -> Text -> (Int, F.YiString) mkTexts x t = (x, F.fromText' x t)allTexts :: [(Int -> String, [(Int, F.YiString)])]allTexts = [longTexts {-, wideTexts, shortTexts, tinyTexts -}]allChars :: [(Int -> String, [(Int, Char)])]allChars = map mkChar "λa" where mkChar c = (\x -> unwords [ "char", [c], show x ], [(1, c)])-- | Sample usage:---- > mkGroup "drop" F.drop allTexts benchOnTextmkGroup :: String -- ^ Group name -> f -- ^ Function being benchmarked -> [(chsize -> String, [(chsize, input)])] -> (input -> String -> f -> Benchmark) -> BenchmarkmkGroup n f subs r = bgroup n tests where mkTest s (l, t) = r t (s l) f tests = Prelude.concat $ map (\(s, t) -> map (mkTest s) t) subsonTextGroup :: NFData a => String -> (F.YiString -> a) -> BenchmarkonTextGroup n f = mkGroup n f allTexts benchOnTextonCharGroup :: NFData a => String -> (Char -> a) -> BenchmarkonCharGroup n f = mkGroup n f allChars benchOnTextonIntGroup :: String -> (Int -> F.YiString -> F.YiString) -> BenchmarkonIntGroup n f = mkGroup n f allTexts benchTakeDroponSplitGroup :: String -> (Int -> F.YiString -> (F.YiString, F.YiString)) -> BenchmarkonSplitGroup n f = mkGroup n f allTexts benchSplitAtsplitBench :: [Benchmark]splitBench = [ onTextGroup "split none" (F.split (== '×')) , onTextGroup "split lots" (F.split (\x -> x == 'a' || x == 'o')) , onTextGroup "split all" (F.split (const True)) ]wordsBench :: [Benchmark]wordsBench = -- The replicate here inflates the benchmark like mad, should be -- moved out. [ onTextGroup "unwords" (\x -> F.unwords (Prelude.replicate 100 x)) , onTextGroup "words" F.words ]main :: IO ()main = defaultMain $ [ onIntGroup "drop" F.drop , onIntGroup "take" F.take , onTextGroup "cons" (F.cons 'λ') , onTextGroup "snoc" (`F.snoc` 'λ') , onCharGroup "singleton" F.singleton , onTextGroup "countNewLines" F.countNewLines , onTextGroup "lines" F.lines , onSplitGroup "splitAt" F.splitAt , onSplitGroup "splitAtLine" F.splitAtLine , onTextGroup "toReverseString" F.toReverseString , onTextGroup "toReverseText" F.toReverseText , onTextGroup "toText" F.toText , onTextGroup "length" F.length , onTextGroup "reverse" F.reverse , onTextGroup "null" F.null , onTextGroup "empty" $ const F.empty , onTextGroup "append" (\x -> F.append x x) , onTextGroup "concat x100" $ F.concat . Prelude.replicate 100 , onTextGroup "any OK, (== '中')" $ F.any (== '中') , onTextGroup "any bad, (== '×')" $ F.any (== '×') , onTextGroup "all OK (/= '×')" $ F.all (== '×') , onTextGroup "all bad, (== '中')" $ F.all (== '中') ] ++ splitBench ++ wordsBench