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: kezhenxu94 <kezhenxu94@163.com> * Update apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/ContextManager.java Co-Authored-By: kezhenxu94 <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 <wu.sheng@foxmail.com>
This commit is contained in:
parent
d4c7006525
commit
c7916d9f27
|
|
@ -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();
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue