1 module dcrypt.benchmark.Benchmark;
2 
3 public import std.datetime: StopWatch;
4 import std.conv;
5 import dcrypt.benchmark.BlockCipherBenchmark;
6 import dcrypt.benchmark.AEADCipherBenchmark;
7 import dcrypt.benchmark.DigestBenchmark;
8 import dcrypt.blockcipher.blockcipher;
9 import dcrypt.digest;
10 import dcrypt.aead.aead;
11 
12 import std.stdio;
13 import std..string;
14 
15 public class Benchmark {
16 
17 	
18 	/// Params:
19 	/// length	=	the length of benchmark in bytes
20 	/// ciphers	=	BlockCiphers to test
21 	public static void doBenchmark(ulong length, IBlockCipher[] ciphers...) {
22 		writeln();
23 
24 		printTabbed(BlockCipherBenchmark.header);
25 		writeln();
26 		foreach(c; ciphers) {
27 			auto bench = new BlockCipherBenchmark(c);
28 			printTabbed(bench.benchmark(length));
29 			stdout.flush();
30 		}
31 	}
32 
33 	public static void doBenchmark(ulong length, IAEADEngine[] ciphers...) {
34 		writeln();
35 		printTabbed(AEADCipherBenchmark.header);
36 		writeln();
37 		foreach(c; ciphers) {
38 			auto bench = new AEADCipherBenchmark(c);
39 			printTabbed(bench.benchmark(length));
40 			stdout.flush();
41 		}
42 	}
43 	
44 	public static void doBenchmark(ulong length, Digest[] digests...) {
45 		writeln();
46 		printTabbed(DigestBenchmark.header);
47 		writeln();
48 		foreach(d; digests) {
49 			auto bench = new DigestBenchmark(d);
50 			printTabbed(bench.benchmark(length));
51 			stdout.flush();
52 		}
53 	}
54 
55 	public static void doCurved25519Benchmark(ulong length) {
56 		import dcrypt.benchmark.curved25519;
57 		
58 		writeln();
59 		printTabbed(Curved25519Benchmark.header);
60 		writeln();
61 		auto bench = new Curved25519Benchmark;
62 		printTabbed(bench.benchmark(length));
63 		stdout.flush();
64 	}
65 
66 	public static void doEd25519Benchmark(ulong length) {
67 		import dcrypt.benchmark.curved25519;
68 		
69 		writeln();
70 		printTabbed(Ed25519Benchmark.header);
71 		writeln();
72 		auto bench = new Ed25519Benchmark;
73 		printTabbed(bench.benchmark(length));
74 		stdout.flush();
75 	}
76 
77 	public static void doSphincs256Benchmark(ulong length) {
78 		import dcrypt.benchmark.sphincs256;
79 		
80 		writeln();
81 		printTabbed(Sphincs256Benchmark.header);
82 		writeln();
83 		auto bench = new Sphincs256Benchmark;
84 		printTabbed(bench.benchmark(length));
85 		stdout.flush();
86 	}
87 
88 	/// do the calculations, (compute hashes, encrypt data, ...)
89 	/// Params: length = length of benchmark (numbers of bytes to process)
90 	/// Returns: a string containing the benchmark results
91 	public abstract string[] benchmark(ulong length);
92 
93 	@trusted
94 	static void printTabbed(string[] strs...) {
95 		writefln("%-(%-20s%)", strs);
96 	}
97 
98 	public string numberFormat(double d) {
99 		return format("%10.2f", d);
100 	}
101 }