Skip to content

Commit

Permalink
#424 Fix Simple key producing issue
Browse files Browse the repository at this point in the history
  • Loading branch information
jemacineiras committed Nov 20, 2023
1 parent f0478f3 commit 4bdc6a7
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 68 deletions.
2 changes: 1 addition & 1 deletion pom-maven-central.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<artifactId>kloadgen</artifactId>

<version>5.6.10</version>
<version>5.6.11</version>

<name>KLoadGen</name>
<description>Load Generation Jmeter plugin for Kafka Cluster. Supporting AVRO, JSON Schema and Protobuf schema types. Generate Artificial
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<artifactId>kloadgen</artifactId>

<version>5.6.10</version>
<version>5.6.11</version>

<name>KLoadGen</name>
<description>Load Generation Jmeter plugin for Kafka Cluster. Supporting AVRO, JSON Schema and Protobuf schema types. Generate Artificial
Expand Down
85 changes: 28 additions & 57 deletions src/main/java/com/sngular/kloadgen/randomtool/util/ValueUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

import org.apache.avro.Schema;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.jmeter.threads.JMeterContextService;

public class ValueUtils {
Expand All @@ -25,11 +27,16 @@ private ValueUtils() {
}

public static List<String> replaceValuesContext(final List<String> fieldValuesList) {
final List<String> parameterList = new ArrayList<>(fieldValuesList);
final List<String> parameterList;
if (CollectionUtils.isNotEmpty(fieldValuesList)) {
parameterList = new ArrayList<>(fieldValuesList);
parameterList.replaceAll(fieldValue ->
fieldValue.matches("\\$\\{\\w*}")
? JMeterContextService.getContext().getVariables().get(fieldValue.substring(2, fieldValue.length() - 1)) : fieldValue);
} else {
parameterList = Collections.emptyList();
}

parameterList.replaceAll(fieldValue ->
fieldValue.matches("\\$\\{\\w*}")
? JMeterContextService.getContext().getVariables().get(fieldValue.substring(2, fieldValue.length() - 1)) : fieldValue);
return parameterList;
}

Expand All @@ -44,59 +51,23 @@ public static String replaceValueContext(final String fieldValue) {
public static Object castValue(final Object valueObject, final String type) {
final Object castValue;
final String value = valueObject.toString();
switch (type) {
case ValidTypeConstants.INT:
castValue = Integer.valueOf(value);
break;
case ValidTypeConstants.DOUBLE:
castValue = Double.valueOf(value);
break;
case ValidTypeConstants.LONG:
castValue = Long.valueOf(value);
break;
case ValidTypeConstants.FLOAT:
castValue = Float.valueOf(value);
break;
case ValidTypeConstants.SHORT:
castValue = Short.valueOf(value);
break;
case ValidTypeConstants.BOOLEAN:
castValue = Boolean.valueOf(value);
break;
case ValidTypeConstants.LONG_TIMESTAMP:
castValue = LocalDateTime.parse(value.trim()).toInstant(ZoneOffset.UTC).toEpochMilli();
break;
case ValidTypeConstants.STRING_TIMESTAMP:
castValue = LocalDateTime.parse(value.trim()).toString();
break;
case ValidTypeConstants.INT_DATE:
castValue = LocalDate.parse(value.trim());
break;
case ValidTypeConstants.INT_TIME_MILLIS:
case ValidTypeConstants.LONG_TIME_MICROS:
case ValidTypeConstants.LONG_LOCAL_TIMESTAMP_MILLIS:
castValue = LocalTime.parse(value.trim());
break;
case ValidTypeConstants.LONG_TIMESTAMP_MILLIS:
case ValidTypeConstants.LONG_TIMESTAMP_MICROS:
castValue = LocalDateTime.parse(value.trim()).toInstant(ZoneOffset.UTC);
break;
case ValidTypeConstants.TIMESTAMP:
case ValidTypeConstants.LONG_LOCAL_TIMESTAMP_MICROS:
castValue = LocalDateTime.parse(value.trim());
break;
case ValidTypeConstants.UUID:
case ValidTypeConstants.STRING_UUID:
castValue = UUID.fromString(value);
break;
case ValidTypeConstants.BYTES_DECIMAL:
case ValidTypeConstants.FIXED_DECIMAL:
castValue = new BigDecimal(value);
break;
default:
castValue = value;
break;
}
castValue = switch (type) {
case ValidTypeConstants.INT -> Integer.valueOf(value);
case ValidTypeConstants.DOUBLE -> Double.valueOf(value);
case ValidTypeConstants.LONG -> Long.valueOf(value);
case ValidTypeConstants.FLOAT -> Float.valueOf(value);
case ValidTypeConstants.SHORT -> Short.valueOf(value);
case ValidTypeConstants.BOOLEAN -> Boolean.valueOf(value);
case ValidTypeConstants.LONG_TIMESTAMP -> LocalDateTime.parse(value.trim()).toInstant(ZoneOffset.UTC).toEpochMilli();
case ValidTypeConstants.STRING_TIMESTAMP -> LocalDateTime.parse(value.trim()).toString();
case ValidTypeConstants.INT_DATE -> LocalDate.parse(value.trim());
case ValidTypeConstants.INT_TIME_MILLIS, ValidTypeConstants.LONG_TIME_MICROS, ValidTypeConstants.LONG_LOCAL_TIMESTAMP_MILLIS -> LocalTime.parse(value.trim());
case ValidTypeConstants.LONG_TIMESTAMP_MILLIS, ValidTypeConstants.LONG_TIMESTAMP_MICROS -> LocalDateTime.parse(value.trim()).toInstant(ZoneOffset.UTC);
case ValidTypeConstants.TIMESTAMP, ValidTypeConstants.LONG_LOCAL_TIMESTAMP_MICROS -> LocalDateTime.parse(value.trim());
case ValidTypeConstants.UUID, ValidTypeConstants.STRING_UUID -> UUID.fromString(value);
case ValidTypeConstants.BYTES_DECIMAL, ValidTypeConstants.FIXED_DECIMAL -> new BigDecimal(value);
default -> value;
};
return castValue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,10 @@ public void setupTest(final JavaSamplerContext context) {
if (!Objects.isNull(JMeterContextService.getContext().getVariables().get(PropsKeysHelper.KEY_SUBJECT_NAME))) {
keyGenerator = SamplerUtil.configureKeyGenerator(props);
} else {
msgKeyType = props.getProperty(PropsKeysHelper.MESSAGE_KEY_KEY_TYPE);
msgKeyValue = PropsKeysHelper.MSG_KEY_VALUE.equalsIgnoreCase(props.getProperty(PropsKeysHelper.MESSAGE_KEY_KEY_VALUE))
? Collections.emptyList() : Collections.singletonList(props.getProperty(PropsKeysHelper.MESSAGE_KEY_KEY_VALUE));
msgKeyType = JavaSamplerContext.getJMeterVariables().get(PropsKeysHelper.KEY_TYPE);
msgKeyValue =
Objects.isNull(JavaSamplerContext.getJMeterVariables().get(PropsKeysHelper.KEY_VALUE))
? Collections.emptyList() : Collections.singletonList(JavaSamplerContext.getJMeterVariables().get(PropsKeysHelper.KEY_VALUE));
}
} else {
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, ProducerKeysHelper.KEY_SERIALIZER_CLASS_CONFIG_DEFAULT);
Expand Down
7 changes: 1 addition & 6 deletions src/main/java/com/sngular/kloadgen/sampler/SamplerUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -395,12 +395,7 @@ public static BaseLoadGenerator configureValueGenerator(final Properties props)
}
} else {
try {
final String schema;
if (jMeterVariables.get(PropsKeysHelper.VALUE_SCHEMA).isEmpty()) {
schema = props.getProperty(PropsKeysHelper.VALUE_SCHEMA);
} else {
schema = jMeterVariables.get(PropsKeysHelper.VALUE_SCHEMA);
}
final String schema = StringUtils.defaultIfEmpty(jMeterVariables.get(PropsKeysHelper.VALUE_SCHEMA), props.getProperty(PropsKeysHelper.VALUE_SCHEMA));
generator.setUpGenerator(schema, (List<FieldValueMapping>) jMeterVariables.getObject(PropsKeysHelper.VALUE_SCHEMA_PROPERTIES));
} catch (final SchemaParseException exc) {
generator.setUpGenerator(props.getProperty(PropsKeysHelper.VALUE_SCHEMA), (List<FieldValueMapping>) jMeterVariables.getObject(PropsKeysHelper.VALUE_SCHEMA_PROPERTIES));
Expand Down

0 comments on commit 4bdc6a7

Please sign in to comment.