forked from Stromner/sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKafkaConsumerMessageToCloudEventBenchmark.java
More file actions
106 lines (92 loc) · 3.31 KB
/
Copy pathKafkaConsumerMessageToCloudEventBenchmark.java
File metadata and controls
106 lines (92 loc) · 3.31 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
/*
* Copyright 2018-Present The CloudEvents Authors
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 io.cloudevents.bench.kafka;
import io.cloudevents.jackson.JsonFormat;
import io.cloudevents.kafka.KafkaMessageFactory;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.record.TimestampType;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;
import java.util.Optional;
import static io.cloudevents.core.test.Data.V1_WITH_JSON_DATA_WITH_EXT;
public class KafkaConsumerMessageToCloudEventBenchmark {
@State(Scope.Thread)
public static class BinaryMessage {
public ConsumerRecord<String, byte[]> message;
public BinaryMessage() {
// Hack to generate a consumer message
ProducerRecord<Void, byte[]> inRecord = KafkaMessageFactory
.createWriter("aaa")
.writeBinary(V1_WITH_JSON_DATA_WITH_EXT);
this.message = new ConsumerRecord<>(
"aaa",
0,
0,
0,
TimestampType.NO_TIMESTAMP_TYPE,
ConsumerRecord.NULL_SIZE,
ConsumerRecord.NULL_SIZE,
"aaa",
inRecord.value(),
inRecord.headers(),
Optional.empty()
);
}
}
@Benchmark
@BenchmarkMode(Mode.Throughput)
public void testBinaryEncoding(BinaryMessage binaryMessage, Blackhole bh) {
bh.consume(
KafkaMessageFactory
.createReader(binaryMessage.message)
.toEvent()
);
}
@State(Scope.Thread)
public static class StructuredJsonMessage {
public ConsumerRecord<String, byte[]> message;
public StructuredJsonMessage() {
// Hack to generate a consumer message
ProducerRecord<Void, byte[]> inRecord = KafkaMessageFactory
.createWriter("aaa")
.writeStructured(V1_WITH_JSON_DATA_WITH_EXT, JsonFormat.CONTENT_TYPE);
this.message = new ConsumerRecord<>(
"aaa",
0,
0,
0,
TimestampType.NO_TIMESTAMP_TYPE,
ConsumerRecord.NULL_SIZE,
ConsumerRecord.NULL_SIZE,
"aaa",
inRecord.value(),
inRecord.headers(),
Optional.empty()
);
}
}
@Benchmark
@BenchmarkMode(Mode.Throughput)
public void testStructuredJsonEncoding(StructuredJsonMessage structuredJsonMessage, Blackhole bh) {
bh.consume(
KafkaMessageFactory
.createReader(structuredJsonMessage.message)
.toEvent()
);
}
}