1
+ classdef traceTest < matlab .perftest .TestCase
2
+ % performance tests for tracing
3
+
4
+ properties
5
+ TestData
6
+ end
7
+
8
+ methods (TestClassSetup )
9
+ function setupOnce(testCase )
10
+ testdir = fileparts(mfilename(" fullpath" ));
11
+ addpath(fullfile(testdir , " .." )); % add directory where common setup and teardown code lives
12
+ commonSetupOnce(testCase );
13
+ end
14
+ end
15
+
16
+ methods (TestMethodSetup )
17
+ function setup(testCase )
18
+ commonSetup(testCase );
19
+
20
+ % create a global tracer provider
21
+ import opentelemetry .sdk .trace .*
22
+ tp = TracerProvider(BatchSpanProcessor());
23
+ setTracerProvider(tp );
24
+ end
25
+ end
26
+
27
+ methods (TestMethodTeardown )
28
+ function teardown(testCase )
29
+ commonTeardown(testCase );
30
+ end
31
+ end
32
+
33
+ methods (Test )
34
+ function testSpan(testCase )
35
+ % start and end a span
36
+ tr = opentelemetry .trace .getTracer(" Tracer" );
37
+
38
+ testCase .startMeasuring();
39
+ sp = startSpan(tr , " Span" );
40
+ endSpan(sp );
41
+ testCase .stopMeasuring();
42
+ end
43
+
44
+ function testCurrentSpan(testCase )
45
+ % start a span, put it in current context, end the span
46
+ tr = opentelemetry .trace .getTracer(" Tracer" );
47
+
48
+ testCase .startMeasuring();
49
+ sp = startSpan(tr , " Span" );
50
+ scope = makeCurrent(sp ); % #ok<NASGU>
51
+ endSpan(sp );
52
+ testCase .stopMeasuring();
53
+ end
54
+
55
+ function testNestedSpansImplicitContext(testCase )
56
+ % nested spans, using current span as parent
57
+ tr = opentelemetry .trace .getTracer(" Tracer" );
58
+
59
+ testCase .startMeasuring();
60
+ osp = startSpan(tr , " outer" );
61
+ oscope = makeCurrent(osp ); % #ok<NASGU>
62
+
63
+ isp = startSpan(tr , " inner" );
64
+ iscope = makeCurrent(isp ); % #ok<NASGU>
65
+
66
+ imsp = startSpan(tr , " innermost" );
67
+
68
+ endSpan(imsp );
69
+ endSpan(isp );
70
+ endSpan(osp );
71
+ testCase .stopMeasuring();
72
+ end
73
+
74
+ function testNestedSpansExplicitContext(testCase )
75
+ % nested spans, explicitly setting parents
76
+ tr = opentelemetry .trace .getTracer(" Tracer" );
77
+
78
+ testCase .startMeasuring();
79
+ osp = startSpan(tr , " outer" );
80
+ context = insertSpan(osp );
81
+
82
+ isp = startSpan(tr , " inner" , Context= context );
83
+ context = insertSpan(isp , context );
84
+
85
+ imsp = startSpan(tr , " innermost" , Context= context );
86
+
87
+ endSpan(imsp );
88
+ endSpan(isp );
89
+ endSpan(osp );
90
+ testCase .stopMeasuring();
91
+ end
92
+
93
+ function testAttributes(testCase )
94
+ % span with 3 attributes
95
+ tr = opentelemetry .trace .getTracer(" Tracer" );
96
+ m = magic(4 );
97
+
98
+ testCase .startMeasuring();
99
+ sp = startSpan(tr , " Span" );
100
+ setAttributes(sp , " attribute 1" , " value 1" , " attribute 2" , 10 , ...
101
+ " attribute 3" , m );
102
+ endSpan(sp );
103
+ testCase .stopMeasuring()
104
+ end
105
+
106
+ function testEvents(testCase )
107
+ % span with 3 events
108
+ tr = opentelemetry .trace .getTracer(" Tracer" );
109
+
110
+ testCase .startMeasuring();
111
+ sp = startSpan(tr , " Span" );
112
+ addEvent(sp , " event 1" )
113
+ addEvent(sp , " event 2" );
114
+ addEvent(sp , " event 3" );
115
+ endSpan(sp );
116
+ testCase .stopMeasuring()
117
+ end
118
+
119
+ function testLinks(testCase )
120
+ % span with 2 links
121
+ tr = opentelemetry .trace .getTracer(" Tracer" );
122
+ sp1 = startSpan(tr , " Span 1" );
123
+ sp1ctxt = getSpanContext(sp1 );
124
+ sp2 = startSpan(tr , " Span 2" );
125
+ sp2ctxt = getSpanContext(sp2 );
126
+
127
+ testCase .startMeasuring();
128
+ link1 = opentelemetry .trace .Link(sp1ctxt );
129
+ link2 = opentelemetry .trace .Link(sp2ctxt );
130
+ sp3 = startSpan(tr , " Span 3" , Links= [link1 link2 ]);
131
+ endSpan(sp3 );
132
+ testCase .stopMeasuring()
133
+ end
134
+
135
+ function testGetTracer(testCase )
136
+ % get a tracer from the global tracer provider instance
137
+ testCase .startMeasuring();
138
+ tr = opentelemetry .trace .getTracer(" Tracer" ); % #ok<NASGU>
139
+ testCase .stopMeasuring();
140
+ end
141
+
142
+ function testCreateDefaultTracerProvider(testCase )
143
+ % create default TracerProvider in the sdk
144
+ testCase .startMeasuring();
145
+ tp = opentelemetry .sdk .trace .TracerProvider(); % #ok<NASGU>
146
+ testCase .stopMeasuring();
147
+ end
148
+
149
+ function testSpanContext(testCase )
150
+ % retrieve trace ID and span ID in the span context
151
+ tr = opentelemetry .trace .getTracer(" Tracer" );
152
+ sp = startSpan(tr , " Span" );
153
+
154
+ testCase .startMeasuring();
155
+ spctxt = getSpanContext(sp );
156
+ traceid = spctxt .TraceId ; % #ok<NASGU>
157
+ spanid = spctxt .SpanId ; % #ok<NASGU>
158
+ testCase .stopMeasuring();
159
+ end
160
+
161
+ end
162
+ end
0 commit comments