|
| 1 | +package app.photofox.vipsffm; |
| 2 | + |
| 3 | +import app.photofox.vipsffm.enums.VipsInteresting; |
| 4 | +import org.junit.jupiter.api.Test; |
| 5 | +import org.openjdk.jmh.annotations.*; |
| 6 | +import org.openjdk.jmh.runner.Runner; |
| 7 | +import org.openjdk.jmh.runner.options.OptionsBuilder; |
| 8 | +import org.openjdk.jmh.runner.options.TimeValue; |
| 9 | + |
| 10 | +import java.lang.foreign.Arena; |
| 11 | +import java.util.concurrent.TimeUnit; |
| 12 | + |
| 13 | +public class BenchmarkTests { |
| 14 | + |
| 15 | + @Test |
| 16 | + void runBenchmarks() throws Exception { |
| 17 | + var options = new OptionsBuilder() |
| 18 | + .include(this.getClass().getName() + ".*") |
| 19 | + .mode(Mode.AverageTime) |
| 20 | + .timeUnit(TimeUnit.MILLISECONDS) |
| 21 | + .warmupIterations(3) |
| 22 | + .measurementTime(TimeValue.seconds(1)) |
| 23 | + .measurementIterations(10) |
| 24 | + .threads(1) |
| 25 | + .forks(1) |
| 26 | + .shouldFailOnError(true) |
| 27 | + .shouldDoGC(true) |
| 28 | + .build(); |
| 29 | + |
| 30 | + new Runner(options).run(); |
| 31 | + } |
| 32 | + |
| 33 | + @State(Scope.Thread) |
| 34 | + public static class BenchmarkState { |
| 35 | + |
| 36 | + VSource sourceImage; |
| 37 | + Arena arena = Arena.ofConfined(); |
| 38 | + |
| 39 | + @Setup(Level.Trial) |
| 40 | + public void init() throws VipsError { |
| 41 | + Vips.init(false, true); |
| 42 | + VipsHelper.cache_set_max(0); |
| 43 | + |
| 44 | + sourceImage = VSource.newFromFile( |
| 45 | + arena, |
| 46 | + "../sample/src/main/resources/sample_images/fox.jpg" |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + @TearDown |
| 51 | + public void tearDown() { |
| 52 | + arena.close(); |
| 53 | + Vips.shutdown(); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + @Benchmark |
| 58 | + public void thumbnailOnly(BenchmarkState state) { |
| 59 | + Vips.run(arena -> { |
| 60 | + var target = VTarget.newToMemory(arena); |
| 61 | + var image = VImage.newFromSource(arena, state.sourceImage, ""); |
| 62 | + image |
| 63 | + .thumbnailImage(400) |
| 64 | + .writeToTarget(target, ".jpg"); |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + @Benchmark |
| 69 | + public void jvipsStyle(BenchmarkState state) { |
| 70 | + Vips.run(arena -> { |
| 71 | + var target = VTarget.newToMemory(arena); |
| 72 | + var image = VImage.newFromSource(arena, state.sourceImage, ""); |
| 73 | + var insertSize = 512; |
| 74 | + var paddingSize = 100; |
| 75 | + var finalImageSize = insertSize + paddingSize; |
| 76 | + var backgroundImage = VImage.newImage(arena) |
| 77 | + .black(finalImageSize, finalImageSize); |
| 78 | + var insertImage = image |
| 79 | + .thumbnailImage( |
| 80 | + insertSize, |
| 81 | + VipsOption.Int("height", insertSize), |
| 82 | + VipsOption.Enum("crop", VipsInteresting.INTERESTING_CENTRE) |
| 83 | + ); |
| 84 | + backgroundImage |
| 85 | + .insert(insertImage, paddingSize / 2, paddingSize / 2) |
| 86 | + .writeToTarget(target, ".jpg"); |
| 87 | + }); |
| 88 | + } |
| 89 | +} |
0 commit comments