TestWriteSpeed.cpp 3.8 KB

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