1 #!/usr/bin/env dub 2 /+ dub.sdl: 3 name "bench_jwtd" 4 targetType "executable" 5 6 dependency "jwtd" version="~>0.4.6" 7 8 configuration "phobos" { 9 targetName "bench_jwtd_phobos" 10 subConfiguration "jwtd" "phobos" 11 } 12 13 configuration "openssl" { 14 targetName "bench_jwtd_openssl" 15 subConfiguration "jwtd" "openssl-1.1" 16 } 17 18 configuration "botan" { 19 targetName "bench_jwtd_botan" 20 subConfiguration "jwtd" "botan" 21 } 22 +/ 23 24 module bench.jwtd; 25 import core.memory; 26 import std.conv; 27 import std.datetime.stopwatch; 28 import std.json; 29 import std.stdio; 30 import jwtd.jwt; 31 32 int main(string[] args) 33 { 34 // args: enc/dec/val, cycle count, alg, token/payload, signature 35 // output: 36 // payload/token/true 37 // msecs taken 38 // GC used bytes 39 40 if (args.length != 6) { writeln("Invalid args"); return 1; } 41 size_t cycles = args[2].to!size_t; 42 43 JWTAlgorithm alg = args[3].to!JWTAlgorithm; 44 45 { 46 StopWatch sw; 47 sw.start(); 48 immutable prevAllocated = GC.allocatedInCurrentThread; 49 scope (exit) 50 { 51 sw.stop(); 52 writeln(sw.peek.total!"msecs"); 53 writeln(GC.allocatedInCurrentThread - prevAllocated); 54 } 55 56 try 57 { 58 if (args[1] == "val") return validate(cycles, args[4], args[5]); 59 else if (args[1] == "dec") return decode(cycles, args[4], args[5]); 60 else if (args[1] == "enc") return encode(cycles, alg, args[4], args[5]); 61 } 62 catch (Exception ex) 63 { 64 writeln(ex.msg); 65 return 1; 66 } 67 } 68 69 writeln("Invalid command: ", args[1]); 70 return 1; 71 } 72 73 int validate(size_t cycles, string token, string secret) 74 { 75 bool res; 76 foreach (_; 0..cycles) 77 res = verify(token, secret); 78 writeln(res); 79 return 0; 80 } 81 82 int decode(size_t cycles, string token, string secret) 83 { 84 JSONValue res; 85 foreach (_; 0..cycles) 86 res = jwtd.jwt.decode(token, secret); 87 writeln(res); 88 return 0; 89 } 90 91 int encode(size_t cycles, JWTAlgorithm alg, string payload, string secret) 92 { 93 string res; 94 JSONValue pay = parseJSON(payload); 95 foreach (_; 0..cycles) 96 res = jwtd.jwt.encode(pay, secret, alg); 97 writeln(res); 98 return 0; 99 }