From 4419de916493d8a4292e9b78be6c18aa3641d353 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Sun, 27 Jan 2019 15:26:54 -0800 Subject: 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 Signed-off-by: Junio C Hamano --- t/helper/test-date.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 't/helper/test-date.c') diff --git a/t/helper/test-date.c b/t/helper/test-date.c index a0837371ab..792a805374 100644 --- a/t/helper/test-date.c +++ b/t/helper/test-date.c @@ -7,6 +7,7 @@ static const char *usage_msg = "\n" " test-tool date parse [date]...\n" " test-tool date approxidate [date]...\n" " test-tool date timestamp [date]...\n" +" test-tool date getnanos [start-nanos]\n" " test-tool date is64bit\n" " test-tool date time_t-is64bit\n"; @@ -82,6 +83,15 @@ static void parse_approx_timestamp(const char **argv, struct timeval *now) } } +static void getnanos(const char **argv, struct timeval *now) +{ + double seconds = getnanotime() / 1.0e9; + + if (*argv) + seconds -= strtod(*argv, NULL); + printf("%lf\n", seconds); +} + int cmd__date(int argc, const char **argv) { struct timeval now; @@ -108,6 +118,8 @@ int cmd__date(int argc, const char **argv) parse_approxidate(argv+1, &now); else if (!strcmp(*argv, "timestamp")) parse_approx_timestamp(argv+1, &now); + else if (!strcmp(*argv, "getnanos")) + getnanos(argv+1, &now); else if (!strcmp(*argv, "is64bit")) return sizeof(timestamp_t) == 8 ? 0 : 1; else if (!strcmp(*argv, "time_t-is64bit")) -- cgit v1.2.3 From ba285a712d8bc036d8f0d4625710305711b3c2cb Mon Sep 17 00:00:00 2001 From: Jeff King Date: Wed, 6 Feb 2019 14:35:52 -0500 Subject: test-date: drop unused parameter to getnanos() The getnanos() helper always gets the current time from our getnanotime() facility. The caller cannot override it via TEST_DATE_NOW, and hence we simply ignore the "now" parameter to the function. Let's remove it, as it may mislead callers into thinking it does something. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- t/helper/test-date.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 't/helper/test-date.c') diff --git a/t/helper/test-date.c b/t/helper/test-date.c index 792a805374..f9e2b91ed1 100644 --- a/t/helper/test-date.c +++ b/t/helper/test-date.c @@ -83,7 +83,7 @@ static void parse_approx_timestamp(const char **argv, struct timeval *now) } } -static void getnanos(const char **argv, struct timeval *now) +static void getnanos(const char **argv) { double seconds = getnanotime() / 1.0e9; @@ -119,7 +119,7 @@ int cmd__date(int argc, const char **argv) else if (!strcmp(*argv, "timestamp")) parse_approx_timestamp(argv+1, &now); else if (!strcmp(*argv, "getnanos")) - getnanos(argv+1, &now); + getnanos(argv+1); else if (!strcmp(*argv, "is64bit")) return sizeof(timestamp_t) == 8 ? 0 : 1; else if (!strcmp(*argv, "time_t-is64bit")) -- cgit v1.2.3