Skip to content

Commit 0f9fdff

Browse files
committed
test-date: add a subcommand to measure times in shell scripts
In the next commit, we want to teach Git's test suite to optionally output test results in JUnit-style .xml files. These files contain information about the time spent. So we need a way to measure time. While we could use `date +%s` for that, this will give us only seconds, i.e. very coarse-grained timings. GNU `date` supports `date +%s.%N` (i.e. nanosecond-precision output), but there is no equivalent in BSD `date` (read: on macOS, we would not be able to obtain precise timings). So let's introduce `test-tool date getnanos`, with an optional start time, that outputs preciser values. Note that this might not actually give us nanosecond precision on some platforms, but it will give us as precise information as possible, without the portability issues of shell commands. Granted, it is a bit pointless to try measuring times accurately in shell scripts, certainly to nanosecond precision. But it is better than second-granularity. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 653e1f4 commit 0f9fdff

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

t/helper/test-date.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ static const char *usage_msg = "\n"
77
" test-tool date parse [date]...\n"
88
" test-tool date approxidate [date]...\n"
99
" test-tool date timestamp [date]...\n"
10+
" test-tool date getnanos [start-nanos]\n"
1011
" test-tool date is64bit\n"
1112
" test-tool date time_t-is64bit\n";
1213

@@ -82,6 +83,15 @@ static void parse_approx_timestamp(const char **argv, struct timeval *now)
8283
}
8384
}
8485

86+
static void getnanos(const char **argv, struct timeval *now)
87+
{
88+
double seconds = getnanotime() / 1.0e9;
89+
90+
if (*argv)
91+
seconds -= strtod(*argv, NULL);
92+
printf("%lf\n", seconds);
93+
}
94+
8595
int cmd__date(int argc, const char **argv)
8696
{
8797
struct timeval now;
@@ -108,6 +118,8 @@ int cmd__date(int argc, const char **argv)
108118
parse_approxidate(argv+1, &now);
109119
else if (!strcmp(*argv, "timestamp"))
110120
parse_approx_timestamp(argv+1, &now);
121+
else if (!strcmp(*argv, "getnanos"))
122+
getnanos(argv+1, &now);
111123
else if (!strcmp(*argv, "is64bit"))
112124
return sizeof(timestamp_t) == 8 ? 0 : 1;
113125
else if (!strcmp(*argv, "time_t-is64bit"))

0 commit comments

Comments
 (0)