Skip to content

Improve performance of ContextManagerExtendService.createTraceContext() #12355

@jiangyunpeng

Description

@jiangyunpeng

Search before asking

  • I had searched in the issues and found no similar issues.

Apache SkyWalking Component

Java Agent (apache/skywalking-java)

What happened

When we conducted a benchmark test to kafka producer , we found some performance isseue at skywalking agent.

here is out benchmark report:
with skywalking agent: qps: 22892

-- ------------------- ----------- ------ ---------- ------- ----- ------------- ---------- --------- ---------- --------- ----- 
id time                task        thread connection request error duration(sec) rtMean(ms) rtP99(ms) rtP999(ms) rtMax(ms) qps   
-- ------------------- ----------- ------ ---------- ------- ----- ------------- ---------- --------- ---------- --------- ----- 
54 2024-06-21 00:54:07 kafkaSingle 200    2        457980  0     20.006        7.818      17.868    218.561    218.561   22892  
55 2024-06-21 11:25:37 kafkaSingle 200    2          436157  0     20.005        8.1        29.739    216.576    218.306   21802 

without skywalking: qps: 85614

-- ------------------- ----------- ------ ---------- ------- ----- ------------- ---------- --------- ---------- --------- ----- 
id time                task        thread connection request error duration(sec) rtMean(ms) rtP99(ms) rtP999(ms) rtMax(ms) qps   
-- ------------------- ----------- ------ ---------- ------- ----- ------------- ---------- --------- ---------- --------- ----- 
1  2024-06-21 11:22:52 kafkaSingle 200    2          1712112 0     19.998        2.195      4.009     10.222     21.237    85614 

By through CPU profile, We found the performance bottleneck maye be at ContextManagerExtendService.createTraceContext():
profile-1
profile-2

This method is a hot method, and the Arrays.stream().anyMatch() does not have a good performance.

Here is a jmh benchmark report between Arrays.stream().anyMatch() and HashSet.get()

# JMH version: 1.32
# VM version: JDK 1.8.0_291, Java HotSpot(TM) 64-Bit Server VM, 25.291-b10
# VM invoker: /data/program/java/jre/bin/java
Benchmark                                                    Mode       Cnt          Score     Error   Units
IgnoreSuffixBenchmark.testByArray                           thrpt         3          0.006 ±   0.011  ops/ns
IgnoreSuffixBenchmark.testByHashSet                         thrpt         3          0.065 ±   0.143  ops/ns
IgnoreSuffixBenchmark.testByArray                          sample  20517960      14498.921 ± 824.389   ns/op
IgnoreSuffixBenchmark.testByArray:testByArray·p0.00        sample                  347.000             ns/op
IgnoreSuffixBenchmark.testByArray:testByArray·p0.50        sample                  674.000             ns/op
IgnoreSuffixBenchmark.testByArray:testByArray·p0.90        sample                  820.000             ns/op
IgnoreSuffixBenchmark.testByArray:testByArray·p0.95        sample                 1160.000             ns/op
IgnoreSuffixBenchmark.testByArray:testByArray·p0.99        sample                 3192.000             ns/op
IgnoreSuffixBenchmark.testByArray:testByArray·p0.999       sample                21728.000             ns/op
IgnoreSuffixBenchmark.testByArray:testByArray·p0.9999      sample             77201408.000             ns/op
IgnoreSuffixBenchmark.testByArray:testByArray·p1.00        sample            366477312.000             ns/op
IgnoreSuffixBenchmark.testByHashSet                        sample  24946521       3453.060 ± 389.775   ns/op
IgnoreSuffixBenchmark.testByHashSet:testByHashSet·p0.00    sample                   56.000             ns/op
IgnoreSuffixBenchmark.testByHashSet:testByHashSet·p0.50    sample                  106.000             ns/op
IgnoreSuffixBenchmark.testByHashSet:testByHashSet·p0.90    sample                  123.000             ns/op
IgnoreSuffixBenchmark.testByHashSet:testByHashSet·p0.95    sample                  126.000             ns/op
IgnoreSuffixBenchmark.testByHashSet:testByHashSet·p0.99    sample                  174.000             ns/op
IgnoreSuffixBenchmark.testByHashSet:testByHashSet·p0.999   sample                 5528.000             ns/op
IgnoreSuffixBenchmark.testByHashSet:testByHashSet·p0.9999  sample                80256.000             ns/op
IgnoreSuffixBenchmark.testByHashSet:testByHashSet·p1.00    sample            510656512.000             ns/op

The Arrays.stream().anyMatch() is slower 4 times than HashSet.get()

we updated ContextManagerExtendService use HashSet, the kafkaSingle qps:

-- ------------------- ----------- ------ ---------- ------- ----- ------------- ---------- --------- ---------- --------- -----
id time                task        thread connection request error duration(sec) rtMean(ms) rtP99(ms) rtP999(ms) rtMax(ms) qps
-- ------------------- ----------- ------ ---------- ------- ----- ------------- ---------- --------- ---------- --------- -----
1  2024-06-21 12:22:12 kafkaSingle 200    2          1702189 0     20.0          2.175      3.895     8.835      22.07     85109

Anything else

No response

Are you willing to submit a pull request to fix on your own?

  • Yes I am willing to submit a pull request on my own!

Code of Conduct

Metadata

Metadata

Labels

agentLanguage agent related.enhancementEnhancement on performance or codesjavaJava agent related

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions