OILS / benchmarks / ysh-json.sh View on Github | oilshell.org

107 lines, 39 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-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
20set -o nounset
21set -o pipefail
22set -o errexit
23
24YSH=_bin/cxx-opt/ysh
25OSH=_bin/cxx-opt/osh
26
27readonly JSON_FILE=_tmp/github-issues.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
41with-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
54with-cpython() {
55 local prog='
56import json
57import sys
58for 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
70with-js() {
71 # 195 ms, minus ~100 ms startup time = 90 ms
72 time cat $JSON_FILE | nodejs -e '
73var fs = require("fs")
74var stdin = fs.readFileSync(0, "utf-8")
75
76for (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
86with-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
94compare() {
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"$@"