bench.d 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #!/usr/bin/env dub
  2. /+ dub.json:
  3. {
  4. "name": "bench",
  5. "dependencies": {"deuterium": {"path":"../"}}
  6. }
  7. +/
  8. import core.memory : GC;
  9. import core.time : Duration, MonoTime, hnsecs, seconds, to;
  10. import std.stdio : write, writeln, writefln;
  11. import deuterium.random : getrandom;
  12. private {
  13. __gshared Duration g_minTimingInterval;
  14. immutable size_t[] szs = [1, 64, 128, 256, 512, 1024, 65536];
  15. }
  16. void main() {
  17. getClockPrecision();
  18. benchX25519();
  19. benchChaCha20();
  20. benchPoly1305();
  21. benchSipHash24();
  22. }
  23. private void benchChaCha20() {
  24. import deuterium.stream.chacha20 : ChaCha20, g_keySize, g_ivSize;
  25. writeln("ChaCha20:");
  26. foreach (size_t sz; szs) {
  27. ubyte[g_keySize] key;
  28. ubyte[g_ivSize] iv;
  29. key.getrandom();
  30. iv.getrandom();
  31. auto s = new ChaCha20(key, iv);
  32. ubyte[] dst;
  33. dst.length = sz;
  34. void oneIter() {
  35. s.xorKeystream(dst, dst);
  36. }
  37. auto elapsed = benchmark(&oneIter);
  38. printSizedResult(elapsed, sz);
  39. }
  40. writeln();
  41. }
  42. private void benchPoly1305() {
  43. import deuterium.mac.poly1305 : Poly1305, g_keySize, g_tagSize;
  44. writeln("Poly1305:");
  45. foreach (size_t sz; szs) {
  46. ubyte[g_keySize] key;
  47. ubyte[g_tagSize] tag;
  48. key.getrandom();
  49. auto m = new Poly1305();
  50. ubyte[] msg;
  51. msg.length = sz;
  52. msg.getrandom();
  53. void oneIter() {
  54. m.initialize(key);
  55. m.update(msg);
  56. m.finish(tag);
  57. }
  58. auto elapsed = benchmark(&oneIter);
  59. printSizedResult(elapsed, sz);
  60. }
  61. writeln();
  62. }
  63. private void benchSipHash24() {
  64. import deuterium.mac.siphash : SipHash24, g_keySize, g_tagSize;
  65. writeln("SipHash-2-4:");
  66. foreach (size_t sz; szs) {
  67. ubyte[g_keySize] key;
  68. ubyte[g_tagSize] tag;
  69. key.getrandom();
  70. auto m = new SipHash24();
  71. ubyte[] msg;
  72. msg.length = sz;
  73. msg.getrandom();
  74. void oneIter() {
  75. m.initialize(key);
  76. m.update(msg);
  77. m.finish(tag);
  78. }
  79. auto elapsed = benchmark(&oneIter);
  80. printSizedResult(elapsed, sz);
  81. }
  82. writeln();
  83. }
  84. private void benchX25519() {
  85. import deuterium.ecc.x25519 : scalarBaseMult, scalarMult, g_keySize;
  86. writeln("X25519:");
  87. ubyte[g_keySize] key, bp, ss;
  88. bp[0] = 9;
  89. key.getrandom();
  90. void oneScalarMultIter() {
  91. scalarMult(ss, key, bp);
  92. }
  93. auto elapsed = benchmark(&oneScalarMultIter);
  94. write(" scalarMult\t");
  95. printTimedResult(elapsed);
  96. void oneScalarBaseMultIter() {
  97. scalarBaseMult(ss, key);
  98. }
  99. elapsed = benchmark(&oneScalarBaseMultIter);
  100. write(" scalarBaseMult\t");
  101. printTimedResult(elapsed);
  102. writeln();
  103. }
  104. private void getClockPrecision() {
  105. immutable auto res = MonoTime.ticksPerSecond;
  106. g_minTimingInterval = seconds(1);
  107. g_minTimingInterval /= res;
  108. if (g_minTimingInterval < hnsecs(1)) {
  109. g_minTimingInterval = hnsecs(1);
  110. }
  111. }
  112. private double benchmark(void delegate() fn) {
  113. // This is inspired by Andrew Moon's chacha-opt package.
  114. ulong batchSize = 1;
  115. ulong trials = 1;
  116. GC.disable();
  117. // Calculate the batch size and number of trials.
  118. while (true) {
  119. Duration tbest = Duration.max;
  120. for (auto i = 0; i < 100; ++i) {
  121. immutable auto start = MonoTime.currTime;
  122. for (auto j = 0; j < batchSize; ++j) {
  123. fn();
  124. }
  125. immutable auto elapsed = MonoTime.currTime - start;
  126. tbest = (elapsed < tbest) ? elapsed : tbest;
  127. }
  128. if (tbest > g_minTimingInterval * 25) {
  129. trials = seconds(1) / tbest;
  130. if (trials < 1) {
  131. trials = 1;
  132. }
  133. break;
  134. }
  135. batchSize = (batchSize == 1) ? 2 : (((batchSize * 4) / 3) + 1);
  136. }
  137. // Measure.
  138. Duration tbest = Duration.max;
  139. for (auto i = 0; i < trials; ++i) {
  140. immutable auto start = MonoTime.currTime;
  141. for (auto j = 0; j < batchSize; ++j) {
  142. fn();
  143. }
  144. immutable auto elapsed = MonoTime.currTime - start;
  145. tbest = (elapsed < tbest) ? elapsed : tbest;
  146. }
  147. GC.enable();
  148. GC.collect();
  149. return cast(double)(tbest.total!("nsecs")) / batchSize;
  150. }
  151. private void printTimedResult(immutable double nsPerIter) {
  152. writefln(" %f ns/op", nsPerIter);
  153. }
  154. private void printSizedResult(immutable double nsPerIter, immutable size_t sz) {
  155. auto nsPerByte = nsPerIter / sz;
  156. auto bytesPerSec = cast(size_t)(1000000000.0 / nsPerByte);
  157. writefln(" %d\t\t%s/s (%f ns/byte)", sz, bytesPerSec.toHuman, nsPerByte);
  158. }
  159. private string toHuman(size_t n) {
  160. import core.bitop : bsr;
  161. import std.array : appender;
  162. import std.conv : to;
  163. immutable string[] prefixes = ["Bytes", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"];
  164. immutable auto idx = bsr(n) / 10;
  165. immutable auto divisor = 1uL << (idx * 10);
  166. auto s = appender!string;
  167. s.put(to!string(to!double(n) / divisor));
  168. s.put(" ");
  169. s.put(prefixes[idx]);
  170. return s.data;
  171. }
  172. debug: static assert(0, "Why are you compiling this without optimization?");