graphql-engine/server/lib/graphql-parser-hs/bench/Benchmark.hs
kodiakhq[bot] c5de79d10c server: merge graphql-parser-hs into the monorepo
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5906
Co-authored-by: José Lorenzo Rodríguez <37621+lorenzo@users.noreply.github.com>
Co-authored-by: Vitali Barozzi <26206141+vitalibarozzi@users.noreply.github.com>
Co-authored-by: Karthikeyan Chinnakonda <15602904+codingkarthik@users.noreply.github.com>
Co-authored-by: Auke Booij <164426+abooij@users.noreply.github.com>
Co-authored-by: jkachmar <8461423+jkachmar@users.noreply.github.com>
Co-authored-by: Antoine Leblanc <1618949+nicuveo@users.noreply.github.com>
Co-authored-by: Solomon <24038+solomon-b@users.noreply.github.com>
Co-authored-by: paritosh-08 <85472423+paritosh-08@users.noreply.github.com>
Co-authored-by: Evie Ciobanu <1017953+eviefp@users.noreply.github.com>
Co-authored-by: Vamshi Surabhi <6562944+0x777@users.noreply.github.com>
Co-authored-by: Samir Talwar <47582+SamirTalwar@users.noreply.github.com>
GitOrigin-RevId: 4864173fd616fbbb2fe3a839d0c5eabd76a2e1f2
2022-09-26 11:23:37 +00:00

85 lines
2.7 KiB
Haskell

module Main
( main,
)
where
-------------------------------------------------------------------------------
import Data.Bifunctor (second)
import Data.ByteString.Builder qualified as BS
import Data.Function ((&))
import Data.Maybe (mapMaybe)
import Data.Text (Text)
import Data.Text.Lazy.Builder qualified as LTB
import Data.Traversable (for)
import Language.GraphQL.Draft.Generator (genExecutableDocument, genText, generate)
import Language.GraphQL.Draft.Parser (parseExecutableDoc)
import Language.GraphQL.Draft.Printer (executableDocument, renderExecutableDoc)
import Language.GraphQL.Draft.Syntax (ExecutableDocument, Name, mkName)
import Prettyprinter qualified as PP
import Prettyprinter.Render.Text qualified as PP
import Test.Tasty.Bench (bench, bgroup, defaultMain, nf, whnf)
import Text.Builder qualified as STB -- Strict Text Builder
import Prelude
-------------------------------------------------------------------------------
genDocs :: Int -> IO [(Int, ExecutableDocument Name)]
genDocs num =
for [1 .. num] $ \n -> (n,) <$> generate genExecutableDocument
genTexts :: Int -> IO [(Int, [Text])]
genTexts num =
for [1 .. num] $ \n -> do
texts <- for [1 .. 500 :: Int] \_ -> generate genText
pure (n, texts)
main :: IO ()
main = do
docs <- genDocs 10
texts <- genTexts 10
let grp1 = mkPPGrp docs
grp2 = mkBBGrp docs
grp3 = mkTBGrp docs
grp4 = mkTLBGrp docs
renderedDocs = map (second renderExecutableDoc) docs
grp5 = mkPGrp renderedDocs
grp6 = mkNGrp texts
defaultMain [grp1, grp2, grp3, grp4, grp5, grp6]
where
mkNGrp texts =
bgroup "checking name validity" $
texts & map \(n, t) ->
bench (show n) $ nf (length . mapMaybe mkName) t
mkPGrp qs =
bgroup "parsing executableDocument" $
qs & map \(n, q) ->
bench (show n) $ whnf parseExecutableDoc q
mkPPGrp gqs =
bgroup "rendering executableDocument (prettyprinter)" $
gqs & map \(n, gq) ->
bench (show n) $ nf (renderPP . executableDocument) gq
mkBBGrp gqs =
bgroup "rendering executableDocument (bytestring builder)" $
gqs & map \(n, gq) ->
bench (show n) $ nf (renderBB . executableDocument) gq
mkTBGrp gqs =
bgroup "rendering executableDocument (text builder)" $
gqs & map \(n, gq) ->
bench (show n) $ nf (renderTB . executableDocument) gq
mkTLBGrp gqs =
bgroup "rendering executableDocument (lazy text builder)" $
gqs & map \(n, gq) ->
bench (show n) $ nf (renderTLB . executableDocument) gq
renderPP :: PP.Doc Text -> Text
renderPP = PP.renderStrict . PP.layoutPretty PP.defaultLayoutOptions
renderBB = BS.toLazyByteString
renderTB = STB.run
renderTLB = LTB.toLazyText