michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "prio.h" michael@0: #include "prmem.h" michael@0: #include "prprf.h" michael@0: #include "prinrval.h" michael@0: michael@0: #include "plerror.h" michael@0: #include "plgetopt.h" michael@0: michael@0: static PRFileDesc *err = NULL; michael@0: michael@0: static void Help(void) michael@0: { michael@0: PR_fprintf(err, "Usage: [-S] [-K ] [-h] \n"); michael@0: PR_fprintf(err, "\t-c Nuber of iterations (default: 10)\n"); michael@0: PR_fprintf(err, "\t-S Sync the file (default: FALSE)\n"); michael@0: PR_fprintf(err, "\t-K Size of file (K bytes) (default: 10)\n"); michael@0: PR_fprintf(err, "\t Name of file to write (default: /usr/tmp/sync.dat)\n"); michael@0: PR_fprintf(err, "\t-h This message and nothing else\n"); michael@0: } /* Help */ michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: PRStatus rv; michael@0: PLOptStatus os; michael@0: PRUint8 *buffer; michael@0: PRFileDesc *file = NULL; michael@0: const char *filename = "sync.dat"; michael@0: PRUint32 index, loops, iterations = 10, filesize = 10; michael@0: PRIntn flags = PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE; michael@0: PLOptState *opt = PL_CreateOptState(argc, argv, "hSK:c:"); michael@0: PRIntervalTime time, total = 0, shortest = 0x7fffffff, longest = 0; michael@0: michael@0: err = PR_GetSpecialFD(PR_StandardError); michael@0: michael@0: while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) michael@0: { michael@0: if (PL_OPT_BAD == os) continue; michael@0: switch (opt->option) michael@0: { michael@0: case 0: /* Name of file to create */ michael@0: filename = opt->value; michael@0: break; michael@0: case 'S': /* Use sych option on file */ michael@0: flags |= PR_SYNC; michael@0: break; michael@0: case 'K': /* Size of file to write */ michael@0: filesize = atoi(opt->value); michael@0: break; michael@0: case 'c': /* Number of iterations */ michael@0: iterations = atoi(opt->value); michael@0: break; michael@0: case 'h': /* user wants some guidance */ michael@0: default: /* user needs some guidance */ michael@0: Help(); /* so give him an earful */ michael@0: return 2; /* but not a lot else */ michael@0: } michael@0: } michael@0: PL_DestroyOptState(opt); michael@0: michael@0: file = PR_Open(filename, flags, 0666); michael@0: if (NULL == file) michael@0: { michael@0: PL_FPrintError(err, "Failed to open file"); michael@0: return 1; michael@0: } michael@0: michael@0: buffer = (PRUint8*)PR_CALLOC(1024); michael@0: if (NULL == buffer) michael@0: { michael@0: PL_FPrintError(err, "Cannot allocate buffer"); michael@0: return 1; michael@0: } michael@0: michael@0: for (index = 0; index < sizeof(buffer); ++index) michael@0: buffer[index] = (PRUint8)index; michael@0: michael@0: for (loops = 0; loops < iterations; ++loops) michael@0: { michael@0: time = PR_IntervalNow(); michael@0: for (index = 0; index < filesize; ++index) michael@0: { michael@0: PR_Write(file, buffer, 1024); michael@0: } michael@0: time = (PR_IntervalNow() - time); michael@0: michael@0: total += time; michael@0: if (time < shortest) shortest = time; michael@0: else if (time > longest) longest = time; michael@0: if (0 != PR_Seek(file, 0, PR_SEEK_SET)) michael@0: { michael@0: PL_FPrintError(err, "Rewinding file"); michael@0: return 1; michael@0: } michael@0: } michael@0: michael@0: total = total / iterations; michael@0: PR_fprintf( michael@0: err, "%u iterations over a %u kbyte %sfile: %u [%u] %u\n", michael@0: iterations, filesize, ((flags & PR_SYNC) ? "SYNCH'd " : ""), michael@0: PR_IntervalToMicroseconds(shortest), michael@0: PR_IntervalToMicroseconds(total), michael@0: PR_IntervalToMicroseconds(longest)); michael@0: michael@0: PR_DELETE(buffer); michael@0: rv = PR_Close(file); michael@0: if (PR_SUCCESS != rv) michael@0: { michael@0: PL_FPrintError(err, "Closing file failed"); michael@0: return 1; michael@0: } michael@0: rv = PR_Delete(filename); michael@0: if (PR_SUCCESS != rv) michael@0: { michael@0: PL_FPrintError(err, "Deleting file failed"); michael@0: return 1; michael@0: } michael@0: return 0; michael@0: }