| 1 | #!/usr/bin/env bash
|
| 2 | #
|
| 3 | # Benchmarks for YSH JSON
|
| 4 | #
|
| 5 | # Usage:
|
| 6 | # benchmarks/ysh-for.sh <function name>
|
| 7 | #
|
| 8 | # TODO: All of these can use BYO
|
| 9 | #
|
| 10 | # - benchmarks/ysh-json
|
| 11 | # - benchmarks/io/read-lines.sh # buffered and unbuffered, not hooked up
|
| 12 | # - benchmarks/compute
|
| 13 | # - control-flow with exceptions - slower than other shells
|
| 14 | # - word-split - faster
|
| 15 | #
|
| 16 | # Also:
|
| 17 | # - ovm-build should be added to CI
|
| 18 |
|
| 19 | set -o nounset
|
| 20 | set -o pipefail
|
| 21 | set -o errexit
|
| 22 |
|
| 23 | YSH=_bin/cxx-opt/ysh
|
| 24 | OSH=_bin/cxx-opt/osh
|
| 25 |
|
| 26 | readonly JSON_FILE=_tmp/github-issues.json
|
| 27 | readonly BIG_FILE=_tmp/compute/big.json
|
| 28 |
|
| 29 | fetch-issues() {
|
| 30 | # only gets 25 issues by default
|
| 31 | # https://docs.github.com/en/rest/issues?apiVersion=2022-11-28
|
| 32 |
|
| 33 | local n=${1:-100}
|
| 34 | local url="https://api.github.com/repos/oils-for-unix/oils/issues?per_page=$n"
|
| 35 |
|
| 36 | curl $url > $JSON_FILE
|
| 37 |
|
| 38 | ls -l -h $JSON_FILE
|
| 39 |
|
| 40 | jq \
|
| 41 | -s \
|
| 42 | --arg n "$n" \
|
| 43 | '. as $original | [range(0; $n|tonumber)] | map($original) | flatten' \
|
| 44 | $JSON_FILE > $BIG_FILE
|
| 45 |
|
| 46 | ls -l -h $BIG_FILE
|
| 47 | }
|
| 48 |
|
| 49 | with-ysh() {
|
| 50 | local n=${1:-100}
|
| 51 | ninja $YSH
|
| 52 |
|
| 53 | if false; then
|
| 54 |
|
| 55 | time $YSH -c '
|
| 56 | var n = int($2)
|
| 57 | for i in (1 ..= n) {
|
| 58 | json read < $1
|
| 59 | }
|
| 60 | ' dummy $JSON_FILE $n
|
| 61 | fi
|
| 62 |
|
| 63 | echo ' YSH'
|
| 64 | time $YSH -c 'json read < $1' dummy $BIG_FILE
|
| 65 | }
|
| 66 |
|
| 67 | with-cpython() {
|
| 68 | local n=${1:-100}
|
| 69 |
|
| 70 | if false; then
|
| 71 | local prog='
|
| 72 | import json
|
| 73 | import sys
|
| 74 | n = int(sys.argv[2])
|
| 75 | for i in range(n):
|
| 76 | with open(sys.argv[1]) as f:
|
| 77 | json.load(f)
|
| 78 | '
|
| 79 |
|
| 80 | echo 'PY2'
|
| 81 | # 391 ms
|
| 82 | time python2 -c "$prog" $JSON_FILE $n
|
| 83 |
|
| 84 | echo 'PY3'
|
| 85 | # 175 ms
|
| 86 | time python3 -c "$prog" $JSON_FILE $n
|
| 87 | fi
|
| 88 |
|
| 89 | local prog='
|
| 90 | import json
|
| 91 | import sys
|
| 92 | with open(sys.argv[1]) as f:
|
| 93 | json.load(f)
|
| 94 | '
|
| 95 |
|
| 96 | echo 'PY2'
|
| 97 | # 391 ms
|
| 98 | time python2 -c "$prog" $BIG_FILE
|
| 99 |
|
| 100 | echo 'PY3'
|
| 101 | # 175 ms
|
| 102 | time python3 -c "$prog" $BIG_FILE
|
| 103 | }
|
| 104 |
|
| 105 | with-js() {
|
| 106 | local n=${1:-100}
|
| 107 |
|
| 108 | if false; then
|
| 109 | # 195 ms, minus ~100 ms startup time = 90 ms
|
| 110 | time nodejs -e '
|
| 111 | var fs = require("fs")
|
| 112 | var filename = process.argv[1];
|
| 113 | var n = process.argv[2];
|
| 114 |
|
| 115 | //console.log("FILE " + filename);
|
| 116 | var json = fs.readFileSync(filename, "utf-8");
|
| 117 |
|
| 118 | for (let i = 0; i < n; ++i) {
|
| 119 | JSON.parse(json)
|
| 120 | }
|
| 121 | ' $JSON_FILE $n
|
| 122 | fi
|
| 123 |
|
| 124 | time nodejs -e '
|
| 125 | var fs = require("fs")
|
| 126 | var filename = process.argv[1];
|
| 127 |
|
| 128 | //console.log("FILE " + filename);
|
| 129 | var json = fs.readFileSync(filename, "utf-8");
|
| 130 |
|
| 131 | JSON.parse(json)
|
| 132 | ' $BIG_FILE
|
| 133 |
|
| 134 | # 100 ms startup time is misleading
|
| 135 | #time nodejs -e 'console.log("hi")'
|
| 136 | }
|
| 137 |
|
| 138 | with-jq() {
|
| 139 | local n=${1:-100}
|
| 140 |
|
| 141 | wc -l $JSON_FILE
|
| 142 |
|
| 143 | time jq '. | length' $BIG_FILE >/dev/null
|
| 144 | }
|
| 145 |
|
| 146 |
|
| 147 | compare() {
|
| 148 | local n=${1:-100}
|
| 149 | local OILS_GC_STATS=${2:-}
|
| 150 |
|
| 151 | ninja $OSH $YSH
|
| 152 |
|
| 153 | for bin in ysh cpython js jq; do
|
| 154 | echo "=== $bin ==="
|
| 155 | with-$bin $n
|
| 156 | echo
|
| 157 | done
|
| 158 | }
|
| 159 |
|
| 160 | "$@"
|