From 09b9ebb0a85c417be2afe779578adca6b77f035c Mon Sep 17 00:00:00 2001 From: Devin Lehmacher Date: Sat, 12 Mar 2022 14:08:36 -0800 Subject: [PATCH] add benchmarks for SourceName escaping --- commonmark/benchmark/benchmark.hs | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/commonmark/benchmark/benchmark.hs b/commonmark/benchmark/benchmark.hs index 44c9441..625ad99 100644 --- a/commonmark/benchmark/benchmark.hs +++ b/commonmark/benchmark/benchmark.hs @@ -1,6 +1,8 @@ {-# LANGUAGE CPP #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE ScopedTypeVariables #-} +{-# LANGUAGE TypeApplications #-} +{-# LANGUAGE FlexibleContexts #-} import Test.Tasty.Bench import Data.Text (Text) import Data.Functor.Identity -- base >= 4.8 @@ -19,6 +21,8 @@ main = do ] , bgroup "pathological" (map toPathBench pathtests) + , bgroup "name impact" + (map (toNameImpactBench sample) nameImpactTests) ] toPathBench :: (String, Int -> T.Text) -> Benchmark @@ -88,11 +92,31 @@ pathtests = ("a" <> T.replicate num " (String, String) -> Benchmark +toNameImpactBench sample (testName, name) = + let benchArgs n = (show n, take (50 * n) (cycle name), sample) + in bgroup testName + (map (benchCommonmark' @SourceRange defaultSyntaxSpec . benchArgs) + [1, 5, 10, 20]) + +nameImpactTests :: [(String, String)] +nameImpactTests = + [ ("no special characters", "the quick brown fox jumps over the lazy dog") + , ("special characters", "\\-:-as;df-:d:%%-:\\;;;\\-:%%-:---:-sdf-:sa-\\;") + ] + benchCommonmark :: SyntaxSpec Identity (Html ()) (Html ()) -> (String, Text) -> Benchmark benchCommonmark spec (name, contents) = - bench name $ + benchCommonmark' spec (name, name, contents) + +benchCommonmark' :: Rangeable (Html a) + => SyntaxSpec Identity (Html a) (Html a) + -> (String, String, Text) + -> Benchmark +benchCommonmark' spec (testName, name, contents) = + bench testName $ nf (either (error . show) renderHtml . runIdentity . parseCommonmarkWith spec . tokenize name) contents