OILS / benchmarks / ysh-json.sh View on Github | oils.pub

160 lines, 68 significant
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
19set -o nounset
20set -o pipefail
21set -o errexit
22
23YSH=_bin/cxx-opt/ysh
24OSH=_bin/cxx-opt/osh
25
26readonly JSON_FILE=_tmp/github-issues.json
27readonly BIG_FILE=_tmp/compute/big.json
28
29fetch-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
49with-ysh() {
50 local n=${1:-100}
51 ninja $YSH
52
53if 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
61fi
62
63 echo ' YSH'
64 time $YSH -c 'json read < $1' dummy $BIG_FILE
65}
66
67with-cpython() {
68 local n=${1:-100}
69
70if false; then
71 local prog='
72import json
73import sys
74n = int(sys.argv[2])
75for 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
87fi
88
89 local prog='
90import json
91import sys
92with 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
105with-js() {
106 local n=${1:-100}
107
108if false; then
109 # 195 ms, minus ~100 ms startup time = 90 ms
110 time nodejs -e '
111var fs = require("fs")
112var filename = process.argv[1];
113var n = process.argv[2];
114
115//console.log("FILE " + filename);
116var json = fs.readFileSync(filename, "utf-8");
117
118for (let i = 0; i < n; ++i) {
119 JSON.parse(json)
120}
121' $JSON_FILE $n
122fi
123
124 time nodejs -e '
125var fs = require("fs")
126var filename = process.argv[1];
127
128//console.log("FILE " + filename);
129var json = fs.readFileSync(filename, "utf-8");
130
131JSON.parse(json)
132' $BIG_FILE
133
134 # 100 ms startup time is misleading
135 #time nodejs -e 'console.log("hi")'
136}
137
138with-jq() {
139 local n=${1:-100}
140
141 wc -l $JSON_FILE
142
143 time jq '. | length' $BIG_FILE >/dev/null
144}
145
146
147compare() {
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"$@"