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-for
|
11 | # - benchmarks/ysh-json
|
12 | # - benchmarks/io/read-lines.sh # buffered and unbuffered, not hooked up
|
13 | # - benchmarks/compute
|
14 | # - control-flow with exceptions - slower than other shells
|
15 | # - word-split - faster
|
16 | #
|
17 | # Also:
|
18 | # - ovm-build should be added to CI
|
19 |
|
20 | set -o nounset
|
21 | set -o pipefail
|
22 | set -o errexit
|
23 |
|
24 | YSH=_bin/cxx-opt/ysh
|
25 | OSH=_bin/cxx-opt/osh
|
26 |
|
27 | readonly JSON_FILE=_tmp/github-issues.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 |
|
41 | with-ysh() {
|
42 | ninja $YSH
|
43 |
|
44 | # TODO: turn this into some bigger data
|
45 |
|
46 | # 262 ms
|
47 | time $YSH -c '
|
48 | for i in (1 ..= 1000) {
|
49 | json read < $1
|
50 | }
|
51 | ' dummy $JSON_FILE
|
52 | }
|
53 |
|
54 | with-cpython() {
|
55 | local prog='
|
56 | import json
|
57 | import sys
|
58 | for i in range(1000):
|
59 | with open(sys.argv[1]) as f:
|
60 | json.load(f)
|
61 | '
|
62 |
|
63 | # 391 ms
|
64 | time python2 -c "$prog" $JSON_FILE
|
65 |
|
66 | # 175 ms
|
67 | time python3 -c "$prog" $JSON_FILE
|
68 | }
|
69 |
|
70 | with-js() {
|
71 | # 195 ms, minus ~100 ms startup time = 90 ms
|
72 | time cat $JSON_FILE | nodejs -e '
|
73 | var fs = require("fs")
|
74 | var stdin = fs.readFileSync(0, "utf-8")
|
75 |
|
76 | for (let i = 0; i < 1000; ++i) {
|
77 | JSON.parse(stdin)
|
78 | }
|
79 | '
|
80 |
|
81 | # 100 ms startupt ime
|
82 | time nodejs -e 'console.log("hi")'
|
83 |
|
84 | }
|
85 |
|
86 | with-jq() {
|
87 | # TODO: make the data bigger
|
88 |
|
89 | # jq is also printing it here - we can take the length
|
90 | time jq '. | length' < $JSON_FILE
|
91 | }
|
92 |
|
93 |
|
94 | compare() {
|
95 | local n=${1:-1000000}
|
96 | local OILS_GC_STATS=${2:-}
|
97 |
|
98 | ninja $OSH $YSH
|
99 |
|
100 | for bin in ysh cpython js jq; do
|
101 | echo "=== $bin ==="
|
102 | with-$bin
|
103 | echo
|
104 | done
|
105 | }
|
106 |
|
107 | "$@"
|