Repository navigation
Expand file tree
/
Copy pathDecoderIteratorsBenchmark.java
More file actions
125 lines (111 loc) · 3.5 KB
/
Copy pathDecoderIteratorsBenchmark.java
File metadata and controls
125 lines (111 loc) · 3.5 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
123
124
125
/**
* Copyright 2012-2020 The Feign Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package feign.benchmark;
import com.fasterxml.jackson.core.type.TypeReference;
import feign.Request;
import feign.Request.HttpMethod;
import feign.Response;
import feign.Util;
import feign.codec.Decoder;
import feign.jackson.JacksonDecoder;
import feign.jackson.JacksonIteratorDecoder;
import feign.stream.StreamDecoder;
import org.openjdk.jmh.annotations.*;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
/**
* This test shows up how fast different json array response processing implementations are.
*/
@State(Scope.Thread)
public class DecoderIteratorsBenchmark {
@Param({"list", "iterator", "stream"})
private String api;
@Param({"10", "100"})
private String size;
private Response response;
private Decoder decoder;
private Type type;
@Benchmark
@Warmup(iterations = 5, time = 1)
@Measurement(iterations = 10, time = 1)
@Fork(3)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public void decode() throws Exception {
fetch(decoder.decode(response, type));
}
@SuppressWarnings("unchecked")
private void fetch(Object o) {
Iterator<Car> cars;
if (o instanceof Collection) {
cars = ((Collection<Car>) o).iterator();
} else if (o instanceof Stream) {
cars = ((Stream<Car>) o).iterator();
} else {
cars = (Iterator<Car>) o;
}
while (cars.hasNext()) {
cars.next();
}
}
@SuppressWarnings("deprecation")
@Setup(Level.Invocation)
public void buildResponse() {
response = Response.builder()
.status(200)
.reason("OK")
.request(
Request.create(HttpMethod.GET, "/", Collections.emptyMap(), null, Util.UTF_8, null))
.headers(Collections.emptyMap())
.body(carsJson(Integer.parseInt(size)), Util.UTF_8)
.build();
}
@Setup(Level.Trial)
public void buildDecoder() {
switch (api) {
case "list":
decoder = new JacksonDecoder();
type = new TypeReference<List<Car>>() {}.getType();
break;
case "iterator":
decoder = JacksonIteratorDecoder.create();
type = new TypeReference<Iterator<Car>>() {}.getType();
break;
case "stream":
decoder = StreamDecoder.create(JacksonIteratorDecoder.create());
type = new TypeReference<Stream<Car>>() {}.getType();
break;
default:
throw new IllegalStateException("Unknown api: " + api);
}
}
private String carsJson(int count) {
String car = "{\"name\":\"c4\",\"manufacturer\":\"Citroën\"}";
StringBuilder builder = new StringBuilder("[");
builder.append(car);
for (int i = 1; i < count; i++) {
builder.append(",").append(car);
}
return builder.append("]").toString();
}
static class Car {
public String name;
public String manufacturer;
}
}