xref: /aosp_15_r20/external/grpc-grpc-java/api/src/jmh/java/io/grpc/StatusBenchmark.java (revision e07d83d3ffcef9ecfc9f7f475418ec639ff0e5fe)
1 /*
2  * Copyright 2016 The gRPC Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://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,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package io.grpc;
18 
19 import java.nio.charset.Charset;
20 import java.util.concurrent.TimeUnit;
21 import org.openjdk.jmh.annotations.Benchmark;
22 import org.openjdk.jmh.annotations.BenchmarkMode;
23 import org.openjdk.jmh.annotations.Mode;
24 import org.openjdk.jmh.annotations.OutputTimeUnit;
25 import org.openjdk.jmh.annotations.Scope;
26 import org.openjdk.jmh.annotations.State;
27 
28 /** StatusBenchmark. */
29 @State(Scope.Benchmark)
30 public class StatusBenchmark {
31 
32   /**
33    * Javadoc comment.
34    */
35   @Benchmark
36   @BenchmarkMode(Mode.SampleTime)
37   @OutputTimeUnit(TimeUnit.NANOSECONDS)
messageEncodePlain()38   public byte[] messageEncodePlain() {
39     return Status.MESSAGE_KEY.toBytes("Unexpected RST in stream");
40   }
41 
42   /**
43    * Javadoc comment.
44    */
45   @Benchmark
46   @BenchmarkMode(Mode.SampleTime)
47   @OutputTimeUnit(TimeUnit.NANOSECONDS)
messageEncodeEscape()48   public byte[] messageEncodeEscape() {
49     return Status.MESSAGE_KEY.toBytes("Some Error\nWasabi and Horseradish are the same");
50   }
51 
52   /**
53    * Javadoc comment.
54    */
55   @Benchmark
56   @BenchmarkMode(Mode.SampleTime)
57   @OutputTimeUnit(TimeUnit.NANOSECONDS)
messageDecodePlain()58   public String messageDecodePlain() {
59     return Status.MESSAGE_KEY.parseBytes(
60         "Unexpected RST in stream".getBytes(Charset.forName("US-ASCII")));
61   }
62 
63   /**
64    * Javadoc comment.
65    */
66   @Benchmark
67   @BenchmarkMode(Mode.SampleTime)
68   @OutputTimeUnit(TimeUnit.NANOSECONDS)
messageDecodeEscape()69   public String messageDecodeEscape() {
70     return Status.MESSAGE_KEY.parseBytes(
71         "Some Error%10Wasabi and Horseradish are the same".getBytes(Charset.forName("US-ASCII")));
72   }
73 
74   /**
75    * Javadoc comment.
76    */
77   @Benchmark
78   @BenchmarkMode(Mode.SampleTime)
79   @OutputTimeUnit(TimeUnit.NANOSECONDS)
codeEncode()80   public byte[] codeEncode() {
81     return Status.CODE_KEY.toBytes(Status.DATA_LOSS);
82   }
83 
84   /**
85    * Javadoc comment.
86    */
87   @Benchmark
88   @BenchmarkMode(Mode.SampleTime)
89   @OutputTimeUnit(TimeUnit.NANOSECONDS)
codeDecode()90   public Status codeDecode() {
91     return Status.CODE_KEY.parseBytes("15".getBytes(Charset.forName("US-ASCII")));
92   }
93 }
94 
95