Skip to content

Commit df7000c

Browse files
dschogitster
authored andcommitted
test-tool genzeros: generate large amounts of data more efficiently
In this developer's tests, producing one gigabyte worth of NULs in a busy loop that writes out individual bytes, unbuffered, took ~27sec. Writing chunked 256kB buffers instead only took ~0.6sec This matters because we are about to introduce a pair of test cases that want to be able to produce 5GB of NULs, and we cannot use `/dev/zero` because of the HP NonStop platform's lack of support for that device. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent cbc985a commit df7000c

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

t/helper/test-genzeros.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33

44
int cmd__genzeros(int argc, const char **argv)
55
{
6+
/* static, so that it is NUL-initialized */
7+
static const char zeros[256 * 1024];
68
intmax_t count;
9+
ssize_t n;
710

811
if (argc > 2) {
912
fprintf(stderr, "usage: %s [<count>]\n", argv[0]);
@@ -12,9 +15,19 @@ int cmd__genzeros(int argc, const char **argv)
1215

1316
count = argc > 1 ? strtoimax(argv[1], NULL, 0) : -1;
1417

15-
while (count < 0 || count--) {
16-
if (putchar(0) == EOF)
18+
/* Writing out individual NUL bytes is slow... */
19+
while (count < 0)
20+
if (write(1, zeros, ARRAY_SIZE(zeros)) < 0)
1721
return -1;
22+
23+
while (count > 0) {
24+
n = write(1, zeros, count < ARRAY_SIZE(zeros) ?
25+
count : ARRAY_SIZE(zeros));
26+
27+
if (n < 0)
28+
return -1;
29+
30+
count -= n;
1831
}
1932

2033
return 0;

0 commit comments

Comments
 (0)