|
| 1 | +// |
| 2 | +// ======================================================================== |
| 3 | +// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others. |
| 4 | +// |
| 5 | +// This program and the accompanying materials are made available under the |
| 6 | +// terms of the Eclipse Public License v. 2.0 which is available at |
| 7 | +// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 |
| 8 | +// which is available at https://www.apache.org/licenses/LICENSE-2.0. |
| 9 | +// |
| 10 | +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 |
| 11 | +// ======================================================================== |
| 12 | +// |
| 13 | + |
| 14 | +package org.eclipse.jetty.util.thread.jmh; |
| 15 | + |
| 16 | +import java.io.Closeable; |
| 17 | +import java.util.concurrent.ArrayBlockingQueue; |
| 18 | +import java.util.concurrent.BlockingQueue; |
| 19 | +import java.util.concurrent.TimeUnit; |
| 20 | +import org.eclipse.jetty.util.BlockingArrayQueue; |
| 21 | +import org.eclipse.jetty.util.component.LifeCycle; |
| 22 | +import org.eclipse.jetty.util.thread.QueuedThreadPool; |
| 23 | +import org.eclipse.jetty.util.thread.ScheduledExecutorScheduler; |
| 24 | +import org.eclipse.jetty.util.thread.Scheduler; |
| 25 | +import org.openjdk.jmh.annotations.Benchmark; |
| 26 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 27 | +import org.openjdk.jmh.annotations.Level; |
| 28 | +import org.openjdk.jmh.annotations.Measurement; |
| 29 | +import org.openjdk.jmh.annotations.Mode; |
| 30 | +import org.openjdk.jmh.annotations.Param; |
| 31 | +import org.openjdk.jmh.annotations.Scope; |
| 32 | +import org.openjdk.jmh.annotations.Setup; |
| 33 | +import org.openjdk.jmh.annotations.State; |
| 34 | +import org.openjdk.jmh.annotations.TearDown; |
| 35 | +import org.openjdk.jmh.annotations.Threads; |
| 36 | +import org.openjdk.jmh.annotations.Warmup; |
| 37 | +import org.openjdk.jmh.runner.Runner; |
| 38 | +import org.openjdk.jmh.runner.RunnerException; |
| 39 | +import org.openjdk.jmh.runner.options.Options; |
| 40 | +import org.openjdk.jmh.runner.options.OptionsBuilder; |
| 41 | + |
| 42 | +@State(Scope.Benchmark) |
| 43 | +@Warmup(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS) |
| 44 | +@Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS) |
| 45 | +public class QueuedThreadPoolScheduledExecutorSchedulerBenchmark |
| 46 | +{ |
| 47 | + @Param({ |
| 48 | + "BAQ", |
| 49 | + "ABQ", |
| 50 | + }) |
| 51 | + public static String QUEUE_TYPE; |
| 52 | + |
| 53 | + QueuedThreadPool pool; |
| 54 | + ScheduledExecutorScheduler scheduler; |
| 55 | + |
| 56 | + @Setup(Level.Iteration) |
| 57 | + public void buildPool() |
| 58 | + { |
| 59 | + BlockingQueue<Runnable> q = switch (QUEUE_TYPE) |
| 60 | + { |
| 61 | + case "BAQ" -> new BlockingArrayQueue<>(1024 * 1204, 4 * 1024); |
| 62 | + case "ABQ" -> new ArrayBlockingQueue<>(16 * 1024 * 1204); |
| 63 | + default -> throw new IllegalArgumentException(); |
| 64 | + }; |
| 65 | + pool = new QueuedThreadPool(200, 200, q); |
| 66 | + pool.setStopTimeout(30000); |
| 67 | + pool.setReservedThreads(0); |
| 68 | + LifeCycle.start(pool); |
| 69 | + |
| 70 | + scheduler = new ScheduledExecutorScheduler(); |
| 71 | + LifeCycle.start(scheduler); |
| 72 | + } |
| 73 | + |
| 74 | + @TearDown(Level.Iteration) |
| 75 | + public void shutdownPool() |
| 76 | + { |
| 77 | + LifeCycle.stop(pool); |
| 78 | + LifeCycle.stop(scheduler); |
| 79 | + } |
| 80 | + |
| 81 | + @Benchmark |
| 82 | + @BenchmarkMode(Mode.Throughput) |
| 83 | + @Threads(8) |
| 84 | + public void test() |
| 85 | + { |
| 86 | + pool.execute((CloseableRunnable)() -> |
| 87 | + { |
| 88 | + Scheduler.Task task = scheduler.schedule(() -> {}, 1, TimeUnit.SECONDS); |
| 89 | + task.cancel(); |
| 90 | + }); |
| 91 | + } |
| 92 | + |
| 93 | + public static void main(String[] args) throws RunnerException |
| 94 | + { |
| 95 | + // String asyncProfilerPath = "/home/lorban/work/tools/async-profiler/4.0/lib/libasyncProfiler.so"; |
| 96 | + Options opt = new OptionsBuilder() |
| 97 | + .include(QueuedThreadPoolScheduledExecutorSchedulerBenchmark.class.getSimpleName()) |
| 98 | + .forks(1) |
| 99 | + // .addProfiler(CompilerProfiler.class) |
| 100 | + // .addProfiler(LinuxPerfProfiler.class) |
| 101 | + // .addProfiler(LinuxPerfNormProfiler.class) |
| 102 | + // .addProfiler(LinuxPerfAsmProfiler.class, "hotThreshold=0.05") |
| 103 | + // .addProfiler(AsyncProfiler.class, "dir=/tmp/QTP;output=jfr;event=cpu;libPath=" + asyncProfilerPath) |
| 104 | + .build(); |
| 105 | + |
| 106 | + new Runner(opt).run(); |
| 107 | + } |
| 108 | + |
| 109 | + private interface CloseableRunnable extends Closeable, Runnable |
| 110 | + { |
| 111 | + @Override |
| 112 | + default void close() |
| 113 | + { |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments