Skip to content

Commit c80b446

Browse files
committed
tests: Add performance test for string iteration
Signed-off-by: Alexey Gladkov <legion@kernel.org>
1 parent b437576 commit c80b446

5 files changed

Lines changed: 85 additions & 3 deletions

File tree

.github/workflows/lint.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ jobs:
5656
steps:
5757
- uses: actions/checkout@v4
5858
- name: "install tools"
59-
run: sudo apt-get -y -qq install dash
59+
run: sudo apt-get -y -qq install dash bash mksh
6060
- name: "unittests"
6161
run: make check CHECK_SHELL=/bin/dash V=1
6262

@@ -66,7 +66,7 @@ jobs:
6666
steps:
6767
- uses: actions/checkout@v4
6868
- name: "install tools"
69-
run: sudo apt-get -y -qq install bash
69+
run: sudo apt-get -y -qq install dash bash mksh
7070
- name: "unittests"
7171
run: make check CHECK_SHELL=/bin/bash V=1
7272

@@ -76,6 +76,6 @@ jobs:
7676
steps:
7777
- uses: actions/checkout@v4
7878
- name: "install tools"
79-
run: sudo apt-get -y -qq install mksh
79+
run: sudo apt-get -y -qq install dash bash mksh
8080
- name: "unittests"
8181
run: make check CHECK_SHELL=/bin/mksh V=1

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ check:
8585
@cd tests; \
8686
for sh in $${CHECK_SHELL:-/bin/sh /bin/dash /bin/bash /bin/bash3 /bin/bash4 /bin/mksh /bin/yash /bin/pdksh}; do \
8787
[ -x "$$sh" ] || continue; \
88+
export TEST_SHELL="$$sh"; \
8889
echo "Running tests with $$sh"; \
8990
if ! "$$sh" -efu ./runtests; then \
9091
echo "Tests failed with $$sh"; \

tests/check-string-performance

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/bin/sh -efu
2+
3+
get_user_time()
4+
{
5+
# 0m0.099s 0m0.095s
6+
7+
local user_time system_time
8+
read -r user_time system_time ||:
9+
10+
user_time="${user_time%.*}"
11+
12+
echo "$user_time"
13+
}
14+
15+
string_performance_test1() { # UnitTest
16+
. ../shell-string
17+
18+
local i p s dash_shell
19+
20+
case "${TEST_SHELL##*/}" in
21+
ash|dash)
22+
shouldSkip
23+
;;
24+
esac
25+
26+
27+
for i in ash dash; do
28+
dash_shell="$(which "$i" 2>/dev/null)" && break ||:
29+
done
30+
31+
[ -n "$dash_shell" ] ||
32+
shouldSkip
33+
34+
p="1234567890AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"
35+
s=
36+
for i in \
37+
0 1 2 3 4 5 6 7 8 9 \
38+
0 1 2 3 4 5 6 7 8 9 \
39+
0 1 2 3 4 5 6 7 8 9 \
40+
0 1 2 3 4 5 6 7 8 9 \
41+
0 1 2 3 4 5 6 7 8 9 \
42+
0 1 2 3 4 5 6 7 8 9 \
43+
0 1 2 3 4 5 6 7 8 9 \
44+
0 1 2 3 4 5 6 7 8 9 \
45+
;
46+
do
47+
s="$s$p"
48+
done
49+
50+
export string="$s"
51+
52+
local result_dash result_test
53+
54+
result_dash="$($dash_shell -efu ./string-data/iterate-string.sh | get_user_time)"
55+
result_test="$($TEST_SHELL -efu ./string-data/iterate-string.sh | get_user_time)"
56+
57+
assertTrue "$TEST_SHELL ran a minutes longer: $dash_shell: $result_dash, $TEST_SHELL: $result_test" \
58+
"test ${result_dash%m*} -ge ${result_test%m*}"
59+
60+
assertTrue "$TEST_SHELL ran a seconds longer: $dash_shell: $result_dash, $TEST_SHELL: $result_test" \
61+
"test ${result_dash#*m} -ge ${result_test#*m}"
62+
63+
case "${TEST_SHELL##*/}" in
64+
bash*|*ksh)
65+
assertTrue "$TEST_SHELL is too slow: $result_test" \
66+
"test ${result_test%m*} -eq 0 -a ${result_test#*m} -lt 3"
67+
;;
68+
esac
69+
}

tests/runtests

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ tearDownTests() {
2121
}
2222

2323
for s in \
24+
check-string-performance \
2425
quote_sed_regexp quote_shell string_quote_remove \
2526
opt_check_dir opt_check_number opt_check_read \
2627
quote_sed_regexp_variable quote_shell_variable \
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/dash -efu
2+
# SPDX-License-Identifier: GPL-2.0-or-later
3+
4+
. ../shell-string
5+
6+
__shell_string_foreach_prepare ctx "$string"
7+
while __shell_string_foreach_continue "$ctx"; do
8+
__shell_string_foreach_char c "$ctx"
9+
__shell_string_foreach_iter "$ctx"
10+
done
11+
times

0 commit comments

Comments
 (0)