I have a program here to test the file IO(actually output) with buffer
turned on and off. What I want to see is that there will be obvious
differece in time. Here I have an input file scales 1.1M, what I did is
just copy it to another output file(ten times). But I don't think I see
any difference. My file system is ext3, with debian
sarge(kernel-2.6.8).
Here is the main part of my program:
/*
* This is a function to handle error.
* Not exactly handle, recently, it
* just print the error number for
* further handling.
*/
void
err_out (int err);
int
main (int argc, char **argv)
{
FILE *fd_in;
FILE *fd_out;
char *io;
struct stat file_stat;
int retval;
extern int errno;
struct timeval begin_val;
struct timezone begin_zone;
struct timeval finish_val;
struct timezone finish_zone;
unsigned short i;
const char *unbuffered_output_filename = "unbuffered_output.txt";
const char *buffered_output_filename = "buffered_output.txt";
char *buffer;
/* Get the size of the input file and then allocate space for input string. */
retval = stat ("./input.txt", &file_stat);
if (retval == -1) {
fprintf (stderr, "Error while getting the status of input file!\n");
err_out (errno);
exit (1);
} else {
fprintf (stdout, "Size of the input file: %d bytes.\nBuffer size of the file: %d.\n",
file_stat.st_size, file_stat.st_blksize);
}
/* Open the input file for input. */
fd_in = fopen ("./input.txt", "r");
if (fd_in == NULL) {
fprintf (stderr, "Error while openning input file for input!\n");
err_out (errno);
exit (2);
}
io = (char *)malloc (file_stat.st_size * sizeof (char));
/* Read in data. */
retval = (int)fread ((void *)io, 1, file_stat.st_size, fd_in);
if (retval != file_stat.st_size && ferror (fd_in)) {
fprintf (stderr, "Error while reading file for test!\n");
free (io);
exit (4);
}
fclose (fd_in);
fprintf (stdout, "Choose:\n1. Unbuffered test.\n2. Buffered test.\n");
if ((char)getc (stdin) == 'u') {
/* Open file for unbuffered output. */
fd_out = fopen (unbuffered_output_filename, "w");
if (fd_out == NULL) {
fprintf (stderr , "Error while openning file for unbuffered output!\n");
err_out (errno);
free (io);
exit (3);
}
/* Shutdown the buffer for unbuffered test. */
setvbuf (fd_out, NULL, _IONBF, 0);
/* Time counting for unbuffered test begins. */
retval = gettimeofday (&begin_val, &begin_zone);
if (retval == -1) {
fprintf (stderr, "Error while getting begin time!\n");
err_out (errno);
free (io);
exit (4);
}
fprintf (stdout, "Time begin(unbuffered): %u sec %u microsec.\n",
begin_val.tv_sec, begin_val.tv_usec);
/* Write 10 times for unbuffered test. */
for (i = 0; i < 10; ++i) {
retval = (int)fwrite ((void *)io, 1, file_stat.st_size, fd_out);
if (retval != file_stat.st_size && ferror (fd_out)) {
fprintf (stderr, "Error while writing file for unbuffered test!\n");
err_out (errno);
free (io);
exit (5);
}
}
/* Time counting for unbuffered test ends. */
retval = gettimeofday (&finish_val, &finish_zone);
if (retval == -1) {
fprintf (stderr, "Error while getting finish time!\n");
err_out (errno);
free (io);
exit (6);
}
fprintf (stdout, "Time finish(unbuffered): %u sec %u microsec.\n",
finish_val.tv_sec, finish_val.tv_usec);
begin_val.tv_sec = finish_val.tv_sec - begin_val.tv_sec;
begin_val.tv_usec = finish_val.tv_usec - begin_val.tv_usec;
fprintf (stdout, "Time spent on unbuffered test: %u sec %u microsec.\n",
begin_val.tv_sec, begin_val.tv_usec);
fclose (fd_out);
} else {
/* Open file for buffered output. */
fd_out = fopen (buffered_output_filename, "w");
if (fd_out == NULL) {
fprintf (stderr, "Error while openning file for buffered output!\n");
err_out (errno);
free (io);
exit (7);
}
/* Turn on the buffer for buffered test. */
buffer = (char *)malloc (131072 * sizeof (char));
setvbuf (fd_out, buffer, _IOFBF, 131072);
/* Time counting for buffered test begins. */
retval = gettimeofday (&begin_val, &begin_zone);
if (retval == -1) {
fprintf (stderr, "Error while getting begin time!\n");
err_out (errno);
free (io);
exit (8);
}
fprintf (stdout, "Time begin(buffered): %u sec %u microsec.\n",
begin_val.tv_sec, begin_val.tv_usec);
/* Write 10 times for buffered test. */
for (i = 0; i < 10; ++i) {
retval = (int)fwrite ((const void *)io, 1, file_stat.st_size, fd_out);
if (retval != file_stat.st_size && ferror (fd_out)) {
fprintf (stderr, "Error while writing file for buffered test!\n");
err_out (errno);
free (io);
free (buffer);
exit (9);
}
}
/* Time counting for buffered test ends. */
retval = gettimeofday (&finish_val, &finish_zone);
if (retval == -1) {
fprintf (stderr, "Error while getting finish time!\n");
err_out (errno);
free (io);
free (buffer);
exit (10);
}
fprintf (stdout, "Time finish(buffered): %u sec %u microsec.\n",
finish_val.tv_sec, finish_val.tv_usec);
begin_val.tv_sec = finish_val.tv_sec - begin_val.tv_sec;
begin_val.tv_usec = finish_val.tv_usec - begin_val.tv_usec;
fprintf (stdout, "Time spent on buffered test: %u sec %u microsec.\n",
begin_val.tv_sec, begin_val.tv_usec);
free (buffer);
fclose (fd_out);
}
free (io);
return 0;
}
The result is here:
Size of the input file: 1128838 bytes.
Buffer size of the file: 4096.
Choose:
1. Unbuffered test.
2. Buffered test.
b
Time begin(buffered): 1134782563 sec 803489 microsec.
Time finish(buffered): 1134782563 sec 852111 microsec.
Time spent on buffered test: 0 sec 48622 microsec.
Size of the input file: 1128838 bytes.
Buffer size of the file: 4096.
Choose:
1. Unbuffered test.
2. Buffered test.
u
Time begin(unbuffered): 1134782571 sec 685364 microsec.
Time finish(unbuffered): 1134782571 sec 732220 microsec.
Time spent on buffered test: 0 sec 48622 microsec.
can anyone help me?
I've also tried to run this program on FreeBSD-5.4, but no diff either
Thanx!!!
pank7.yardbird