Skip to content

Commit 564e94e

Browse files
rscharfegitster
authored andcommitted
perf: add basic sort performance test
Add a sort command to test-string-list that reads lines from stdin, stores them in a string_list and then sorts it. Use it in a simple perf test script to measure the performance of string_list_sort(). Signed-off-by: Rene Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3ca8699 commit 564e94e

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

t/helper/test-string-list.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,31 @@ int cmd_main(int argc, const char **argv)
9797
return 0;
9898
}
9999

100+
if (argc == 2 && !strcmp(argv[1], "sort")) {
101+
struct string_list list = STRING_LIST_INIT_NODUP;
102+
struct strbuf sb = STRBUF_INIT;
103+
struct string_list_item *item;
104+
105+
strbuf_read(&sb, 0, 0);
106+
107+
/*
108+
* Split by newline, but don't create a string_list item
109+
* for the empty string after the last separator.
110+
*/
111+
if (sb.buf[sb.len - 1] == '\n')
112+
strbuf_setlen(&sb, sb.len - 1);
113+
string_list_split_in_place(&list, sb.buf, '\n', -1);
114+
115+
string_list_sort(&list);
116+
117+
for_each_string_list_item(item, &list)
118+
puts(item->string);
119+
120+
string_list_clear(&list, 0);
121+
strbuf_release(&sb);
122+
return 0;
123+
}
124+
100125
fprintf(stderr, "%s: unknown function name: %s\n", argv[0],
101126
argv[1] ? argv[1] : "(there was none)");
102127
return 1;

t/perf/p0071-sort.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/sh
2+
3+
test_description='Basic sort performance tests'
4+
. ./perf-lib.sh
5+
6+
test_perf_default_repo
7+
8+
test_expect_success 'setup' '
9+
git ls-files --stage "*.[ch]" "*.sh" |
10+
cut -f2 -d" " |
11+
git cat-file --batch >unsorted
12+
'
13+
14+
test_perf 'sort(1)' '
15+
sort <unsorted >expect
16+
'
17+
18+
test_perf 'string_list_sort()' '
19+
test-string-list sort <unsorted >actual
20+
'
21+
22+
test_expect_success 'string_list_sort() sorts like sort(1)' '
23+
test_cmp_bin expect actual
24+
'
25+
26+
test_done

0 commit comments

Comments
 (0)