-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathTrackerBenchmark.java
More file actions
122 lines (103 loc) · 4.13 KB
/
TrackerBenchmark.java
File metadata and controls
122 lines (103 loc) · 4.13 KB
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
/*
* Copyright (c) 2014-present Snowplow Analytics Ltd. All rights reserved.
*
* This program is licensed to you under the Apache License Version 2.0,
* and you may not use this file except in compliance with the Apache License Version 2.0.
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the Apache License Version 2.0 is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
*/
package com.snowplowanalytics;
import com.snowplowanalytics.snowplow.tracker.Tracker;
import com.snowplowanalytics.snowplow.tracker.emitter.BatchEmitter;
import com.snowplowanalytics.snowplow.tracker.emitter.Emitter;
import com.snowplowanalytics.snowplow.tracker.events.PageView;
import com.snowplowanalytics.snowplow.tracker.http.HttpClientAdapter;
import com.snowplowanalytics.snowplow.tracker.payload.SelfDescribingJson;
import com.snowplowanalytics.snowplow.tracker.payload.TrackerPayload;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;
import java.util.concurrent.TimeUnit;
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Warmup(iterations = 15, time = 500, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 20, time = 500, timeUnit = TimeUnit.MILLISECONDS)
@Fork(5)
public class TrackerBenchmark {
public static class MockHttpClientAdapter implements HttpClientAdapter {
@Override
public int post(SelfDescribingJson payload) {
return 200;
}
@Override
public int get(TrackerPayload payload) {
return 0;
}
@Override
public String getUrl() {
return null;
}
@Override
public Object getHttpClient() {
return null;
}
}
public static BatchEmitter getEmitter() {
MockHttpClientAdapter mockHttpClientAdapter = new MockHttpClientAdapter();
return BatchEmitter.builder()
.httpClientAdapter(mockHttpClientAdapter)
.build();
}
public static Tracker getTracker(Emitter emitter) {
return new Tracker.TrackerBuilder(emitter, "namespace", "appId").build();
}
public static void closeThreads(Tracker tracker) {
BatchEmitter emitter = (BatchEmitter) tracker.getEmitter();
emitter.close();
}
// This State class exists only to print out the tracker version
@State(Scope.Benchmark)
public static class TrackerVersion {
BatchEmitter emitter = getEmitter();
Tracker tracker = getTracker(emitter);
@Setup(Level.Trial)
public void printTrackerVersion() {
System.out.println("Using tracker version: " + tracker.getTrackerVersion());
}
@TearDown(Level.Trial)
public void doTearDown() {
System.out.println("Do TearDown for trackerVersion state");
closeThreads(tracker);
}
}
// This class creates the tracker components.
// They are recreated for every iteration of the benchmark test.
@State(Scope.Benchmark)
public static class TrackerComponents {
Tracker tracker;
BatchEmitter emitter;
PageView pageViewEvent = PageView.builder()
.pageUrl("url")
.pageTitle("title")
.referrer("referrer")
.build();
@Setup(Level.Iteration)
public void doSetUp() {
emitter = getEmitter();
tracker = getTracker(emitter);
}
@TearDown(Level.Iteration)
public void doTearDown() {
closeThreads(tracker);
}
}
// The Blackhole forces JMH to measure the method.
@Benchmark
public void testTrackEvent(Blackhole blackhole, TrackerComponents trackerComponents, TrackerVersion trackerVersion) {
trackerComponents.tracker.track(trackerComponents.pageViewEvent);
blackhole.consume(trackerComponents);
}
}