|
| 1 | +package vecxt.benchmark |
| 2 | + |
| 3 | +import org.openjdk.jmh.annotations.* |
| 4 | +import org.openjdk.jmh.infra.Blackhole |
| 5 | +import scala.compiletime.uninitialized |
| 6 | +import scala.util.Random |
| 7 | +import vecxt_re.* |
| 8 | +import vecxt_re.ReReporting.* |
| 9 | +import vecxt.all.* |
| 10 | + |
| 11 | +// ./mill benchmark.runJmh "vecxt.benchmark.LossReportBenchmark" -jvmArgs --add-modules=jdk.incubator.vector -rf json -wi 1 -i 3 -f 1 |
| 12 | + |
| 13 | +/** 231] Benchmark (numEventsStr) (numIterationsStr) Mode Cnt Score Error Units 231] LossReportBenchmark.lossReport_fast |
| 14 | + * 10000 100 thrpt 3 177346.981 ± 24137.324 ops/s 231] LossReportBenchmark.lossReport_fast 10000 1000 thrpt 3 |
| 15 | + * 180400.504 ± 8719.687 ops/s 231] LossReportBenchmark.lossReport_fast 100000 100 thrpt 3 11731.510 ± 1945.957 ops/s |
| 16 | + * 231] LossReportBenchmark.lossReport_fast 100000 1000 thrpt 3 17443.246 ± 425.030 ops/s 231] |
| 17 | + * LossReportBenchmark.lossReport_separate 10000 100 thrpt 3 46850.187 ± 7232.734 ops/s 231] |
| 18 | + * LossReportBenchmark.lossReport_separate 10000 1000 thrpt 3 49876.719 ± 5238.487 ops/s 231] |
| 19 | + * LossReportBenchmark.lossReport_separate 100000 100 thrpt 3 3360.234 ± 326.993 ops/s 231] |
| 20 | + * LossReportBenchmark.lossReport_separate 100000 1000 thrpt 3 4706.819 ± 615.832 ops/s |
| 21 | + */ |
| 22 | + |
| 23 | +@State(Scope.Thread) |
| 24 | +class LossReportBenchmark extends BLASBenchmark: |
| 25 | + |
| 26 | + @Param(Array("10000", "100000")) |
| 27 | + var numEventsStr: String = uninitialized |
| 28 | + |
| 29 | + @Param(Array("100", "1000")) |
| 30 | + var numIterationsStr: String = uninitialized |
| 31 | + |
| 32 | + var years: Array[Int] = uninitialized |
| 33 | + var ceded: Array[Double] = uninitialized |
| 34 | + var layerObj: Layer = uninitialized |
| 35 | + |
| 36 | + @Setup(Level.Trial) |
| 37 | + def setup: Unit = |
| 38 | + val rng = new Random(0) |
| 39 | + val numEvents = numEventsStr.toInt |
| 40 | + val numIterations = numIterationsStr.toInt |
| 41 | + |
| 42 | + val yrs = Array.ofDim[Int](numEvents) |
| 43 | + var i = 0 |
| 44 | + while i < numEvents do |
| 45 | + yrs(i) = rng.nextInt(numIterations) + 1 // 1-based group indices |
| 46 | + i += 1 |
| 47 | + end while |
| 48 | + |
| 49 | + java.util.Arrays.sort(yrs) |
| 50 | + |
| 51 | + years = yrs |
| 52 | + |
| 53 | + ceded = Array.ofDim[Double](numEvents) |
| 54 | + i = 0 |
| 55 | + while i < numEvents do |
| 56 | + // random loss values between 0 and 100 |
| 57 | + ceded(i) = rng.nextDouble() * 100.0 |
| 58 | + i += 1 |
| 59 | + end while |
| 60 | + |
| 61 | + // Choose a layer with a moderate aggLimit to cause some exhaustion hits |
| 62 | + layerObj = Layer(occLimit = Some(100.0), aggLimit = Some(50.0)) |
| 63 | + () |
| 64 | + end setup |
| 65 | + |
| 66 | + @Benchmark |
| 67 | + def lossReport_fast(bh: Blackhole) = |
| 68 | + val calcd = (layerObj, ceded) |
| 69 | + val r = calcd.lossReport(numIterationsStr.toInt, years, ReportDenominator.FirstLimit) |
| 70 | + // consume fields so JMH doesn't optimize away |
| 71 | + bh.consume(r.el) |
| 72 | + bh.consume(r.stdDev) |
| 73 | + bh.consume(r.attachProb) |
| 74 | + bh.consume(r.exhaustProb) |
| 75 | + end lossReport_fast |
| 76 | + |
| 77 | + @Benchmark |
| 78 | + def lossReport_separate(bh: Blackhole) = |
| 79 | + val calcd = (layerObj, ceded) |
| 80 | + val n = numIterationsStr.toInt |
| 81 | + val reportLimit = ReportDenominator.FirstLimit.fromlayer(layerObj) |
| 82 | + |
| 83 | + val el = calcd.expectedLoss(n) / reportLimit |
| 84 | + val std = calcd.std(n, years) / reportLimit |
| 85 | + val attach = calcd.attachmentProbability(n, years) |
| 86 | + val exhaust = calcd.exhaustionProbability(n, years) |
| 87 | + |
| 88 | + bh.consume(el) |
| 89 | + bh.consume(std) |
| 90 | + bh.consume(attach) |
| 91 | + bh.consume(exhaust) |
| 92 | + end lossReport_separate |
| 93 | + |
| 94 | +end LossReportBenchmark |
0 commit comments