|
| 1 | +-- | # Benchmarking |
| 2 | +-- | |
| 3 | +-- | spago -x spago-dev.dhall run --main Bench.Main |
| 4 | +-- | |
| 5 | +-- | This benchmark suite is intended to guide changes to this package so that |
| 6 | +-- | we can compare the benchmarks of different commits. |
| 7 | +-- | |
| 8 | +-- | This benchmark suite also compares parsers to equivalent Regex. This |
| 9 | +-- | provides an answer to the common question “How much slower is this package |
| 10 | +-- | than Regex?” Answer: approximately 100×. The Regex benchmarks also give |
| 11 | +-- | us a rough way to calibrate benchmarks run on different platforms. |
| 12 | +-- | |
| 13 | +-- | # Profiling |
| 14 | +-- | |
| 15 | +-- | https://nodejs.org/en/docs/guides/simple-profiling/ |
| 16 | +-- | https://nodesource.com/blog/diagnostics-in-NodeJS-2 |
| 17 | +-- | |
| 18 | +-- | spago -x spago-dev.dhall build --source-maps |
| 19 | +-- | purs bundle output/**/*.js --source-maps --output ./index.bundle.js |
| 20 | +-- | |
| 21 | +-- | |
| 22 | +-- | spago -x spago-dev.dhall build --source-maps --purs-args '--codegen corefn,sourcemaps' |
| 23 | +-- | zephyr Bench.Main.main --codegen sourcemaps,js |
| 24 | +-- | purs bundle dce-output/**/*.js --source-maps --module Bench.Main --main Bench.Main --output ./index.dce.bundle.js |
| 25 | +-- | node index.dce.bundle.js |
| 26 | +-- | |
| 27 | +-- | spago -x spago-dev.dhall build --source-maps --purs-args '--codegen corefn,sourcemaps' |
| 28 | +-- | purs bundle output/**/*.js --source-maps --module Bench.Main --main Bench.Main --output ./index.bundle.js |
| 29 | +-- | node index.bundle.js |
| 30 | +-- | node --prof --enable-source-maps ./index.bundle.js |
| 31 | +-- | node --prof-process --source-map ./index.bundle.js.map isolate--.log > prof.txt |
| 32 | +-- | |
| 33 | +-- | node --prof --enable-source-maps -e 'require("./output/Bench.Main/index.js").main()' |
| 34 | +-- | node --prof-process isolate--.log |
| 35 | +-- | |
| 36 | +-- | spago -x spago-dev.dhall build |
| 37 | +-- | node --prof -e 'require("./output/Bench.Main/index.js").main()' |
| 38 | +-- | node --prof-process isolate--.log > prof.txt |
| 39 | + |
| 40 | +module Bench.Main where |
| 41 | + |
| 42 | +import Prelude |
| 43 | + |
| 44 | +import Data.Array (fold, replicate) |
| 45 | +import Data.Either (either) |
| 46 | +import Data.List (manyRec) |
| 47 | +import Data.List.Types (List) |
| 48 | +import Data.String.Regex (Regex, regex) |
| 49 | +import Data.String.Regex as Regex |
| 50 | +import Data.String.Regex.Flags (RegexFlags(..)) |
| 51 | +import Effect (Effect) |
| 52 | +import Effect.Console (log) |
| 53 | +import Effect.Exception (throw) |
| 54 | +import Effect.Unsafe (unsafePerformEffect) |
| 55 | +import Performance.Minibench (benchWith) |
| 56 | +import Text.Parsing.Parser (Parser, runParser) |
| 57 | +import Text.Parsing.Parser.String (string) |
| 58 | +import Text.Parsing.Parser.Token (digit) |
| 59 | +import Text.Parsing.StringParser as StringParser |
| 60 | +import Text.Parsing.StringParser.CodePoints as StringParser.CodePoints |
| 61 | +import Text.Parsing.StringParser.CodeUnits as StringParser.CodeUnits |
| 62 | + |
| 63 | +string23 :: String |
| 64 | +string23 = "23" |
| 65 | +string23_2 :: String |
| 66 | +string23_2 = fold $ replicate 2 string23 |
| 67 | +string23_10000 :: String |
| 68 | +string23_10000 = fold $ replicate 10000 string23 |
| 69 | + |
| 70 | +stringSkidoo :: String |
| 71 | +stringSkidoo = "skidoo" |
| 72 | +stringSkidoo_2 :: String |
| 73 | +stringSkidoo_2 = fold $ replicate 2 stringSkidoo |
| 74 | +stringSkidoo_10000 :: String |
| 75 | +stringSkidoo_10000 = fold $ replicate 10000 stringSkidoo |
| 76 | + |
| 77 | +parse23 :: Parser String (List Char) |
| 78 | +parse23 = manyRec digit |
| 79 | + |
| 80 | +parse23Points :: StringParser.Parser (List Char) |
| 81 | +parse23Points = manyRec StringParser.CodePoints.anyDigit |
| 82 | + |
| 83 | +parse23Units :: StringParser.Parser (List Char) |
| 84 | +parse23Units = manyRec StringParser.CodeUnits.anyDigit |
| 85 | + |
| 86 | +pattern23 :: Regex |
| 87 | +pattern23 = either (unsafePerformEffect <<< throw) identity $ |
| 88 | + regex "\\d" $ RegexFlags |
| 89 | + { dotAll: true |
| 90 | + , global: true |
| 91 | + , ignoreCase: false |
| 92 | + , multiline: true |
| 93 | + , sticky: false |
| 94 | + , unicode: true |
| 95 | + } |
| 96 | + |
| 97 | +parseSkidoo :: Parser String (List String) |
| 98 | +parseSkidoo = manyRec $ string "skidoo" |
| 99 | + |
| 100 | +patternSkidoo :: Regex |
| 101 | +patternSkidoo = either (unsafePerformEffect <<< throw) identity $ |
| 102 | + regex "skidoo" $ RegexFlags |
| 103 | + { dotAll: true |
| 104 | + , global: true |
| 105 | + , ignoreCase: false |
| 106 | + , multiline: true |
| 107 | + , sticky: false |
| 108 | + , unicode: true |
| 109 | + } |
| 110 | + |
| 111 | +main :: Effect Unit |
| 112 | +main = do |
| 113 | + -- log $ show $ runParser string23_2 parse23 |
| 114 | + -- log $ show $ Regex.match pattern23 string23_2 |
| 115 | + -- log $ show $ runParser stringSkidoo_2 parseSkidoo |
| 116 | + -- log $ show $ Regex.match patternSkidoo stringSkidoo_2 |
| 117 | + log "runParser parse23" |
| 118 | + benchWith 200 |
| 119 | + $ \_ -> runParser string23_10000 parse23 |
| 120 | + log "StringParser.runParser parse23Points" |
| 121 | + benchWith 20 |
| 122 | + $ \_ -> StringParser.runParser parse23Points string23_10000 |
| 123 | + log "StringParser.runParser parse23Units" |
| 124 | + benchWith 200 |
| 125 | + $ \_ -> StringParser.runParser parse23Units string23_10000 |
| 126 | + log "Regex.match pattern23" |
| 127 | + benchWith 200 |
| 128 | + $ \_ -> Regex.match pattern23 string23_10000 |
| 129 | + log "runParser parseSkidoo" |
| 130 | + benchWith 200 |
| 131 | + $ \_ -> runParser stringSkidoo_10000 parseSkidoo |
| 132 | + log "Regex.match patternSkidoo" |
| 133 | + benchWith 200 |
| 134 | + $ \_ -> Regex.match patternSkidoo stringSkidoo_10000 |
0 commit comments