-
Notifications
You must be signed in to change notification settings - Fork 714
/
EndToEndBenchmarks.java
214 lines (192 loc) · 7.96 KB
/
EndToEndBenchmarks.java
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
204
205
206
207
208
209
210
211
212
213
214
/*
* Copyright The OpenZipkin Authors
* SPDX-License-Identifier: Apache-2.0
*/
package brave;
import brave.baggage.BaggageField;
import brave.baggage.BaggagePropagation;
import brave.baggage.BaggagePropagationConfig.SingleBaggageField;
import brave.context.log4j2.ThreadContextScopeDecorator;
import brave.handler.SpanHandler;
import brave.http.HttpServerBenchmarks;
import brave.okhttp3.TracingCallFactory;
import brave.propagation.B3Propagation;
import brave.propagation.ThreadLocalCurrentTraceContext;
import brave.sampler.Sampler;
import brave.servlet.TracingFilter;
import io.undertow.servlet.Servlets;
import io.undertow.servlet.api.DeploymentInfo;
import io.undertow.servlet.api.FilterInfo;
import java.io.IOException;
import java.util.Date;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import okhttp3.Call;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import static javax.servlet.DispatcherType.REQUEST;
/** Uses the canonical zipkin frontend-backend app, with the fastest components */
public class EndToEndBenchmarks extends HttpServerBenchmarks {
public static final BaggageField REQUEST_ID = BaggageField.create("x-vcap-request-id");
public static final BaggageField COUNTRY_CODE = BaggageField.create("country-code");
public static final BaggageField USER_ID = BaggageField.create("user-id");
static volatile int PORT;
static class HelloServlet extends HttpServlet {
final Call.Factory callFactory = new OkHttpClient();
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
resp.addHeader("Content-Type", "text/plain; charset=UTF-8");
// regardless of how we got here, if we are "api" we just return a string
if (req.getRequestURI().endsWith("/api")) {
resp.getWriter().println(new Date().toString());
} else {
// noop if baggage propagation is not configured
COUNTRY_CODE.updateValue("FO");
Request request = new Request.Builder().url(new HttpUrl.Builder()
.scheme("http")
.host("127.0.0.1")
.port(PORT)
.encodedPath(req.getRequestURI() + "/api").build()).build();
// If we are tracing, we'll have a scoped call factory available
Call.Factory localCallFactory =
(Call.Factory) req.getAttribute(Call.Factory.class.getName());
if (localCallFactory == null) localCallFactory = callFactory;
resp.getWriter().println(localCallFactory.newCall(request).execute().body().string());
}
}
}
public static class Unsampled extends ForwardingTracingFilter {
public Unsampled() {
super(Tracing.newBuilder()
.sampler(Sampler.NEVER_SAMPLE)
.addSpanHandler(new SpanHandler() {
// intentionally not NOOP to ensure spans report
})
.build());
}
}
public static class OnlySampledLocal extends ForwardingTracingFilter {
public OnlySampledLocal() {
super(Tracing.newBuilder()
.addSpanHandler(new SpanHandler() {
// anonymous subtype prevents all recording from being no-op
})
.alwaysSampleLocal()
.sampler(Sampler.NEVER_SAMPLE)
.build());
}
}
public static class Traced extends ForwardingTracingFilter {
public Traced() {
super(Tracing.newBuilder()
.addSpanHandler(new SpanHandler() {
// intentionally not NOOP to ensure spans report
})
.build());
}
}
public static class TracedCorrelated extends ForwardingTracingFilter {
public TracedCorrelated() {
super(Tracing.newBuilder()
.currentTraceContext(ThreadLocalCurrentTraceContext.newBuilder()
// intentionally added twice to test overhead of multiple correlations
.addScopeDecorator(ThreadContextScopeDecorator.get())
.addScopeDecorator(ThreadContextScopeDecorator.get())
.build())
.addSpanHandler(new SpanHandler() {
// intentionally not NOOP to ensure spans report
})
.build());
}
}
public static class TracedBaggage extends ForwardingTracingFilter {
public TracedBaggage() {
super(Tracing.newBuilder()
.propagationFactory(BaggagePropagation.newFactoryBuilder(B3Propagation.FACTORY)
.add(SingleBaggageField.remote(REQUEST_ID))
.add(SingleBaggageField.newBuilder(COUNTRY_CODE)
.addKeyName("baggage-country-code")
.build())
.add(SingleBaggageField.newBuilder(USER_ID)
.addKeyName("baggage-user-id")
.build())
.build())
.addSpanHandler(new SpanHandler() {
// intentionally not NOOP to ensure spans report
})
.build());
}
}
public static class Traced128 extends ForwardingTracingFilter {
public Traced128() {
super(Tracing.newBuilder()
.traceId128Bit(true)
.addSpanHandler(new SpanHandler() {
// intentionally not NOOP to ensure spans report
})
.build());
}
}
@Override protected void init(DeploymentInfo servletBuilder) {
servletBuilder.addFilter(new FilterInfo("Unsampled", Unsampled.class))
.addFilterUrlMapping("Unsampled", "/unsampled", REQUEST)
.addFilterUrlMapping("Unsampled", "/unsampled/api", REQUEST)
.addFilter(new FilterInfo("OnlySampledLocal", OnlySampledLocal.class))
.addFilterUrlMapping("OnlySampledLocal", "/onlysampledlocal", REQUEST)
.addFilterUrlMapping("OnlySampledLocal", "/onlysampledlocal/api", REQUEST)
.addFilter(new FilterInfo("Traced", Traced.class))
.addFilterUrlMapping("Traced", "/traced", REQUEST)
.addFilterUrlMapping("Traced", "/traced/api", REQUEST)
.addFilter(new FilterInfo("TracedBaggage", TracedBaggage.class))
.addFilterUrlMapping("TracedBaggage", "/tracedBaggage", REQUEST)
.addFilterUrlMapping("TracedBaggage", "/tracedBaggage/api", REQUEST)
.addFilter(new FilterInfo("TracedCorrelated", TracedCorrelated.class))
.addFilterUrlMapping("TracedCorrelated", "/tracedcorrelated", REQUEST)
.addFilterUrlMapping("TracedCorrelated", "/tracedcorrelated/api", REQUEST)
.addFilter(new FilterInfo("Traced128", Traced128.class))
.addFilterUrlMapping("Traced128", "/traced128", REQUEST)
.addFilterUrlMapping("Traced128", "/traced128/api", REQUEST)
.addServlets(Servlets.servlet("HelloServlet", HelloServlet.class).addMapping("/*"));
}
@Override protected int initServer() throws Exception {
return PORT = super.initServer();
}
// Convenience main entry-point
public static void main(String[] args) throws Exception {
Options opt = new OptionsBuilder()
.addProfiler("gc")
.include(".*"
+ EndToEndBenchmarks.class.getSimpleName()
+ ".*(tracedCorrelatedServer_get|tracedServer_get)$")
.build();
new Runner(opt).run();
}
static class ForwardingTracingFilter implements Filter {
final Filter delegate;
final Call.Factory callFactory;
public ForwardingTracingFilter(Tracing tracing) {
this.delegate = TracingFilter.create(tracing);
this.callFactory = TracingCallFactory.create(tracing, new OkHttpClient());
}
@Override public void init(FilterConfig filterConfig) throws ServletException {
}
@Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
FilterChain filterChain) throws IOException, ServletException {
servletRequest.setAttribute(Call.Factory.class.getName(), callFactory);
delegate.doFilter(servletRequest, servletResponse, filterChain);
}
@Override public void destroy() {
}
}
}