1 /** 2 Copyright: Copyright (c) 2017-2019 Andrey Penechko. 3 License: $(WEB boost.org/LICENSE_1_0.txt, Boost License 1.0). 4 Authors: Andrey Penechko. 5 */ 6 module bench; 7 8 import vox.all; 9 import tester; 10 import std.stdio; 11 import std.string : stripLeft; 12 import tests.passing; 13 14 /// --tsv output in tsv format with greek µ instead of u 15 int runBench(string[] args) 16 { 17 bool outputTsv; 18 foreach(arg; args) if (arg == "--tsv") outputTsv = true; 19 20 Test curTest = makeTest!(tests.passing.test21); 21 //Test curTest = makeTest!(tests.passing.test31); 22 23 Driver driver; 24 driver.initialize(jitPasses); 25 //driver.context.validateIr = true; 26 scope(exit) driver.releaseMemory; 27 28 enum iters = 100_000; 29 auto times = PerPassTimeMeasurements(iters, driver.passes); 30 31 foreach (iteration; 0..times.totalTimes.numIters) 32 { 33 auto time1 = currTime; 34 driver.beginCompilation(); 35 driver.addHostSymbols(curTest.hostSymbols); 36 string strippedHar = curTest.harData.stripLeft; 37 driver.addHar("test.har", strippedHar); 38 driver.compile(); 39 auto time2 = currTime; 40 41 times.onIteration(iteration, time2-time1); 42 } 43 44 driver.context.printMemSize; 45 46 if (outputTsv) times.printTsv; 47 else times.print; 48 49 return 0; 50 } 51 52 immutable input = q{--- fibonacci 53 // test fibonacci. while loop. func call 54 void print(i32){} 55 i32 fibonacci(i32 max) { 56 i32 lo = 0; 57 i32 hi = 1; 58 while (hi < max) { 59 hi = hi + lo; 60 lo = hi - lo; 61 print(lo); 62 } 63 return lo; 64 } 65 }; 66 67 int benchSpeed() 68 { 69 auto test = Test("Fib", input); 70 71 Driver driver; 72 driver.initialize(jitPasses); 73 scope(exit) driver.releaseMemory; 74 75 driver.beginCompilation(); 76 driver.addHostSymbols(test.hostSymbols); 77 string strippedHar = test.harData.stripLeft; 78 driver.addHar("test.har", strippedHar); 79 driver.compile(); 80 driver.markCodeAsExecutable(); 81 82 auto fib = driver.context.getFunctionPtr!(int, int)("fibonacci"); 83 84 auto time1 = currTime; 85 int res; 86 foreach (iteration; 0..100_000) 87 { 88 res = fib(1073741824); 89 } 90 auto time2 = currTime; 91 92 writefln("run time %ss", scaledNumberFmt(time2-time1)); 93 writefln("res %s", res); 94 95 return 0; 96 }