Raw
1 /*
2 * A test helper to exercise the progress display.
3 *
4 * Reads instructions from standard input, one instruction per line:
5 *
6 * "start <total>[ <title>]" - Call start_progress(title, total),
7 * Uses the default title of "Working hard"
8 * if the " <title>" is omitted.
9 * "progress <items>" - Call display_progress() with the given item count
10 * as parameter.
11 * "throughput <bytes> <millis> - Call display_throughput() with the given
12 * byte count as parameter. The 'millis'
13 * specify the time elapsed since the
14 * start_progress() call.
15 * "update" - Set the 'progress_update' flag.
16 * "stop" - Call stop_progress().
17 *
18 * See 't0500-progress-display.sh' for examples.
19 */
20
21 #define USE_THE_REPOSITORY_VARIABLE
22 #define GIT_TEST_PROGRESS_ONLY
23
24 #include "test-tool.h"
25 #include "parse-options.h"
26 #include "progress.h"
27 #include "repository.h"
28 #include "strbuf.h"
29 #include "string-list.h"
30
31 int cmd__progress(int argc, const char **argv)
32 {
33 const char *const default_title = "Working hard";
34 struct string_list titles = STRING_LIST_INIT_DUP;
35 struct strbuf line = STRBUF_INIT;
36 struct progress *progress = NULL;
37
38 const char *usage[] = {
39 "test-tool progress <stdin",
40 NULL
41 };
42 struct option options[] = {
43 OPT_END(),
44 };
45
46 argc = parse_options(argc, argv, NULL, options, usage, 0);
47 if (argc)
48 usage_with_options(usage, options);
49
50 progress_testing = 1;
51 while (strbuf_getline(&line, stdin) != EOF) {
52 char *end;
53
54 if (skip_prefix(line.buf, "start ", (const char **) &end)) {
55 uint64_t total = strtoull(end, &end, 10);
56 const char *title;
57
58 /*
59 * We can't use "end + 1" as an argument to
60 * start_progress(), it doesn't xstrdup() its
61 * "title" argument. We need to hold onto a
62 * valid "char *" for it until the end.
63 */
64 if (!*end)
65 title = default_title;
66 else if (*end == ' ')
67 title = string_list_insert(&titles, end + 1)->string;
68 else
69 die("invalid input: '%s'", line.buf);
70
71 progress = start_progress(the_repository, title, total);
72 } else if (skip_prefix(line.buf, "progress ", (const char **) &end)) {
73 uint64_t item_count = strtoull(end, &end, 10);
74 if (*end != '\0')
75 die("invalid input: '%s'", line.buf);
76 display_progress(progress, item_count);
77 } else if (skip_prefix(line.buf, "throughput ",
78 (const char **) &end)) {
79 uint64_t byte_count, test_ms;
80
81 byte_count = strtoull(end, &end, 10);
82 if (*end != ' ')
83 die("invalid input: '%s'", line.buf);
84 test_ms = strtoull(end + 1, &end, 10);
85 if (*end != '\0')
86 die("invalid input: '%s'", line.buf);
87 progress_test_ns = test_ms * 1000 * 1000;
88 display_throughput(progress, byte_count);
89 } else if (!strcmp(line.buf, "update")) {
90 progress_test_force_update();
91 } else if (!strcmp(line.buf, "stop")) {
92 stop_progress(&progress);
93 } else {
94 die("invalid input: '%s'", line.buf);
95 }
96 }
97 strbuf_release(&line);
98 string_list_clear(&titles, 0);
99
100 return 0;
101 }