BSD 4_4 development
[unix-history] / usr / src / sys / tests / benchmarks / randwrite.c
CommitLineData
f8d31f56
C
1/*
2 * Random I/O benchmark.
3 *
4 * Process writes blocks to a
5 * file in a random order.
6 */
7#include <sys/types.h>
8#include <sys/file.h>
9#include <sys/stat.h>
10
11char *malloc();
12
13main(argc, argv)
14 char *argv[];
15{
16 char *buf;
17 int fd, bn, maxblocks;
18 struct stat sb;
19 register int i, niter;
20
21 if (argc < 4) {
22 printf("usage: %s file max-file-size #writes\n", argv[0]);
23 exit(1);
24 }
25 fd = open(argv[1], O_WRONLY|O_CREAT|O_TRUNC, 0644);
26 if (fd < 0) {
27 perror(argv[1]);
28 exit(2);
29 }
30 buf = malloc(sb.st_blksize);
31 if (buf == (char *)0) {
32 printf("Couldn't allocate i/o buffer.\n");
33 exit(3);
34 }
35 /* file size is in megabytes */
36 fstat(fd, &sb);
37 maxblocks = atoi(argv[2]) * ((1024 * 1024) / sb.st_blksize);
38 niter = atoi(argv[3]);
39 printf("%d random writes (block size %d)\n", niter, sb.st_blksize);
40 for (i = 0; i < niter; i++) {
41 bn = random() % maxblocks;
42 lseek(fd, bn * sb.st_blksize, L_SET);
43 write(fd, buf, sb.st_blksize);
44 }
45}