|
| 1 | +/* |
| 2 | + * Benchmark for isolated group operations (groupCumSum, groupDiff, groupSum) |
| 3 | + * Testing performance of these hot-path operations |
| 4 | + */ |
| 5 | + |
| 6 | +package vecxt.benchmark |
| 7 | + |
| 8 | +import org.openjdk.jmh.annotations.* |
| 9 | +import org.openjdk.jmh.infra.Blackhole |
| 10 | +import scala.compiletime.uninitialized |
| 11 | +import vecxtensions.* |
| 12 | +import java.util.Random |
| 13 | +import java.util.concurrent.TimeUnit |
| 14 | + |
| 15 | +@State(Scope.Thread) |
| 16 | +@BenchmarkMode(Array(Mode.AverageTime)) |
| 17 | +@OutputTimeUnit(TimeUnit.NANOSECONDS) |
| 18 | +@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) |
| 19 | +@Measurement(iterations = 10, time = 1, timeUnit = TimeUnit.SECONDS) |
| 20 | +@Fork(1) |
| 21 | +class GroupOpsBenchmark: |
| 22 | + |
| 23 | + @Param(Array("100", "1000", "10000", "100000")) |
| 24 | + var size: String = uninitialized |
| 25 | + |
| 26 | + @Param(Array("10", "100", "1000")) |
| 27 | + var groupSize: String = uninitialized |
| 28 | + |
| 29 | + var groups: Array[Int] = uninitialized |
| 30 | + var values: Array[Double] = uninitialized |
| 31 | + var valuesCopy1: Array[Double] = uninitialized |
| 32 | + var valuesCopy2: Array[Double] = uninitialized |
| 33 | + |
| 34 | + @Setup(Level.Invocation) |
| 35 | + def setup: Unit = |
| 36 | + val n = size.toInt |
| 37 | + val avgGroupSize = groupSize.toInt |
| 38 | + val numGroups = math.max(1, n / avgGroupSize) |
| 39 | + |
| 40 | + val random = new Random(42) // Fixed seed for reproducibility |
| 41 | + |
| 42 | + // Generate sorted groups array with realistic group sizes |
| 43 | + groups = new Array[Int](n) |
| 44 | + var currentGroup = 0 |
| 45 | + var i = 0 |
| 46 | + while i < n do |
| 47 | + val remainingElements = n - i |
| 48 | + val remainingGroups = numGroups - currentGroup |
| 49 | + val targetGroupSize = |
| 50 | + if remainingGroups > 0 then remainingElements / remainingGroups |
| 51 | + else remainingElements |
| 52 | + |
| 53 | + val actualGroupSize = math.max(1, targetGroupSize + random.nextInt(avgGroupSize / 2 + 1) - avgGroupSize / 4) |
| 54 | + val groupEnd = math.min(i + actualGroupSize, n) |
| 55 | + |
| 56 | + while i < groupEnd do |
| 57 | + groups(i) = currentGroup |
| 58 | + i += 1 |
| 59 | + end while |
| 60 | + |
| 61 | + currentGroup += 1 |
| 62 | + end while |
| 63 | + |
| 64 | + // Generate random values |
| 65 | + values = Array.fill(n)(random.nextDouble() * 100.0) |
| 66 | + valuesCopy1 = values.clone() |
| 67 | + valuesCopy2 = values.clone() |
| 68 | + end setup |
| 69 | + |
| 70 | + @Benchmark |
| 71 | + def benchGroupCumSum(bh: Blackhole): Unit = |
| 72 | + val result = groupCumSum(groups, values) |
| 73 | + bh.consume(result) |
| 74 | + end benchGroupCumSum |
| 75 | + |
| 76 | + @Benchmark |
| 77 | + def benchGroupCumSumInPlace(bh: Blackhole): Unit = |
| 78 | + groupCumSumInPlace(groups, valuesCopy1) |
| 79 | + bh.consume(valuesCopy1) |
| 80 | + end benchGroupCumSumInPlace |
| 81 | + |
| 82 | + @Benchmark |
| 83 | + def benchGroupDiff(bh: Blackhole): Unit = |
| 84 | + val result = groupDiff(groups, values) |
| 85 | + bh.consume(result) |
| 86 | + end benchGroupDiff |
| 87 | + |
| 88 | + @Benchmark |
| 89 | + def benchGroupDiffInPlace(bh: Blackhole): Unit = |
| 90 | + groupDiffInPlace(groups, valuesCopy2) |
| 91 | + bh.consume(valuesCopy2) |
| 92 | + end benchGroupDiffInPlace |
| 93 | + |
| 94 | + @Benchmark |
| 95 | + def benchGroupSum(bh: Blackhole): Unit = |
| 96 | + val (uniqueGroups, sums) = groupSum(groups, values) |
| 97 | + bh.consume(uniqueGroups) |
| 98 | + bh.consume(sums) |
| 99 | + end benchGroupSum |
| 100 | + |
| 101 | +end GroupOpsBenchmark |
0 commit comments