Bug 346849: Add a "Save Image as..." entry to the context menu for <canvas>, Original...
[mozilla-central.git] / netwerk / test / TestWriteSpeed.cpp
blobf1b56a5456f756a116bea378036cfcb825f8b27e
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
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 ***** */
38 #include "prio.h"
39 #include "prinrval.h"
40 #include "prmem.h"
41 #include <stdio.h>
42 #include <math.h>
44 void
45 NS_MeanAndStdDev(double n, double sumOfValues, double sumOfSquaredValues,
46 double *meanResult, double *stdDevResult)
48 double mean = 0.0, var = 0.0, stdDev = 0.0;
49 if (n > 0.0 && sumOfValues >= 0) {
50 mean = sumOfValues / n;
51 double temp = (n * sumOfSquaredValues) - (sumOfValues * sumOfValues);
52 if (temp < 0.0 || n <= 1)
53 var = 0.0;
54 else
55 var = temp / (n * (n - 1));
56 // for some reason, Windows says sqrt(0.0) is "-1.#J" (?!) so do this:
57 stdDev = var != 0.0 ? sqrt(var) : 0.0;
59 *meanResult = mean;
60 *stdDevResult = stdDev;
63 int
64 Test(const char* filename, PRInt32 minSize, PRInt32 maxSize,
65 PRInt32 sizeIncrement, PRInt32 iterations)
67 fprintf(stdout, " size write: mean stddev iters total: mean stddev iters\n");
68 for (PRInt32 size = minSize; size <= maxSize; size += sizeIncrement) {
69 // create a buffer of stuff to write
70 char* buf = (char*)PR_Malloc(size);
71 if (buf == NULL)
72 return -1;
74 // initialize it with a pattern
75 PRInt32 i;
76 char hex[] = "0123456789ABCDEF";
77 for (i = 0; i < size; i++) {
78 buf[i] = hex[i & 0xF];
81 double writeCount = 0, writeRate = 0, writeRateSquared = 0;
82 double totalCount = 0, totalRate = 0, totalRateSquared = 0;
83 for (i = 0; i < iterations; i++) {
84 PRIntervalTime start = PR_IntervalNow();
86 char name[1024];
87 sprintf(name, "%s_%d", filename, i);
88 PRFileDesc* fd = PR_Open(name, PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE, 0664);
89 if (fd == NULL)
90 return -1;
92 PRIntervalTime writeStart = PR_IntervalNow();
93 PRInt32 rv = PR_Write(fd, buf, size);
94 if (rv < 0) return rv;
95 if (rv != size) return -1;
96 PRIntervalTime writeStop = PR_IntervalNow();
98 PRStatus st = PR_Close(fd);
99 if (st == PR_FAILURE) return -1;
101 PRIntervalTime stop = PR_IntervalNow();
103 PRIntervalTime writeTime = PR_IntervalToMilliseconds(writeStop - writeStart);
104 if (writeTime > 0) {
105 double wr = size / writeTime;
106 writeRate += wr;
107 writeRateSquared += wr * wr;
108 writeCount++;
111 PRIntervalTime totalTime = PR_IntervalToMilliseconds(stop - start);
112 if (totalTime > 0) {
113 double t = size / totalTime;
114 totalRate += t;
115 totalRateSquared += t * t;
116 totalCount++;
120 PR_Free(buf);
122 double writeMean, writeStddev;
123 double totalMean, totalStddev;
124 NS_MeanAndStdDev(writeCount, writeRate, writeRateSquared,
125 &writeMean, &writeStddev);
126 NS_MeanAndStdDev(totalCount, totalRate, totalRateSquared,
127 &totalMean, &totalStddev);
128 fprintf(stdout, "%10d %10.2f %10.2f %10d %10.2f %10.2f %10d\n",
129 size, writeMean, writeStddev, (PRInt32)writeCount,
130 totalMean, totalStddev, (PRInt32)totalCount);
132 return 0;
136 main(int argc, char* argv[])
138 if (argc != 5) {
139 printf("usage: %s <min buf size (K)> <max buf size (K)> <size increment (K)> <iterations>\n", argv[0]);
140 return -1;
142 Test("y:\\foo",
143 atoi(argv[1]) * 1024,
144 atoi(argv[2]) * 1024,
145 atoi(argv[3]) * 1024,
146 atoi(argv[4]));
147 return 0;