wu-sheng commented on a change in pull request #3068: improve ContextManager.stopSpan performance: call ThreadLocal only once URL: https://github.com/apache/skywalking/pull/3068#discussion_r303226513
########## File path: apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/context/ThreadLocalGetXTimeBenchmark.java ########## @@ -0,0 +1,140 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + + +package org.apache.skywalking.apm.agent.core.context; + +import java.util.concurrent.TimeUnit; + +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Fork; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.Warmup; +import org.openjdk.jmh.runner.Runner; +import org.openjdk.jmh.runner.RunnerException; +import org.openjdk.jmh.runner.options.Options; +import org.openjdk.jmh.runner.options.OptionsBuilder; +import org.openjdk.jmh.runner.options.TimeValue; + +@Fork(5) +@Warmup(iterations = 5,time = 1,timeUnit = TimeUnit.SECONDS) +@Measurement(iterations = 10,time = 1,timeUnit = TimeUnit.SECONDS) +@BenchmarkMode(Mode.Throughput) +@OutputTimeUnit(TimeUnit.MILLISECONDS) +@State(Scope.Benchmark) +public class ThreadLocalGetXTimeBenchmark { + + private static class Bean { + private String name = "ABC"; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String stop(Bean bean) { + return bean.getName(); + } + } + private static final ThreadLocal<Bean> CONTEXT = new ThreadLocal<Bean>() { + + @Override + protected Bean initialValue() { + return new Bean(); + } + + }; + + protected String stop(Bean bean) { + return bean.stop(bean); + } + + + @Benchmark + public String getThreadLocalTwice() { + return get().stop(get()); + } + + @Benchmark + public String getThreadLocalFourTime() { + get(); + get(); + get(); + get(); + return get().stop(get()); + } + + @Benchmark + public String getThreadLocalOnce1() { + final Bean bean = get(); + return bean.stop(bean); + } + +//@Benchmark Review comment: What is this comment for? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
