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
|
---|
4 | *
|
---|
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/
|
---|
9 | *
|
---|
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
|
---|
13 | * License.
|
---|
14 | *
|
---|
15 | * The Original Code is the Netscape Portable Runtime (NSPR).
|
---|
16 | *
|
---|
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.
|
---|
21 | *
|
---|
22 | * Contributor(s):
|
---|
23 | *
|
---|
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.
|
---|
35 | *
|
---|
36 | * ***** END LICENSE BLOCK ***** */
|
---|
37 |
|
---|
38 | #include "prio.h"
|
---|
39 | #include "prmem.h"
|
---|
40 | #include "prprf.h"
|
---|
41 | #include "prinrval.h"
|
---|
42 |
|
---|
43 | #include "plerror.h"
|
---|
44 | #include "plgetopt.h"
|
---|
45 |
|
---|
46 | static PRFileDesc *err = NULL;
|
---|
47 |
|
---|
48 | static void Help(void)
|
---|
49 | {
|
---|
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");
|
---|
56 | } /* Help */
|
---|
57 |
|
---|
58 | PRIntn main(PRIntn argc, char **argv)
|
---|
59 | {
|
---|
60 | PRStatus rv;
|
---|
61 | PLOptStatus os;
|
---|
62 | PRUint8 *buffer;
|
---|
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;
|
---|
69 |
|
---|
70 | err = PR_GetSpecialFD(PR_StandardError);
|
---|
71 |
|
---|
72 | while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
|
---|
73 | {
|
---|
74 | if (PL_OPT_BAD == os) continue;
|
---|
75 | switch (opt->option)
|
---|
76 | {
|
---|
77 | case 0: /* Name of file to create */
|
---|
78 | filename = opt->value;
|
---|
79 | break;
|
---|
80 | case 'S': /* Use sych option on file */
|
---|
81 | flags |= PR_SYNC;
|
---|
82 | break;
|
---|
83 | case 'K': /* Size of file to write */
|
---|
84 | filesize = atoi(opt->value);
|
---|
85 | break;
|
---|
86 | case 'c': /* Number of iterations */
|
---|
87 | iterations = atoi(opt->value);
|
---|
88 | break;
|
---|
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 */
|
---|
93 | }
|
---|
94 | }
|
---|
95 | PL_DestroyOptState(opt);
|
---|
96 |
|
---|
97 | file = PR_Open(filename, flags, 0666);
|
---|
98 | if (NULL == file)
|
---|
99 | {
|
---|
100 | PL_FPrintError(err, "Failed to open file");
|
---|
101 | return 1;
|
---|
102 | }
|
---|
103 |
|
---|
104 | buffer = (PRUint8*)PR_CALLOC(1024);
|
---|
105 | if (NULL == buffer)
|
---|
106 | {
|
---|
107 | PL_FPrintError(err, "Cannot allocate buffer");
|
---|
108 | return 1;
|
---|
109 | }
|
---|
110 |
|
---|
111 | for (index = 0; index < sizeof(buffer); ++index)
|
---|
112 | buffer[index] = (PRUint8)index;
|
---|
113 |
|
---|
114 | for (loops = 0; loops < iterations; ++loops)
|
---|
115 | {
|
---|
116 | time = PR_IntervalNow();
|
---|
117 | for (index = 0; index < filesize; ++index)
|
---|
118 | {
|
---|
119 | PR_Write(file, buffer, 1024);
|
---|
120 | }
|
---|
121 | time = (PR_IntervalNow() - time);
|
---|
122 |
|
---|
123 | total += time;
|
---|
124 | if (time < shortest) shortest = time;
|
---|
125 | else if (time > longest) longest = time;
|
---|
126 | if (0 != PR_Seek(file, 0, PR_SEEK_SET))
|
---|
127 | {
|
---|
128 | PL_FPrintError(err, "Rewinding file");
|
---|
129 | return 1;
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | total = total / iterations;
|
---|
134 | PR_fprintf(
|
---|
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));
|
---|
140 |
|
---|
141 | PR_DELETE(buffer);
|
---|
142 | rv = PR_Close(file);
|
---|
143 | if (PR_SUCCESS != rv)
|
---|
144 | {
|
---|
145 | PL_FPrintError(err, "Closing file failed");
|
---|
146 | return 1;
|
---|
147 | }
|
---|
148 | rv = PR_Delete(filename);
|
---|
149 | if (PR_SUCCESS != rv)
|
---|
150 | {
|
---|
151 | PL_FPrintError(err, "Deleting file failed");
|
---|
152 | return 1;
|
---|
153 | }
|
---|
154 | return 0;
|
---|
155 | }
|
---|