Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Move Metadata update to when Sample is tested #84

Merged
merged 1 commit into from
Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/main/java/vn/zalopay/benchmark/GRPCSampler.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ private void initGrpcClient() {
getLibFolder(),
getFullMethod(),
isTls(),
isTlsDisableVerification(),
getMetadata());
isTlsDisableVerification());
}
}

Expand All @@ -65,8 +64,9 @@ public SampleResult sample(Entry ignored) {
try {
initGrpcClient();
sampleResult.setSampleLabel(getName());
String grpcRequest = clientCaller.buildRequest(getRequestJson());
String grpcRequest = clientCaller.buildRequestAndMetadata(getRequestJson(),getMetadata());
sampleResult.setSamplerData(grpcRequest);
sampleResult.setRequestHeaders(clientCaller.getMetadataString());
sampleResult.sampleStart();
grpcResponse = clientCaller.call(getDeadline());
sampleResult.sampleEnd();
Expand Down
23 changes: 18 additions & 5 deletions src/main/java/vn/zalopay/benchmark/core/ClientCaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

public class ClientCaller {
private Descriptors.MethodDescriptor methodDescriptor;
Expand All @@ -42,16 +43,16 @@ public class ClientCaller {
private boolean disableTtlVerification;
ChannelFactory channelFactory;

public ClientCaller(String HOST_PORT, String TEST_PROTO_FILES, String LIB_FOLDER, String FULL_METHOD, boolean TLS, boolean TLS_DISABLE_VERIFICATION, String METADATA) {
this.init(HOST_PORT, TEST_PROTO_FILES, LIB_FOLDER, FULL_METHOD, TLS, TLS_DISABLE_VERIFICATION, METADATA);
public ClientCaller(String HOST_PORT, String TEST_PROTO_FILES, String LIB_FOLDER, String FULL_METHOD, boolean TLS, boolean TLS_DISABLE_VERIFICATION) {
this.init(HOST_PORT, TEST_PROTO_FILES, LIB_FOLDER, FULL_METHOD, TLS, TLS_DISABLE_VERIFICATION);
}

private void init(String HOST_PORT, String TEST_PROTO_FILES, String LIB_FOLDER, String FULL_METHOD, boolean TLS, boolean TLS_DISABLE_VERIFICATION, String METADATA) {
private void init(String HOST_PORT, String TEST_PROTO_FILES, String LIB_FOLDER, String FULL_METHOD, boolean TLS, boolean TLS_DISABLE_VERIFICATION) {
try {
tls = TLS;
disableTtlVerification = TLS_DISABLE_VERIFICATION;
hostAndPort = HostAndPort.fromString(HOST_PORT);
metadataMap = buildHashMetadata(METADATA);
metadataMap = new LinkedHashMap<>();
channelFactory = ChannelFactory.create();
ProtoMethodName grpcMethodName =
ProtoMethodName.parseFullGrpcMethodName(FULL_METHOD);
Expand Down Expand Up @@ -131,10 +132,15 @@ public boolean isTerminated() {
return channel.isTerminated();
}

public String buildRequest(String jsonData) {
public String buildRequestAndMetadata(String jsonData, String metadata) {
try {
metadataMap.clear();
metadataMap.putAll(buildHashMetadata(metadata));
requestMessages = Reader.create(methodDescriptor.getInputType(), jsonData, registry).read();
return JsonFormat.printer().includingDefaultValueFields().print(requestMessages.get(0));
} catch (IllegalArgumentException e) {
shutdownNettyChannel();
throw e;
} catch (Exception e) {
shutdownNettyChannel();
throw new RuntimeException("Caught exception while parsing request for rpc", e);
Expand Down Expand Up @@ -219,4 +225,11 @@ private Long parsingDeadlineTime(String deadlineMs) {
throw new RuntimeException("Caught exception while parsing deadline to long", e);
}
}

public String getMetadataString() {
return metadataMap.entrySet()
.stream()
.map(e -> e.getKey() + ": " + e.getValue())
.collect(Collectors.joining("\n"));
}
}
Loading