-
Notifications
You must be signed in to change notification settings - Fork 25
/
MatchSimulation.java
81 lines (70 loc) · 2.92 KB
/
MatchSimulation.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
package ru.tinkoff.gatling.kafka.javaapi.examples;
import io.gatling.javaapi.core.ScenarioBuilder;
import io.gatling.javaapi.core.Simulation;
import org.apache.kafka.clients.producer.ProducerConfig;
import ru.tinkoff.gatling.kafka.javaapi.protocol.KafkaProtocolBuilderNew;
import ru.tinkoff.gatling.kafka.request.KafkaProtocolMessage;
import java.time.Duration;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;
import java.util.stream.Stream;
import static io.gatling.javaapi.core.CoreDsl.*;
import static ru.tinkoff.gatling.kafka.javaapi.KafkaDsl.kafka;
public class MatchSimulation extends Simulation {
private final KafkaProtocolBuilderNew kafkaProtocolMatchByValue = kafka().requestReply()
.producerSettings(
Map.of(
ProducerConfig.ACKS_CONFIG, "1",
ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092"
)
)
.consumeSettings(
Map.of("bootstrap.servers", "localhost:9092")
)
.timeout(Duration.ofSeconds(5))
// for match by message value
.matchByValue();
private byte[] matchByOwnVal(KafkaProtocolMessage message) {
// do something with the message and extract the values you are interested in
// method is called:
// - for each message which will be sent out
// - for each message which has been received
return "Custom Message".getBytes(); // just returning something
}
private final KafkaProtocolBuilderNew kafkaProtocolMatchByMessage = kafka().requestReply()
.producerSettings(
Map.of(
ProducerConfig.ACKS_CONFIG, "1",
ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092"
)
)
.consumeSettings(
Map.of(
"bootstrap.servers", "localhost:9092"
)
)
.timeout(Duration.ofSeconds(5))
.matchByMessage(this::matchByOwnVal);
private final AtomicInteger c = new AtomicInteger(0);
private final Iterator<Map<String, Object>> feeder =
Stream.generate((Supplier<Map<String, Object>>) () -> Collections.singletonMap("kekey", c.incrementAndGet())
).iterator();
private final ScenarioBuilder scn = scenario("Basic")
.feed(feeder)
.exec(
kafka("ReqRep").requestReply()
.requestTopic("test.t")
.replyTopic("test.t")
.send("#{kekey}", """
{ "m": "dkf" }
""", String.class, String.class));
{
setUp(
scn.injectOpen(atOnceUsers(1)))
.protocols(kafkaProtocolMatchByMessage)
.maxDuration(Duration.ofSeconds(120));
}
}