1 module dcrypt.benchmark.BlockCipherBenchmark; 2 3 import dcrypt.benchmark.Benchmark; 4 import dcrypt.blockcipher.blockcipher; 5 import std.conv; 6 import std.algorithm: swap; 7 8 public class BlockCipherBenchmark: Benchmark { 9 10 private IBlockCipher cipher; 11 12 this (IBlockCipher c){ 13 cipher = c; 14 } 15 16 @property 17 static string[] header() { 18 return ["algorithm", "encrypt MB/s", "decrypt MB/s"]; 19 } 20 21 override string[] benchmark(ulong length) { 22 23 cipher.start(true, new ubyte[16], new ubyte[16]); 24 25 double encrSpeed = getSpeed(length)*1e-6; 26 27 cipher.start(false, new ubyte[16], new ubyte[16]); 28 29 double decrSpeed = getSpeed(length)*1e-6; 30 31 return [cipher.name, numberFormat(encrSpeed), numberFormat(decrSpeed)]; 32 } 33 34 private double getSpeed(ulong length) { 35 ubyte[] blockA = new ubyte[cipher.blockSize()]; 36 ubyte[] blockB = new ubyte[cipher.blockSize()]; 37 StopWatch sw; 38 sw.reset(); 39 sw.start(); 40 foreach(size_t i; 0 .. length/blockA.length) { 41 42 cipher.processBlock(blockA, blockB); 43 swap(blockA, blockB); 44 } 45 sw.stop(); 46 47 double speed = 1e9 * length / sw.peek().nsecs(); 48 return speed; 49 } 50 51 }