Back to index

lightning-sunbird  0.9+nobinonly
TestWriteSpeed.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "prio.h"
00039 #include "prinrval.h"
00040 #include "prmem.h"
00041 #include <stdio.h>
00042 #include <math.h>
00043 
00044 void 
00045 NS_MeanAndStdDev(double n, double sumOfValues, double sumOfSquaredValues,
00046                  double *meanResult, double *stdDevResult)
00047 {
00048   double mean = 0.0, var = 0.0, stdDev = 0.0;
00049   if (n > 0.0 && sumOfValues >= 0) {
00050     mean = sumOfValues / n;
00051     double temp = (n * sumOfSquaredValues) - (sumOfValues * sumOfValues);
00052     if (temp < 0.0 || n <= 1)
00053       var = 0.0;
00054     else
00055       var = temp / (n * (n - 1));
00056     // for some reason, Windows says sqrt(0.0) is "-1.#J" (?!) so do this:
00057     stdDev = var != 0.0 ? sqrt(var) : 0.0;
00058   }
00059   *meanResult = mean;
00060   *stdDevResult = stdDev;
00061 }
00062 
00063 int
00064 Test(const char* filename, PRInt32 minSize, PRInt32 maxSize, 
00065      PRInt32 sizeIncrement, PRInt32 iterations)
00066 {
00067     fprintf(stdout, "      size  write:    mean     stddev      iters  total:    mean     stddev      iters\n");
00068     for (PRInt32 size = minSize; size <= maxSize; size += sizeIncrement) {
00069         // create a buffer of stuff to write
00070         char* buf = (char*)PR_Malloc(size);
00071         if (buf == NULL)
00072             return -1;
00073 
00074         // initialize it with a pattern
00075         PRInt32 i;
00076         char hex[] = "0123456789ABCDEF";
00077         for (i = 0; i < size; i++) {
00078             buf[i] = hex[i & 0xF];
00079         }
00080 
00081         double writeCount = 0, writeRate = 0, writeRateSquared = 0;
00082         double totalCount = 0, totalRate = 0, totalRateSquared = 0;
00083         for (i = 0; i < iterations; i++) {
00084             PRIntervalTime start = PR_IntervalNow();
00085 
00086             char name[1024];
00087             sprintf(name, "%s_%d", filename, i);
00088             PRFileDesc* fd = PR_Open(name, PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE, 0664);
00089             if (fd == NULL)
00090                 return -1;
00091 
00092             PRIntervalTime writeStart = PR_IntervalNow();
00093             PRInt32 rv = PR_Write(fd, buf, size);
00094             if (rv < 0) return rv;
00095             if (rv != size) return -1;
00096             PRIntervalTime writeStop = PR_IntervalNow();
00097 
00098             PRStatus st = PR_Close(fd);
00099             if (st == PR_FAILURE) return -1; 
00100 
00101             PRIntervalTime stop = PR_IntervalNow();
00102 
00103             PRIntervalTime writeTime = PR_IntervalToMilliseconds(writeStop - writeStart);
00104             if (writeTime > 0) {
00105                 double wr = size / writeTime;
00106                 writeRate += wr;
00107                 writeRateSquared += wr * wr;
00108                 writeCount++;
00109             }
00110 
00111             PRIntervalTime totalTime = PR_IntervalToMilliseconds(stop - start);
00112             if (totalTime > 0) {
00113                 double t = size / totalTime;
00114                 totalRate += t;
00115                 totalRateSquared += t * t;
00116                 totalCount++;
00117             }
00118         }
00119 
00120         PR_Free(buf);
00121 
00122         double writeMean, writeStddev;
00123         double totalMean, totalStddev;
00124         NS_MeanAndStdDev(writeCount, writeRate, writeRateSquared,
00125                          &writeMean, &writeStddev);
00126         NS_MeanAndStdDev(totalCount, totalRate, totalRateSquared,
00127                          &totalMean, &totalStddev);
00128         fprintf(stdout, "%10d      %10.2f %10.2f %10d      %10.2f %10.2f %10d\n",
00129                 size, writeMean, writeStddev, (PRInt32)writeCount, 
00130                 totalMean, totalStddev, (PRInt32)totalCount);
00131     }
00132     return 0;
00133 }
00134 
00135 int
00136 main(int argc, char* argv[])
00137 {
00138     if (argc != 5) {
00139         printf("usage: %s <min buf size (K)> <max buf size (K)> <size increment (K)> <iterations>\n", argv[0]);
00140         return -1;
00141     }
00142     Test("y:\\foo",
00143          atoi(argv[1]) * 1024,
00144          atoi(argv[2]) * 1024,
00145          atoi(argv[3]) * 1024,
00146          atoi(argv[4]));
00147     return 0;
00148 }