|
| 1 | +/* |
| 2 | + * Copyright (c) 2009-2026 jMonkeyEngine |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are |
| 7 | + * met: |
| 8 | + * |
| 9 | + * * Redistributions of source code must retain the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer. |
| 11 | + * |
| 12 | + * * Redistributions in binary form must reproduce the above copyright |
| 13 | + * notice, this list of conditions and the following disclaimer in the |
| 14 | + * documentation and/or other materials provided with the distribution. |
| 15 | + * |
| 16 | + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors |
| 17 | + * may be used to endorse or promote products derived from this software |
| 18 | + * without specific prior written permission. |
| 19 | + * |
| 20 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 22 | + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 23 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 24 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 25 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 26 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 27 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 28 | + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 29 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 30 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | + */ |
| 32 | +package com.jme3.light; |
| 33 | + |
| 34 | +import com.jme3.math.Vector3f; |
| 35 | +import com.jme3.scene.Geometry; |
| 36 | +import com.jme3.scene.Mesh; |
| 37 | +import java.util.Random; |
| 38 | +import java.util.concurrent.TimeUnit; |
| 39 | +import org.openjdk.jmh.annotations.Benchmark; |
| 40 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 41 | +import org.openjdk.jmh.annotations.Fork; |
| 42 | +import org.openjdk.jmh.annotations.Level; |
| 43 | +import org.openjdk.jmh.annotations.Measurement; |
| 44 | +import org.openjdk.jmh.annotations.Mode; |
| 45 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 46 | +import org.openjdk.jmh.annotations.Param; |
| 47 | +import org.openjdk.jmh.annotations.Scope; |
| 48 | +import org.openjdk.jmh.annotations.Setup; |
| 49 | +import org.openjdk.jmh.annotations.State; |
| 50 | +import org.openjdk.jmh.annotations.Warmup; |
| 51 | +import org.openjdk.jmh.infra.Blackhole; |
| 52 | + |
| 53 | +@BenchmarkMode(Mode.AverageTime) |
| 54 | +@OutputTimeUnit(TimeUnit.MICROSECONDS) |
| 55 | +@Warmup(iterations = 5, time = 200, timeUnit = TimeUnit.MILLISECONDS) |
| 56 | +@Measurement(iterations = 5, time = 200, timeUnit = TimeUnit.MILLISECONDS) |
| 57 | +@Fork(2) |
| 58 | +@State(Scope.Thread) |
| 59 | +public class LightListSortBenchmark { |
| 60 | + |
| 61 | + @Param({"8", "64", "256"}) |
| 62 | + public int lightCount; |
| 63 | + |
| 64 | + @Param({"1", "8", "1024"}) |
| 65 | + public int retainedCapacityMultiplier; |
| 66 | + |
| 67 | + private Geometry owner; |
| 68 | + private Light[] lights; |
| 69 | + private LightList list; |
| 70 | + private int invocation; |
| 71 | + |
| 72 | + @Setup(Level.Trial) |
| 73 | + public void setupTrial() { |
| 74 | + owner = new Geometry("owner", new Mesh()); |
| 75 | + owner.setLocalTranslation(3f, -7f, 11f); |
| 76 | + owner.updateGeometricState(); |
| 77 | + |
| 78 | + lights = new Light[lightCount]; |
| 79 | + Random random = new Random(0x51A7E5L + lightCount); |
| 80 | + for (int i = 0; i < lightCount; i++) { |
| 81 | + switch (i & 3) { |
| 82 | + case 0: |
| 83 | + lights[i] = new AmbientLight(); |
| 84 | + break; |
| 85 | + case 1: |
| 86 | + lights[i] = new DirectionalLight(new Vector3f(1f, -1f, 0.25f).normalizeLocal()); |
| 87 | + break; |
| 88 | + default: |
| 89 | + lights[i] = new PointLight(new Vector3f( |
| 90 | + random.nextFloat() * 200f - 100f, |
| 91 | + random.nextFloat() * 200f - 100f, |
| 92 | + random.nextFloat() * 200f - 100f)); |
| 93 | + break; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + list = new LightList(owner); |
| 98 | + int retainedCapacity = Math.max(lightCount, lightCount * retainedCapacityMultiplier); |
| 99 | + for (int i = 0; i < retainedCapacity; i++) { |
| 100 | + list.add(lights[i % lightCount]); |
| 101 | + } |
| 102 | + list.clear(); |
| 103 | + } |
| 104 | + |
| 105 | + @Setup(Level.Invocation) |
| 106 | + public void setupInvocation() { |
| 107 | + list.clear(); |
| 108 | + int offset = (invocation++ & Integer.MAX_VALUE) % lightCount; |
| 109 | + for (int i = 0; i < lightCount; i++) { |
| 110 | + list.add(lights[(i + offset) % lightCount]); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + @Benchmark |
| 115 | + public void sortTransformChanged(Blackhole blackhole) { |
| 116 | + list.sort(true); |
| 117 | + blackhole.consume(list.get(lightCount - 1)); |
| 118 | + } |
| 119 | +} |
0 commit comments