1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is the Netscape Portable Runtime (NSPR).
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998-2000
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
46 static PRFileDesc
*err
= NULL
;
48 static void Help(void)
50 PR_fprintf(err
, "Usage: [-S] [-K <n>] [-h] <filename>\n");
51 PR_fprintf(err
, "\t-c Nuber of iterations (default: 10)\n");
52 PR_fprintf(err
, "\t-S Sync the file (default: FALSE)\n");
53 PR_fprintf(err
, "\t-K Size of file (K bytes) (default: 10)\n");
54 PR_fprintf(err
, "\t Name of file to write (default: /usr/tmp/sync.dat)\n");
55 PR_fprintf(err
, "\t-h This message and nothing else\n");
58 PRIntn
main(PRIntn argc
, char **argv
)
63 PRFileDesc
*file
= NULL
;
64 const char *filename
= "sync.dat";
65 PRUint32 index
, loops
, iterations
= 10, filesize
= 10;
66 PRIntn flags
= PR_WRONLY
| PR_CREATE_FILE
| PR_TRUNCATE
;
67 PLOptState
*opt
= PL_CreateOptState(argc
, argv
, "hSK:c:");
68 PRIntervalTime time
, total
= 0, shortest
= 0x7fffffff, longest
= 0;
70 err
= PR_GetSpecialFD(PR_StandardError
);
72 while (PL_OPT_EOL
!= (os
= PL_GetNextOpt(opt
)))
74 if (PL_OPT_BAD
== os
) continue;
77 case 0: /* Name of file to create */
78 filename
= opt
->value
;
80 case 'S': /* Use sych option on file */
83 case 'K': /* Size of file to write */
84 filesize
= atoi(opt
->value
);
86 case 'c': /* Number of iterations */
87 iterations
= atoi(opt
->value
);
89 case 'h': /* user wants some guidance */
90 default: /* user needs some guidance */
91 Help(); /* so give him an earful */
92 return 2; /* but not a lot else */
95 PL_DestroyOptState(opt
);
97 file
= PR_Open(filename
, flags
, 0666);
100 PL_FPrintError(err
, "Failed to open file");
104 buffer
= (PRUint8
*)PR_CALLOC(1024);
107 PL_FPrintError(err
, "Cannot allocate buffer");
111 for (index
= 0; index
< sizeof(buffer
); ++index
)
112 buffer
[index
] = (PRUint8
)index
;
114 for (loops
= 0; loops
< iterations
; ++loops
)
116 time
= PR_IntervalNow();
117 for (index
= 0; index
< filesize
; ++index
)
119 PR_Write(file
, buffer
, 1024);
121 time
= (PR_IntervalNow() - time
);
124 if (time
< shortest
) shortest
= time
;
125 else if (time
> longest
) longest
= time
;
126 if (0 != PR_Seek(file
, 0, PR_SEEK_SET
))
128 PL_FPrintError(err
, "Rewinding file");
133 total
= total
/ iterations
;
135 err
, "%u iterations over a %u kbyte %sfile: %u [%u] %u\n",
136 iterations
, filesize
, ((flags
& PR_SYNC
) ? "SYNCH'd " : ""),
137 PR_IntervalToMicroseconds(shortest
),
138 PR_IntervalToMicroseconds(total
),
139 PR_IntervalToMicroseconds(longest
));
143 if (PR_SUCCESS
!= rv
)
145 PL_FPrintError(err
, "Closing file failed");
148 rv
= PR_Delete(filename
);
149 if (PR_SUCCESS
!= rv
)
151 PL_FPrintError(err
, "Deleting file failed");