View Javadoc
1   /*
2    * Copyright 2025 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.util;
17  
18  import io.netty.microbench.util.AbstractMicrobenchmark;
19  import jdk.jfr.Event;
20  import jdk.jfr.consumer.RecordingStream;
21  import org.openjdk.jmh.annotations.Benchmark;
22  import org.openjdk.jmh.annotations.BenchmarkMode;
23  import org.openjdk.jmh.annotations.Fork;
24  import org.openjdk.jmh.annotations.Measurement;
25  import org.openjdk.jmh.annotations.Mode;
26  import org.openjdk.jmh.annotations.OutputTimeUnit;
27  import org.openjdk.jmh.annotations.Param;
28  import org.openjdk.jmh.annotations.Setup;
29  import org.openjdk.jmh.annotations.TearDown;
30  import org.openjdk.jmh.profile.GCProfiler;
31  import org.openjdk.jmh.runner.options.ChainedOptionsBuilder;
32  
33  import java.util.concurrent.TimeUnit;
34  
35  @SuppressWarnings("Since15")
36  @Fork(1)
37  @BenchmarkMode(Mode.AverageTime)
38  @OutputTimeUnit(TimeUnit.NANOSECONDS)
39  @Measurement(time = 10, timeUnit = TimeUnit.SECONDS)
40  public class JfrBenchmark extends AbstractMicrobenchmark {
41      @Param({"true", "false"})
42      boolean enabled;
43  
44      private RecordingStream stream;
45  
46      @Override
47      protected ChainedOptionsBuilder newOptionsBuilder() throws Exception {
48          return super.newOptionsBuilder()
49                  .addProfiler(GCProfiler.class);
50      }
51  
52      @Setup
53      public void setup() {
54          stream = new RecordingStream();
55          if (enabled) {
56              stream.enable(NettyEvent.class.getName());
57          } else {
58              stream.disable(NettyEvent.class.getName());
59          }
60          stream.startAsync();
61      }
62  
63      @TearDown
64      public void teardown() {
65          stream.close();
66      }
67  
68      @Benchmark
69      public void nettyEvent() {
70          NettyEvent event = new NettyEvent();
71          event.begin();
72          event.end();
73          event.foo = "bar";
74          event.commit();
75      }
76  
77      static final class NettyEvent extends Event {
78          String foo;
79      }
80  }