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 <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
Jan 22, 2017 at 18:53 UTC
564e94e6190a1a41d8516eaf58b268785ed0a7a7
2 files changed
+51
t/helper/test-string-list.c
+25
@@ -97,6 +97,31 @@ int cmd_main(int argc, const char **argv)
97
return 0;
98
}
99
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
+
125
fprintf(stderr, "%s: unknown function name: %s\n", argv[0],
126
argv[1] ? argv[1] : "(there was none)");
127
return 1;
t/perf/p0071-sort.sh
new
+26
@@ -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