Wed, 31 Dec 2014 06:55:46 +0100
Added tag TORBROWSER_REPLICA for changeset 6474c204b198
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "prio.h" |
michael@0 | 7 | #include "prinrval.h" |
michael@0 | 8 | #include "prmem.h" |
michael@0 | 9 | #include <stdio.h> |
michael@0 | 10 | #include <math.h> |
michael@0 | 11 | |
michael@0 | 12 | void |
michael@0 | 13 | NS_MeanAndStdDev(double n, double sumOfValues, double sumOfSquaredValues, |
michael@0 | 14 | double *meanResult, double *stdDevResult) |
michael@0 | 15 | { |
michael@0 | 16 | double mean = 0.0, var = 0.0, stdDev = 0.0; |
michael@0 | 17 | if (n > 0.0 && sumOfValues >= 0) { |
michael@0 | 18 | mean = sumOfValues / n; |
michael@0 | 19 | double temp = (n * sumOfSquaredValues) - (sumOfValues * sumOfValues); |
michael@0 | 20 | if (temp < 0.0 || n <= 1) |
michael@0 | 21 | var = 0.0; |
michael@0 | 22 | else |
michael@0 | 23 | var = temp / (n * (n - 1)); |
michael@0 | 24 | // for some reason, Windows says sqrt(0.0) is "-1.#J" (?!) so do this: |
michael@0 | 25 | stdDev = var != 0.0 ? sqrt(var) : 0.0; |
michael@0 | 26 | } |
michael@0 | 27 | *meanResult = mean; |
michael@0 | 28 | *stdDevResult = stdDev; |
michael@0 | 29 | } |
michael@0 | 30 | |
michael@0 | 31 | int |
michael@0 | 32 | Test(const char* filename, int32_t minSize, int32_t maxSize, |
michael@0 | 33 | int32_t sizeIncrement, int32_t iterations) |
michael@0 | 34 | { |
michael@0 | 35 | fprintf(stdout, " size write: mean stddev iters total: mean stddev iters\n"); |
michael@0 | 36 | for (int32_t size = minSize; size <= maxSize; size += sizeIncrement) { |
michael@0 | 37 | // create a buffer of stuff to write |
michael@0 | 38 | char* buf = (char*)PR_Malloc(size); |
michael@0 | 39 | if (buf == nullptr) |
michael@0 | 40 | return -1; |
michael@0 | 41 | |
michael@0 | 42 | // initialize it with a pattern |
michael@0 | 43 | int32_t i; |
michael@0 | 44 | char hex[] = "0123456789ABCDEF"; |
michael@0 | 45 | for (i = 0; i < size; i++) { |
michael@0 | 46 | buf[i] = hex[i & 0xF]; |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | double writeCount = 0, writeRate = 0, writeRateSquared = 0; |
michael@0 | 50 | double totalCount = 0, totalRate = 0, totalRateSquared = 0; |
michael@0 | 51 | for (i = 0; i < iterations; i++) { |
michael@0 | 52 | PRIntervalTime start = PR_IntervalNow(); |
michael@0 | 53 | |
michael@0 | 54 | char name[1024]; |
michael@0 | 55 | sprintf(name, "%s_%d", filename, i); |
michael@0 | 56 | PRFileDesc* fd = PR_Open(name, PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE, 0664); |
michael@0 | 57 | if (fd == nullptr) |
michael@0 | 58 | return -1; |
michael@0 | 59 | |
michael@0 | 60 | PRIntervalTime writeStart = PR_IntervalNow(); |
michael@0 | 61 | int32_t rv = PR_Write(fd, buf, size); |
michael@0 | 62 | if (rv < 0) return rv; |
michael@0 | 63 | if (rv != size) return -1; |
michael@0 | 64 | PRIntervalTime writeStop = PR_IntervalNow(); |
michael@0 | 65 | |
michael@0 | 66 | PRStatus st = PR_Close(fd); |
michael@0 | 67 | if (st == PR_FAILURE) return -1; |
michael@0 | 68 | |
michael@0 | 69 | PRIntervalTime stop = PR_IntervalNow(); |
michael@0 | 70 | |
michael@0 | 71 | PRIntervalTime writeTime = PR_IntervalToMilliseconds(writeStop - writeStart); |
michael@0 | 72 | if (writeTime > 0) { |
michael@0 | 73 | double wr = size / writeTime; |
michael@0 | 74 | writeRate += wr; |
michael@0 | 75 | writeRateSquared += wr * wr; |
michael@0 | 76 | writeCount++; |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | PRIntervalTime totalTime = PR_IntervalToMilliseconds(stop - start); |
michael@0 | 80 | if (totalTime > 0) { |
michael@0 | 81 | double t = size / totalTime; |
michael@0 | 82 | totalRate += t; |
michael@0 | 83 | totalRateSquared += t * t; |
michael@0 | 84 | totalCount++; |
michael@0 | 85 | } |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | PR_Free(buf); |
michael@0 | 89 | |
michael@0 | 90 | double writeMean, writeStddev; |
michael@0 | 91 | double totalMean, totalStddev; |
michael@0 | 92 | NS_MeanAndStdDev(writeCount, writeRate, writeRateSquared, |
michael@0 | 93 | &writeMean, &writeStddev); |
michael@0 | 94 | NS_MeanAndStdDev(totalCount, totalRate, totalRateSquared, |
michael@0 | 95 | &totalMean, &totalStddev); |
michael@0 | 96 | fprintf(stdout, "%10d %10.2f %10.2f %10d %10.2f %10.2f %10d\n", |
michael@0 | 97 | size, writeMean, writeStddev, (int32_t)writeCount, |
michael@0 | 98 | totalMean, totalStddev, (int32_t)totalCount); |
michael@0 | 99 | } |
michael@0 | 100 | return 0; |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | int |
michael@0 | 104 | main(int argc, char* argv[]) |
michael@0 | 105 | { |
michael@0 | 106 | if (argc != 5) { |
michael@0 | 107 | printf("usage: %s <min buf size (K)> <max buf size (K)> <size increment (K)> <iterations>\n", argv[0]); |
michael@0 | 108 | return -1; |
michael@0 | 109 | } |
michael@0 | 110 | Test("y:\\foo", |
michael@0 | 111 | atoi(argv[1]) * 1024, |
michael@0 | 112 | atoi(argv[2]) * 1024, |
michael@0 | 113 | atoi(argv[3]) * 1024, |
michael@0 | 114 | atoi(argv[4])); |
michael@0 | 115 | return 0; |
michael@0 | 116 | } |