1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/pr/tests/fsync.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,123 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "prio.h" 1.10 +#include "prmem.h" 1.11 +#include "prprf.h" 1.12 +#include "prinrval.h" 1.13 + 1.14 +#include "plerror.h" 1.15 +#include "plgetopt.h" 1.16 + 1.17 +static PRFileDesc *err = NULL; 1.18 + 1.19 +static void Help(void) 1.20 +{ 1.21 + PR_fprintf(err, "Usage: [-S] [-K <n>] [-h] <filename>\n"); 1.22 + PR_fprintf(err, "\t-c Nuber of iterations (default: 10)\n"); 1.23 + PR_fprintf(err, "\t-S Sync the file (default: FALSE)\n"); 1.24 + PR_fprintf(err, "\t-K Size of file (K bytes) (default: 10)\n"); 1.25 + PR_fprintf(err, "\t Name of file to write (default: /usr/tmp/sync.dat)\n"); 1.26 + PR_fprintf(err, "\t-h This message and nothing else\n"); 1.27 +} /* Help */ 1.28 + 1.29 +int main(int argc, char **argv) 1.30 +{ 1.31 + PRStatus rv; 1.32 + PLOptStatus os; 1.33 + PRUint8 *buffer; 1.34 + PRFileDesc *file = NULL; 1.35 + const char *filename = "sync.dat"; 1.36 + PRUint32 index, loops, iterations = 10, filesize = 10; 1.37 + PRIntn flags = PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE; 1.38 + PLOptState *opt = PL_CreateOptState(argc, argv, "hSK:c:"); 1.39 + PRIntervalTime time, total = 0, shortest = 0x7fffffff, longest = 0; 1.40 + 1.41 + err = PR_GetSpecialFD(PR_StandardError); 1.42 + 1.43 + while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) 1.44 + { 1.45 + if (PL_OPT_BAD == os) continue; 1.46 + switch (opt->option) 1.47 + { 1.48 + case 0: /* Name of file to create */ 1.49 + filename = opt->value; 1.50 + break; 1.51 + case 'S': /* Use sych option on file */ 1.52 + flags |= PR_SYNC; 1.53 + break; 1.54 + case 'K': /* Size of file to write */ 1.55 + filesize = atoi(opt->value); 1.56 + break; 1.57 + case 'c': /* Number of iterations */ 1.58 + iterations = atoi(opt->value); 1.59 + break; 1.60 + case 'h': /* user wants some guidance */ 1.61 + default: /* user needs some guidance */ 1.62 + Help(); /* so give him an earful */ 1.63 + return 2; /* but not a lot else */ 1.64 + } 1.65 + } 1.66 + PL_DestroyOptState(opt); 1.67 + 1.68 + file = PR_Open(filename, flags, 0666); 1.69 + if (NULL == file) 1.70 + { 1.71 + PL_FPrintError(err, "Failed to open file"); 1.72 + return 1; 1.73 + } 1.74 + 1.75 + buffer = (PRUint8*)PR_CALLOC(1024); 1.76 + if (NULL == buffer) 1.77 + { 1.78 + PL_FPrintError(err, "Cannot allocate buffer"); 1.79 + return 1; 1.80 + } 1.81 + 1.82 + for (index = 0; index < sizeof(buffer); ++index) 1.83 + buffer[index] = (PRUint8)index; 1.84 + 1.85 + for (loops = 0; loops < iterations; ++loops) 1.86 + { 1.87 + time = PR_IntervalNow(); 1.88 + for (index = 0; index < filesize; ++index) 1.89 + { 1.90 + PR_Write(file, buffer, 1024); 1.91 + } 1.92 + time = (PR_IntervalNow() - time); 1.93 + 1.94 + total += time; 1.95 + if (time < shortest) shortest = time; 1.96 + else if (time > longest) longest = time; 1.97 + if (0 != PR_Seek(file, 0, PR_SEEK_SET)) 1.98 + { 1.99 + PL_FPrintError(err, "Rewinding file"); 1.100 + return 1; 1.101 + } 1.102 + } 1.103 + 1.104 + total = total / iterations; 1.105 + PR_fprintf( 1.106 + err, "%u iterations over a %u kbyte %sfile: %u [%u] %u\n", 1.107 + iterations, filesize, ((flags & PR_SYNC) ? "SYNCH'd " : ""), 1.108 + PR_IntervalToMicroseconds(shortest), 1.109 + PR_IntervalToMicroseconds(total), 1.110 + PR_IntervalToMicroseconds(longest)); 1.111 + 1.112 + PR_DELETE(buffer); 1.113 + rv = PR_Close(file); 1.114 + if (PR_SUCCESS != rv) 1.115 + { 1.116 + PL_FPrintError(err, "Closing file failed"); 1.117 + return 1; 1.118 + } 1.119 + rv = PR_Delete(filename); 1.120 + if (PR_SUCCESS != rv) 1.121 + { 1.122 + PL_FPrintError(err, "Deleting file failed"); 1.123 + return 1; 1.124 + } 1.125 + return 0; 1.126 +}