提交 c7916d9f 编写于 作者: qxo's avatar qxo 提交者: wu-sheng

improve ContextManager.stopSpan performance: call ThreadLocal only once (#3068)

* improve ContextManager.stopSpan and awaitFinishAsync performance: call ThreadLocal only once
For the [JMH benchmark test](https://gitlab.com/qxo1/Common-JMH-benchmark/-/jobs/251025992) :
On gitlab CI(java 8): call ThreadLocal once vs twice fast over 10%
My T480 java 8 : fast over 40%; java 11: fast over 50%;

[The JMH benchmark code ](https://github.com/qxo/Common-JMH-benchmark/blob/master/src/main/java/test/ThreadLocalGetXTimeBenchmark.java)

* Update apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/ContextManager.java
Co-Authored-By: Nkezhenxu94 <kezhenxu94@163.com>

* Update apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/ContextManager.java
Co-Authored-By: Nkezhenxu94 <kezhenxu94@163.com>

* Update apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/ContextManager.java
Co-Authored-By: wu-sheng's avatar吴晟 Wu Sheng <wu.sheng@foxmail.com>
上级 d4c70065
......@@ -150,11 +150,12 @@ public class ContextManager implements BootService {
}
public static AbstractTracerContext awaitFinishAsync(AbstractSpan span) {
AbstractSpan activeSpan = activeSpan();
final AbstractTracerContext context = get();
AbstractSpan activeSpan = context.activeSpan();
if (span != activeSpan) {
throw new RuntimeException("Span is not the active in current context.");
}
return get().awaitFinishAsync();
return context.awaitFinishAsync();
}
/**
......@@ -165,12 +166,22 @@ public class ContextManager implements BootService {
return get().activeSpan();
}
/**
* Recommend use ContextManager::stopSpan(AbstractSpan span), because in that way,
* the TracingContext core could verify this span is the active one, in order to avoid stop unexpected span.
* If the current span is hard to get or only could get by low-performance way, this stop way is still acceptable.
*/
public static void stopSpan() {
stopSpan(activeSpan());
final AbstractTracerContext context = get();
stopSpan(context.activeSpan(),context);
}
public static void stopSpan(AbstractSpan span) {
if (get().stopSpan(span)) {
stopSpan(span, get());
}
private static void stopSpan(AbstractSpan span, final AbstractTracerContext context) {
if (context.stopSpan(span)) {
CONTEXT.remove();
RUNTIME_CONTEXT.remove();
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册