1515import _TracingBenchmarkTools
1616import Tracing
1717
18- public let SpanAttributesDSLBenchmarks : [ BenchmarkInfo ] = [
19- BenchmarkInfo (
20- name: " SpanAttributesDSLBenchmarks.000_bench_empty " ,
21- runFunction: { _ in try ! bench_empty ( times: 100 ) } ,
22- tags: [ ] ,
23- setUpFunction: { setUp ( ) } ,
24- tearDownFunction: tearDown
25- ) ,
26- BenchmarkInfo (
27- name: " SpanAttributesDSLBenchmarks.001_bench_makeSpan " ,
28- runFunction: { _ in try ! bench_makeSpan ( times: 100 ) } ,
29- tags: [ ] ,
30- setUpFunction: { setUp ( ) } ,
31- tearDownFunction: tearDown
32- ) ,
33- BenchmarkInfo (
34- name: " SpanAttributesDSLBenchmarks.002_bench_startSpan_end " ,
35- runFunction: { _ in try ! bench_makeSpan ( times: 100 ) } ,
36- tags: [ ] ,
37- setUpFunction: { setUp ( ) } ,
38- tearDownFunction: tearDown
39- ) ,
40-
41- BenchmarkInfo (
42- name: " SpanAttributesDSLBenchmarks.00_bench_set_String_raw " ,
43- runFunction: { _ in try ! bench_set_String_raw ( times: 100 ) } ,
44- tags: [ ] ,
45- setUpFunction: { setUp ( ) } ,
46- tearDownFunction: tearDown
47- ) ,
48- BenchmarkInfo (
49- name: " SpanAttributesDSLBenchmarks.01_bench_set_String_dsl " ,
50- runFunction: { _ in try ! bench_set_String_dsl ( times: 100 ) } ,
51- tags: [ ] ,
52- setUpFunction: { setUp ( ) } ,
53- tearDownFunction: tearDown
54- ) ,
55-
56- BenchmarkInfo (
57- name: " SpanAttributesDSLBenchmarks.02_bench_set_Int_raw " ,
58- runFunction: { _ in try ! bench_set_String_raw ( times: 100 ) } ,
59- tags: [ ] ,
60- setUpFunction: { setUp ( ) } ,
61- tearDownFunction: tearDown
62- ) ,
63- BenchmarkInfo (
64- name: " SpanAttributesDSLBenchmarks.03_bench_set_Int_dsl " ,
65- runFunction: { _ in try ! bench_set_String_dsl ( times: 100 ) } ,
66- tags: [ ] ,
67- setUpFunction: { setUp ( ) } ,
68- tearDownFunction: tearDown
69- ) ,
70- ]
71-
72- private var span : ( any Tracing . Span ) !
73-
74- private func setUp( ) {
75- span = InstrumentationSystem . legacyTracer. startAnySpan ( " something " , context: . topLevel)
76- }
18+ @available ( macOS 10 . 15 , iOS 13 , tvOS 13 , watchOS 6 , * ) // for TaskLocal ServiceContext
19+ enum DSLBenchmarks {
20+ public static let SpanAttributesDSLBenchmarks : [ BenchmarkInfo ] = [
21+ BenchmarkInfo (
22+ name: " SpanAttributesDSLBenchmarks.000_bench_empty " ,
23+ runFunction: { _ in try ! bench_empty ( times: 100 ) } ,
24+ tags: [ ] ,
25+ setUpFunction: { setUp ( ) } ,
26+ tearDownFunction: tearDown
27+ ) ,
28+ BenchmarkInfo (
29+ name: " SpanAttributesDSLBenchmarks.001_bench_makeSpan " ,
30+ runFunction: { _ in try ! bench_makeSpan ( times: 100 ) } ,
31+ tags: [ ] ,
32+ setUpFunction: { setUp ( ) } ,
33+ tearDownFunction: tearDown
34+ ) ,
35+ BenchmarkInfo (
36+ name: " SpanAttributesDSLBenchmarks.002_bench_startSpan_end " ,
37+ runFunction: { _ in try ! bench_makeSpan ( times: 100 ) } ,
38+ tags: [ ] ,
39+ setUpFunction: { setUp ( ) } ,
40+ tearDownFunction: tearDown
41+ ) ,
42+
43+ BenchmarkInfo (
44+ name: " SpanAttributesDSLBenchmarks.00_bench_set_String_raw " ,
45+ runFunction: { _ in try ! bench_set_String_raw ( times: 100 ) } ,
46+ tags: [ ] ,
47+ setUpFunction: { setUp ( ) } ,
48+ tearDownFunction: tearDown
49+ ) ,
50+ BenchmarkInfo (
51+ name: " SpanAttributesDSLBenchmarks.01_bench_set_String_dsl " ,
52+ runFunction: { _ in try ! bench_set_String_dsl ( times: 100 ) } ,
53+ tags: [ ] ,
54+ setUpFunction: { setUp ( ) } ,
55+ tearDownFunction: tearDown
56+ ) ,
57+
58+ BenchmarkInfo (
59+ name: " SpanAttributesDSLBenchmarks.02_bench_set_Int_raw " ,
60+ runFunction: { _ in try ! bench_set_String_raw ( times: 100 ) } ,
61+ tags: [ ] ,
62+ setUpFunction: { setUp ( ) } ,
63+ tearDownFunction: tearDown
64+ ) ,
65+ BenchmarkInfo (
66+ name: " SpanAttributesDSLBenchmarks.03_bench_set_Int_dsl " ,
67+ runFunction: { _ in try ! bench_set_String_dsl ( times: 100 ) } ,
68+ tags: [ ] ,
69+ setUpFunction: { setUp ( ) } ,
70+ tearDownFunction: tearDown
71+ ) ,
72+ ]
73+
74+ fileprivate static var span : ( any Tracing . Span ) !
75+
76+ fileprivate static func setUp( ) {
77+ self . span = InstrumentationSystem . legacyTracer. startAnySpan ( " something " , context: . topLevel)
78+ }
7779
78- private func tearDown( ) {
79- span = nil
80- }
80+ fileprivate static func tearDown( ) {
81+ self . span = nil
82+ }
8183
82- // ==== ----------------------------------------------------------------------------------------------------------------
83- // MARK: make span
84+ // ==== ----------------------------------------------------------------------------------------------------------------
85+ // MARK: make span
8486
85- func bench_empty( times: Int ) throws { }
87+ static func bench_empty( times: Int ) throws { }
8688
87- func bench_makeSpan( times: Int ) throws {
88- for _ in 0 ..< times {
89- let span = InstrumentationSystem . legacyTracer. startAnySpan ( " something " , context: . topLevel)
90- _ = span
89+ static func bench_makeSpan( times: Int ) throws {
90+ for _ in 0 ..< times {
91+ let span = InstrumentationSystem . legacyTracer. startAnySpan ( " something " , context: . topLevel)
92+ _ = span
93+ }
9194 }
92- }
9395
94- func bench_startSpan_end( times: Int ) throws {
95- for _ in 0 ..< times {
96- let span = InstrumentationSystem . legacyTracer. startAnySpan ( " something " , context: . topLevel)
97- span. end ( )
96+ static func bench_startSpan_end( times: Int ) throws {
97+ for _ in 0 ..< times {
98+ let span = InstrumentationSystem . legacyTracer. startAnySpan ( " something " , context: . topLevel)
99+ span. end ( )
100+ }
98101 }
99- }
100102
101- // ==== ----------------------------------------------------------------------------------------------------------------
102- // MARK: set String
103+ // ==== ----------------------------------------------------------------------------------------------------------------
104+ // MARK: set String
103105
104- func bench_set_String_raw( times: Int ) throws {
105- for _ in 0 ..< times {
106- span. attributes [ " http.method " ] = " POST "
106+ static func bench_set_String_raw( times: Int ) throws {
107+ for _ in 0 ..< times {
108+ self . span. attributes [ " http.method " ] = " POST "
109+ }
107110 }
108- }
109111
110- func bench_set_String_dsl( times: Int ) throws {
111- for _ in 0 ..< times {
112- span. attributes. http. method = " POST "
112+ static func bench_set_String_dsl( times: Int ) throws {
113+ for _ in 0 ..< times {
114+ self . span. attributes. http. method = " POST "
115+ }
113116 }
114- }
115117
116- // ==== ----------------------------------------------------------------------------------------------------------------
117- // MARK: set Int
118+ // ==== ----------------------------------------------------------------------------------------------------------------
119+ // MARK: set Int
118120
119- func bench_set_Int_raw( times: Int ) throws {
120- for _ in 0 ..< times {
121- span. attributes [ " http.status_code " ] = 200
121+ static func bench_set_Int_raw( times: Int ) throws {
122+ for _ in 0 ..< times {
123+ self . span. attributes [ " http.status_code " ] = 200
124+ }
122125 }
123- }
124126
125- func bench_set_Int_dsl( times: Int ) throws {
126- for _ in 0 ..< times {
127- span. attributes. http. statusCode = 200
127+ static func bench_set_Int_dsl( times: Int ) throws {
128+ for _ in 0 ..< times {
129+ self . span. attributes. http. statusCode = 200
130+ }
131+ }
132+
133+ @dynamicMemberLookup
134+ struct HTTPAttributes : SpanAttributeNamespace {
135+ var attributes : SpanAttributes
136+
137+ init ( attributes: SpanAttributes ) {
138+ self . attributes = attributes
139+ }
140+
141+ struct NestedSpanAttributes : NestedSpanAttributesProtocol {
142+ init ( ) { }
143+
144+ var method : Key < String > { " http.method " }
145+ var statusCode : Key < Int > { " http.status_code " }
146+ }
128147 }
129148}
130149
150+ @available ( macOS 10 . 15 , iOS 13 , tvOS 13 , watchOS 6 , * ) // for TaskLocal ServiceContext
131151extension SpanAttributes {
132- var http : HTTPAttributes {
152+ var http : DSLBenchmarks . HTTPAttributes {
133153 get {
134154 . init( attributes: self )
135155 }
@@ -138,19 +158,3 @@ extension SpanAttributes {
138158 }
139159 }
140160}
141-
142- @dynamicMemberLookup
143- struct HTTPAttributes : SpanAttributeNamespace {
144- var attributes : SpanAttributes
145-
146- init ( attributes: SpanAttributes ) {
147- self . attributes = attributes
148- }
149-
150- struct NestedSpanAttributes : NestedSpanAttributesProtocol {
151- init ( ) { }
152-
153- var method : Key < String > { " http.method " }
154- var statusCode : Key < Int > { " http.status_code " }
155- }
156- }
0 commit comments