|
| 1 | +package benchmarks |
| 2 | + |
| 3 | +import DebuggerTestBase |
| 4 | +import be.ugent.topl.mio.debugger.Debugger |
| 5 | +import getBinaryInfo |
| 6 | +import org.junit.jupiter.api.Test |
| 7 | +import java.io.File |
| 8 | +import java.io.FileWriter |
| 9 | +import java.lang.System.currentTimeMillis |
| 10 | + |
| 11 | +/** |
| 12 | + * Benchmarks which use a headless version of MIO to test the performance of various operations. To get accurate |
| 13 | + * performance estimates they should be performed with an actual microcontroller since communication latency is an |
| 14 | + * important factor. |
| 15 | + */ |
| 16 | +class Benchmarks : DebuggerTestBase() { |
| 17 | + /** |
| 18 | + * Executes the code using continueFor over n instructions and measures how much time it takes with different |
| 19 | + * checkpointing strategies. |
| 20 | + */ |
| 21 | + @Test |
| 22 | + fun `Measure impact of checkpointing on forward execution`() { |
| 23 | + val writer = FileWriter(File("results-forward-execution.csv")) |
| 24 | + writer.write("Policy, Instructions executed, Time elapsed\n") |
| 25 | + val results = mutableListOf<Triple<Int, Double, Debugger.SnapshotPolicy>>() |
| 26 | + for (policy in listOf( |
| 27 | + Debugger.SnapshotPolicy.None(), |
| 28 | + Debugger.SnapshotPolicy.Checkpointing(1), |
| 29 | + Debugger.SnapshotPolicy.Checkpointing(5), |
| 30 | + Debugger.SnapshotPolicy.Checkpointing(10), |
| 31 | + Debugger.SnapshotPolicy.Checkpointing(50), |
| 32 | + Debugger.SnapshotPolicy.Checkpointing(100))) { |
| 33 | + |
| 34 | + for (n in 250 ..< 1500 step 250) { |
| 35 | + println("Progress $n/1500") |
| 36 | + var totalTime = 0L |
| 37 | + val times = 10 |
| 38 | + repeat(times) { |
| 39 | + runWithDebugger("prime/prime-no-mem.wasm", true) { |
| 40 | + it.setSnapshotPolicy(policy) |
| 41 | + val startTime = currentTimeMillis() |
| 42 | + it.continueFor(n) |
| 43 | + totalTime += currentTimeMillis() - startTime |
| 44 | + } |
| 45 | + } |
| 46 | + writer.write("$policy, $n, ${totalTime.toDouble() / times}\n") |
| 47 | + writer.flush() |
| 48 | + } |
| 49 | + } |
| 50 | + writer.close() |
| 51 | + |
| 52 | + println("Policy, Instructions executed, Time elapsed") |
| 53 | + for (result in results) { |
| 54 | + println("${result.third}, ${result.first}, ${result.second}") |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Executes 1000 instructions, then steps back 1, needing 999 instructions to be re-executed. Because checkpoints |
| 60 | + * are deleted, it first has to re-execute 999 then 1999 then 2999 and so on. Since it steps back one instruction it |
| 61 | + * also needs to step forward. |
| 62 | + */ |
| 63 | + @Test |
| 64 | + fun `Measure re-execution speed when stepping back`() { |
| 65 | + val wasmFile = "prime/prime-no-mem.wasm" |
| 66 | + val binaryInfo = getBinaryInfo(config.symbolicWdcliPath, getFile(wasmFile).absolutePath) |
| 67 | + val results = mutableListOf<List<Pair<Int, Long>>>() |
| 68 | + repeat(10) { |
| 69 | + runWithDebugger(wasmFile, true) { |
| 70 | + var t = 0 |
| 71 | + val timings = mutableListOf<Pair<Int, Long>>() |
| 72 | + it.setSnapshotPolicy(Debugger.SnapshotPolicy.Checkpointing(0xffffff)) |
| 73 | + it.stepInto() |
| 74 | + it.checkpoints[it.checkpoints.size - 1] = null |
| 75 | + timings.add(Pair(t, timeElapsed { |
| 76 | + it.stepBack(1, binaryInfo) {} |
| 77 | + })) |
| 78 | + |
| 79 | + (0..30).forEach { i -> |
| 80 | + it.continueFor(1000) |
| 81 | + it.checkpoints[it.checkpoints.size - 1] = null |
| 82 | + t += 999 |
| 83 | + timings.add(Pair(t, timeElapsed { |
| 84 | + it.stepBack(1, binaryInfo) {} |
| 85 | + })) |
| 86 | + it.checkpoints[it.checkpoints.size - 1] = null |
| 87 | + } |
| 88 | + println(timings) |
| 89 | + results.add(timings) |
| 90 | + } |
| 91 | + } |
| 92 | + val average = mutableListOf<Pair<Int, Float>>() |
| 93 | + for (i in results[0].indices) { |
| 94 | + var sum = 0.0 |
| 95 | + for (run in results.indices) { |
| 96 | + sum += results[run][i].second |
| 97 | + } |
| 98 | + average.add(Pair(results[0][i].first, sum.toFloat() / results.size)) |
| 99 | + } |
| 100 | + val writer = FileWriter(File("results-step-back-re-execution.csv")) |
| 101 | + writer.write("t, avg_time\n") |
| 102 | + for (pair in average) { |
| 103 | + writer.write("${pair.first}, ${pair.second}\n") |
| 104 | + } |
| 105 | + writer.close() |
| 106 | + } |
| 107 | + |
| 108 | + private fun timeElapsed(action: () -> Unit): Long { |
| 109 | + val startTime = currentTimeMillis() |
| 110 | + action() |
| 111 | + return currentTimeMillis() - startTime |
| 112 | + } |
| 113 | +} |
0 commit comments