|
| 1 | +package io.github.amayaframework.di; |
| 2 | + |
| 3 | +import io.github.amayaframework.di.core.*; |
| 4 | +import io.github.amayaframework.di.internal.PlainScopedServiceProvider; |
| 5 | +import io.github.amayaframework.di.internal.PlainServiceProvider; |
| 6 | +import org.openjdk.jmh.annotations.*; |
| 7 | + |
| 8 | +import java.util.concurrent.TimeUnit; |
| 9 | + |
| 10 | +@BenchmarkMode(Mode.AverageTime) |
| 11 | +@OutputTimeUnit(TimeUnit.NANOSECONDS) |
| 12 | +@Warmup |
| 13 | +public class CloseableBenchmark { |
| 14 | + private static final ServiceProvider PROVIDER = prepareProvider(); |
| 15 | + private static final ScopedServiceProvider SCOPED_PROVIDER = prepareScopedProvider(); |
| 16 | + |
| 17 | + private static ServiceProvider prepareProvider() { |
| 18 | + var repo = new HashTypeRepository(); |
| 19 | + repo.putCloseable(new A()); |
| 20 | + repo.putCloseable(new B()); |
| 21 | + repo.put(new C()); |
| 22 | + return new PlainServiceProvider(repo); |
| 23 | + } |
| 24 | + |
| 25 | + private static ScopedServiceProvider prepareScopedProvider() { |
| 26 | + var repo = new HashTypeRepository(); |
| 27 | + repo.putCloseable(new A()); |
| 28 | + repo.putCloseable(new B()); |
| 29 | + repo.put(new C()); |
| 30 | + return new PlainScopedServiceProvider(new ScopedTypeRepository(repo, new HashTypeRepository())); |
| 31 | + } |
| 32 | + |
| 33 | + @Benchmark |
| 34 | + public void benchClose() { |
| 35 | + PROVIDER.close(); |
| 36 | + } |
| 37 | + |
| 38 | + @Benchmark |
| 39 | + public void benchScopedClose() { |
| 40 | + SCOPED_PROVIDER.close(); |
| 41 | + } |
| 42 | + |
| 43 | + public static final class A implements Closeable { |
| 44 | + |
| 45 | + @Override |
| 46 | + public void close() { |
| 47 | + |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + public static final class B implements Closeable { |
| 52 | + |
| 53 | + @Override |
| 54 | + public void close() { |
| 55 | + |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public static final class C { |
| 60 | + } |
| 61 | +} |
0 commit comments