OILS / benchmarks / io / read-lines.sh View on Github | oilshell.org

184 lines, 95 significant
1#!/usr/bin/env bash
2#
3# Test how long it takes to read many files
4
5big-stream() {
6 cat */*.py
7 # Python messes up here!
8 #*/*/*.py
9}
10
11OSH=_bin/cxx-opt/osh
12YSH=_bin/cxx-opt/ysh
13
14setup() {
15 local n=${1:-1} # how many copies
16
17 for i in $(seq $n); do
18 big-stream
19 done > $BIG_FILE
20
21 wc -l $BIG_FILE
22
23 ninja $OSH $YSH
24}
25
26py3-count() {
27 echo '=== python3'
28
29 # Buffered I/O is much faster
30 python3 -c '
31import sys
32i = 0
33for line in sys.stdin:
34 i += 1
35print(i)
36'
37}
38
39awk-count() {
40 echo '=== awk'
41 awk '{ i += 1 } END { print i } '
42}
43
44exec-ysh-count() {
45 local do_trap=${1:-}
46
47 echo '=== ysh'
48
49 local code='
50var i = 0
51for _ in <> {
52 setvar i += 1
53}
54echo $i
55'
56
57 if test -n "$do_trap"; then
58 # Register BEFORE creating pipeline
59 #trap usr1-handler USR1
60 code="
61trap 'echo usr1 in \$\$' USR1
62
63$code
64"
65 fi
66
67 # New buffered read!
68 exec $YSH -c "$code"
69}
70
71usr1-handler() {
72 echo "pid $$ got usr1"
73}
74
75exec-sh-count() {
76 local sh=$1
77 local do_trap=${2:-}
78
79 echo "pid = $$"
80
81 echo === $sh
82
83 local code='
84i=0
85while read -r line; do
86 i=$(( i + 1 ))
87done
88echo $i
89'
90
91 if test -n "$do_trap"; then
92 # Register BEFORE creating pipeline
93 #trap usr1-handler USR1
94 code="
95trap 'echo usr1 in \$\$' USR1
96
97$code
98"
99 fi
100 #echo "$code"
101
102 # need exec here for trap-demo
103 exec $sh -c "$code"
104}
105
106readonly BIG_FILE=_tmp/lines.txt
107
108compare() {
109
110 echo '=== wc'
111 time wc -l < $BIG_FILE # warmup
112 echo
113
114 time py3-count < $BIG_FILE
115 echo
116
117 time awk-count < $BIG_FILE
118 echo
119
120 time $0 exec-ysh-count < $BIG_FILE
121 echo
122
123 for sh in dash bash $OSH; do
124 # need $0 because it exec
125 time $0 exec-sh-count $sh < $BIG_FILE
126 echo
127 done
128}
129
130sh-count-with-trap() {
131 local sh=$1
132
133 local -a argv
134 case $sh in
135 *ysh)
136 argv=(exec-ysh-count T)
137 ;;
138 *)
139 argv=(exec-sh-count $sh T)
140 ;;
141 esac
142
143 "${argv[@]}" < $BIG_FILE &
144
145 #$0 sh-count bash T &
146 #$0 sh-count dash T &
147
148 local pid=$!
149 echo "background = $pid"
150 pstree -p $pid
151 echo
152
153 #wait
154 #echo status=$?
155 #return
156
157 while true; do
158 # wait for USR1 to be registered
159 sleep 0.05
160
161 kill -s USR1 $pid
162 local status=$?
163
164 echo "kill status: $status"
165 if test $status -ne 0; then
166 break
167 fi
168
169 done
170
171 wait
172 echo status=$?
173}
174
175compare-trap() {
176 for sh in $YSH dash bash $OSH; do
177 sh-count-with-trap $sh
178 echo
179 echo
180 done
181}
182
183"$@"
184