1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/test/TestWriteSpeed.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,116 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 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 "prinrval.h" 1.11 +#include "prmem.h" 1.12 +#include <stdio.h> 1.13 +#include <math.h> 1.14 + 1.15 +void 1.16 +NS_MeanAndStdDev(double n, double sumOfValues, double sumOfSquaredValues, 1.17 + double *meanResult, double *stdDevResult) 1.18 +{ 1.19 + double mean = 0.0, var = 0.0, stdDev = 0.0; 1.20 + if (n > 0.0 && sumOfValues >= 0) { 1.21 + mean = sumOfValues / n; 1.22 + double temp = (n * sumOfSquaredValues) - (sumOfValues * sumOfValues); 1.23 + if (temp < 0.0 || n <= 1) 1.24 + var = 0.0; 1.25 + else 1.26 + var = temp / (n * (n - 1)); 1.27 + // for some reason, Windows says sqrt(0.0) is "-1.#J" (?!) so do this: 1.28 + stdDev = var != 0.0 ? sqrt(var) : 0.0; 1.29 + } 1.30 + *meanResult = mean; 1.31 + *stdDevResult = stdDev; 1.32 +} 1.33 + 1.34 +int 1.35 +Test(const char* filename, int32_t minSize, int32_t maxSize, 1.36 + int32_t sizeIncrement, int32_t iterations) 1.37 +{ 1.38 + fprintf(stdout, " size write: mean stddev iters total: mean stddev iters\n"); 1.39 + for (int32_t size = minSize; size <= maxSize; size += sizeIncrement) { 1.40 + // create a buffer of stuff to write 1.41 + char* buf = (char*)PR_Malloc(size); 1.42 + if (buf == nullptr) 1.43 + return -1; 1.44 + 1.45 + // initialize it with a pattern 1.46 + int32_t i; 1.47 + char hex[] = "0123456789ABCDEF"; 1.48 + for (i = 0; i < size; i++) { 1.49 + buf[i] = hex[i & 0xF]; 1.50 + } 1.51 + 1.52 + double writeCount = 0, writeRate = 0, writeRateSquared = 0; 1.53 + double totalCount = 0, totalRate = 0, totalRateSquared = 0; 1.54 + for (i = 0; i < iterations; i++) { 1.55 + PRIntervalTime start = PR_IntervalNow(); 1.56 + 1.57 + char name[1024]; 1.58 + sprintf(name, "%s_%d", filename, i); 1.59 + PRFileDesc* fd = PR_Open(name, PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE, 0664); 1.60 + if (fd == nullptr) 1.61 + return -1; 1.62 + 1.63 + PRIntervalTime writeStart = PR_IntervalNow(); 1.64 + int32_t rv = PR_Write(fd, buf, size); 1.65 + if (rv < 0) return rv; 1.66 + if (rv != size) return -1; 1.67 + PRIntervalTime writeStop = PR_IntervalNow(); 1.68 + 1.69 + PRStatus st = PR_Close(fd); 1.70 + if (st == PR_FAILURE) return -1; 1.71 + 1.72 + PRIntervalTime stop = PR_IntervalNow(); 1.73 + 1.74 + PRIntervalTime writeTime = PR_IntervalToMilliseconds(writeStop - writeStart); 1.75 + if (writeTime > 0) { 1.76 + double wr = size / writeTime; 1.77 + writeRate += wr; 1.78 + writeRateSquared += wr * wr; 1.79 + writeCount++; 1.80 + } 1.81 + 1.82 + PRIntervalTime totalTime = PR_IntervalToMilliseconds(stop - start); 1.83 + if (totalTime > 0) { 1.84 + double t = size / totalTime; 1.85 + totalRate += t; 1.86 + totalRateSquared += t * t; 1.87 + totalCount++; 1.88 + } 1.89 + } 1.90 + 1.91 + PR_Free(buf); 1.92 + 1.93 + double writeMean, writeStddev; 1.94 + double totalMean, totalStddev; 1.95 + NS_MeanAndStdDev(writeCount, writeRate, writeRateSquared, 1.96 + &writeMean, &writeStddev); 1.97 + NS_MeanAndStdDev(totalCount, totalRate, totalRateSquared, 1.98 + &totalMean, &totalStddev); 1.99 + fprintf(stdout, "%10d %10.2f %10.2f %10d %10.2f %10.2f %10d\n", 1.100 + size, writeMean, writeStddev, (int32_t)writeCount, 1.101 + totalMean, totalStddev, (int32_t)totalCount); 1.102 + } 1.103 + return 0; 1.104 +} 1.105 + 1.106 +int 1.107 +main(int argc, char* argv[]) 1.108 +{ 1.109 + if (argc != 5) { 1.110 + printf("usage: %s <min buf size (K)> <max buf size (K)> <size increment (K)> <iterations>\n", argv[0]); 1.111 + return -1; 1.112 + } 1.113 + Test("y:\\foo", 1.114 + atoi(argv[1]) * 1024, 1.115 + atoi(argv[2]) * 1024, 1.116 + atoi(argv[3]) * 1024, 1.117 + atoi(argv[4])); 1.118 + return 0; 1.119 +}