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

146 lines, 71 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
11setup() {
12 for i in {1..2}; do
13 big-stream
14 done > $BIG_FILE
15
16 wc -l $BIG_FILE
17}
18
19py3-count() {
20 echo '=== python3'
21
22 # Buffered I/O is much faster
23 python3 -c '
24import sys
25i = 0
26for line in sys.stdin:
27 i += 1
28print(i)
29'
30}
31
32awk-count() {
33 echo '=== awk'
34 awk '{ i += 1 } END { print i } '
35}
36
37ysh-count() {
38 echo '=== ysh'
39
40 local ysh=_bin/cxx-opt/ysh
41 ninja $ysh
42
43 # New buffered read!
44 $ysh -c '
45var i = 0
46for _ in <> {
47 setvar i += 1
48}
49echo $i
50 '
51}
52
53usr1-handler() {
54 echo "pid $$ got usr1"
55}
56
57exec-sh-count() {
58 local sh=$1
59 local do_trap=${2:-}
60
61 echo "pid = $$"
62
63 echo === $sh
64
65 local code='
66i=0
67while read -r line; do
68 i=$(( i + 1 ))
69done
70echo $i
71'
72
73 if test -n "$do_trap"; then
74 # Register BEFORE creating pipeline
75 #trap usr1-handler USR1
76 code="
77trap 'echo usr1 in \$\$' USR1
78
79$code
80"
81 fi
82 #echo "$code"
83
84 # need exec here for trap-demo
85 exec $sh -c "$code"
86}
87
88readonly BIG_FILE=_tmp/lines.txt
89
90compare() {
91
92 time wc -l < $BIG_FILE # warmup
93 echo
94
95 time py3-count < $BIG_FILE
96 echo
97
98 time awk-count < $BIG_FILE
99 echo
100
101 time ysh-count < $BIG_FILE
102 echo
103
104 local osh=_bin/cxx-opt/osh
105 ninja $osh
106
107 for sh in dash bash $osh; do
108 # need $0 because it exec
109 time $0 exec-sh-count $sh < $BIG_FILE
110 echo
111 done
112}
113
114trap-demo() {
115 exec-sh-count bash T < $BIG_FILE &
116 #$0 sh-count bash T &
117 #$0 sh-count dash T &
118
119 local pid=$!
120 echo "background = $pid"
121 pstree -p $pid
122
123 #wait
124 #echo status=$?
125 #return
126
127 while true; do
128 # wait for USR1 to be registered
129 sleep 0.05
130
131 kill -s USR1 $pid
132 local status=$?
133
134 echo status=$status
135 if test $status -ne 0; then
136 break
137 fi
138
139 done
140
141 wait
142 echo status=$?
143}
144
145"$@"
146