forked from DataDog/dd-trace-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.js
203 lines (189 loc) · 4.41 KB
/
core.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
'use strict'
const benchmark = require('./benchmark')
const proxyquire = require('proxyquire')
const Config = require('../packages/dd-trace/src/config')
const DatadogTracer = require('../packages/dd-trace/src/tracer')
const DatadogSpanContext = require('../packages/dd-trace/src/opentracing/span_context')
const TextMapPropagator = require('../packages/dd-trace/src/opentracing/propagation/text_map')
const Writer = proxyquire('../packages/dd-trace/src/exporters/agent/writer', {
'./request': () => Promise.resolve(),
'../../encode/0.4': {
AgentEncoder: function () {
return { encode () {} }
}
}
})
const Sampler = require('../packages/dd-trace/src/sampler')
const format = require('../packages/dd-trace/src/format')
const { AgentEncoder: Agent04Encoder } = require('../packages/dd-trace/src/encode/0.4')
const { AgentEncoder: Agent05Encoder } = require('../packages/dd-trace/src/encode/0.5')
const config = new Config({ service: 'benchmark' })
const id = require('../packages/dd-trace/src/id')
const Histogram = require('../packages/dd-trace/src/histogram')
const histogram = new Histogram()
const runtimeMetrics = require('../packages/dd-trace/src/runtime_metrics')
const log = require('../packages/dd-trace/src/log')
const encoder04 = new Agent04Encoder({ flush: () => encoder04.makePayload() })
const encoder05 = new Agent05Encoder({ flush: () => encoder05.makePayload() })
const suite = benchmark('core')
let tracer
let spanContext
let propagator
let carrier
let writer
let sampler
const spanStub = require('./stubs/span')
const span = format(spanStub)
suite
.add('DatadogTracer#startSpan', {
onStart () {
tracer = new DatadogTracer(config)
},
fn () {
tracer.startSpan()
}
})
.add('TextMapPropagator#inject', {
onStart () {
propagator = new TextMapPropagator(config)
carrier = {}
spanContext = new DatadogSpanContext({
traceId: id('1234567812345678'),
spanId: id('1234567812345678'),
baggageItems: { foo: 'bar' }
})
},
fn () {
propagator.inject(spanContext, carrier)
}
})
.add('TextMapPropagator#extract', {
onStart () {
propagator = new TextMapPropagator(config)
carrier = {
'x-datadog-trace-id': '1234567891234567',
'x-datadog-parent-id': '1234567891234567',
'ot-baggage-foo': 'bar'
}
},
fn () {
propagator.extract(carrier)
}
})
.add('Writer#append', {
onStart () {
writer = new Writer({ sample: () => {} })
},
fn () {
writer.append([span])
}
})
.add('Sampler#isSampled', {
onStart () {
sampler = new Sampler(0.5)
},
fn () {
sampler.isSampled()
}
})
.add('format', {
fn () {
format(spanStub)
}
})
.add('encode (0.4)', {
fn () {
encoder04.encode([span])
}
})
.add('encode (0.5)', {
fn () {
encoder05.encode([span])
}
})
.add('id', {
fn () {
id()
}
})
.add('Histogram', {
fn () {
histogram.record(Math.round(Math.random() * 3.6e12))
}
})
.add('metrics#boolean', {
fn () {
runtimeMetrics.boolean('test', Math.random() < 0.5)
}
})
.add('metrics#histogram', {
fn () {
runtimeMetrics.histogram('test', Math.random() * 3.6e12)
}
})
.add('metrics#gauge', {
fn () {
runtimeMetrics.gauge('test', Math.random())
}
})
.add('metrics#increment', {
fn () {
runtimeMetrics.boolean('test')
}
})
.add('metrics#increment (monotonic)', {
fn () {
runtimeMetrics.boolean('test', true)
}
})
.add('metrics#decrement', {
fn () {
runtimeMetrics.boolean('test')
}
})
.add('log (debug)', {
onStart () {
log.use({
debug: () => {},
error: () => {}
})
log.toggle(true, 'debug')
},
onComplete () {
log.toggle(false)
},
fn () {
log.debug('hello')
}
})
.add('log (error)', {
onStart () {
log.use({
debug: () => {},
error: () => {}
})
log.toggle(true, 'error')
},
onComplete () {
log.toggle(false)
},
fn () {
log.error('boom')
}
})
.add('log (none)', {
onStart () {
log.use({
debug: () => {},
error: () => {}
})
log.toggle(true, 'error')
},
onComplete () {
log.toggle(false)
},
fn () {
log.debug(() => (new Error('boom')).message)
}
})
suite.run()