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 io.grpc.internal.GrpcUtil; 20 import io.grpc.internal.TransportFrameUtil; 21 import java.io.IOException; 22 import java.io.InputStream; 23 import java.util.UUID; 24 import java.util.concurrent.TimeUnit; 25 import org.openjdk.jmh.annotations.Benchmark; 26 import org.openjdk.jmh.annotations.BenchmarkMode; 27 import org.openjdk.jmh.annotations.Mode; 28 import org.openjdk.jmh.annotations.OutputTimeUnit; 29 import org.openjdk.jmh.annotations.Param; 30 import org.openjdk.jmh.annotations.Scope; 31 import org.openjdk.jmh.annotations.Setup; 32 import org.openjdk.jmh.annotations.State; 33 34 /** 35 * Decomressor Registry encoding benchmark. 36 */ 37 @State(Scope.Benchmark) 38 public class DecompressorRegistryBenchmark { 39 40 @Param({"0", "1", "2"}) 41 public int extraEncodings; 42 43 private DecompressorRegistry reg = DecompressorRegistry.getDefaultInstance(); 44 45 @Setup setUp()46 public void setUp() throws Exception { 47 reg = DecompressorRegistry.getDefaultInstance(); 48 for (int i = 0; i < extraEncodings; i++) { 49 reg = reg.with(new Decompressor() { 50 51 @Override 52 public String getMessageEncoding() { 53 return UUID.randomUUID().toString(); 54 55 } 56 57 @Override 58 public InputStream decompress(InputStream is) throws IOException { 59 return null; 60 61 } 62 }, true); 63 } 64 } 65 66 /** 67 * Javadoc comment. 68 */ 69 @Benchmark 70 @BenchmarkMode(Mode.SampleTime) 71 @OutputTimeUnit(TimeUnit.NANOSECONDS) marshalOld()72 public byte[][] marshalOld() { 73 Metadata m = new Metadata(); 74 m.put( 75 GrpcUtil.MESSAGE_ACCEPT_ENCODING_KEY, 76 InternalDecompressorRegistry.getRawAdvertisedMessageEncodings(reg)); 77 return TransportFrameUtil.toHttp2Headers(m); 78 } 79 } 80 81