1 /* 2 * Copyright 2018, OpenCensus 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.opencensus.implcore.trace.propagation; 18 19 import io.opencensus.trace.SpanContext; 20 import io.opencensus.trace.propagation.SpanContextParseException; 21 import io.opencensus.trace.propagation.TextFormat; 22 import io.opencensus.trace.propagation.TextFormat.Getter; 23 import io.opencensus.trace.propagation.TextFormat.Setter; 24 import java.util.Map; 25 import javax.annotation.Nullable; 26 27 /** Generic benchmarks for {@link io.opencensus.trace.propagation.TextFormat}. */ 28 final class TextFormatBenchmarkBase { 29 private static final Setter<Map<String, String>> setter = 30 new Setter<Map<String, String>>() { 31 @Override 32 public void put(Map<String, String> carrier, String key, String value) { 33 carrier.put(key, value); 34 } 35 }; 36 37 private static final Getter<Map<String, String>> getter = 38 new Getter<Map<String, String>>() { 39 @Nullable 40 @Override 41 public String get(Map<String, String> carrier, String key) { 42 return carrier.get(key); 43 } 44 }; 45 46 private final TextFormat textFormat; 47 48 TextFormatBenchmarkBase(TextFormat textFormat) { 49 this.textFormat = textFormat; 50 } 51 52 void inject(SpanContext spanContext, Map<String, String> carrier) { 53 textFormat.inject(spanContext, carrier, setter); 54 } 55 56 SpanContext extract(Map<String, String> carrier) throws SpanContextParseException { 57 return textFormat.extract(carrier, getter); 58 } 59 } 60