Skip to content

Commit

Permalink
make PatternConfig list
Browse files Browse the repository at this point in the history
  • Loading branch information
cmendesce committed Jan 4, 2024
1 parent 4aaa897 commit c0cb4e9
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ public static List<Map<String, Object>> expandConfigTemplateJson(Map<String, Jso
return resultList;
}

public static List<Map<String, Object>> expandConfigTemplate(List<PatternConfig> patternConfigs) {
public static List<Map<String, Object>> expandConfigTemplate(PatternConfig patternConfigs) {
List<Map<String, Object>> resultList = new ArrayList<>();
resultList.add(new HashMap<>());

for (PatternConfig config : patternConfigs) {
for (var config : patternConfigs) {
String key = config.getName();
JsonNode value = config.getValue();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,62 +5,83 @@
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.ArrayList;
import java.util.Arrays;

import static io.vertx.core.impl.ConversionHelper.toObject;
import static java.util.stream.Collectors.joining;

public class PatternConfig {

@JsonIgnore
private static final ObjectMapper mapper = new ObjectMapper();

private String name;
private JsonNode value;

public class PatternConfig extends ArrayList<PatternConfig.Attribute> {
public PatternConfig() {
}

public PatternConfig(String name, JsonNode value) {
this.name = name;
this.value = value;
public PatternConfig(Attribute... params) {
this.addAll(Arrays.asList(params));
}

public PatternConfig(String name, Object value) {
this(name, mapper.valueToTree(value));
@Override
public String toString() {
var serialized = this.stream()
.map(Attribute::toString)
.collect(joining("."));
if (serialized.isEmpty()) {
serialized = "none";
}
return serialized;
}

public String getName() {
return name;
}
public static class Attribute {
@JsonIgnore
private static final ObjectMapper mapper = new ObjectMapper();

public JsonNode getValue() {
return value;
}
private String name;
private JsonNode value;

public Attribute() {
}

public Attribute(String name, JsonNode value) {
this.name = name;
this.value = value;
}

public Attribute(String name, Object value) {
this(name, mapper.valueToTree(value));
}

@JsonIgnore
public Object getValueAsObject() {
if (getValue().isTextual()) {
return getValue().asText();
} else if (getValue().isNumber()) {
if (getValue().isDouble() || getValue().isFloatingPointNumber()) {
return getValue().doubleValue();
} else if (getValue().isLong()) {
return getValue().longValue();
public String getName() {
return name;
}

public JsonNode getValue() {
return value;
}

@JsonIgnore
public Object getValueAsObject() {
if (getValue().isTextual()) {
return getValue().asText();
} else if (getValue().isNumber()) {
if (getValue().isDouble() || getValue().isFloatingPointNumber()) {
return getValue().doubleValue();
} else if (getValue().isLong()) {
return getValue().longValue();
} else {
return getValue().intValue();
}
} else if (getValue().isBoolean()) {
return getValue().asBoolean();
} else if (getValue().isArray()) {
var list = new ArrayList<>();
getValue().elements().forEachRemaining(element -> list.add(toObject(element)));
return list;
} else {
return getValue().intValue();
return getValue();
}
} else if (getValue().isBoolean()) {
return getValue().asBoolean();
} else if (getValue().isArray()) {
var list = new ArrayList<>();
getValue().elements().forEachRemaining(element -> list.add(toObject(element)));
return list;
} else {
return getValue();
}
}

@Override
public String toString() {
return getName() + "-" + getValueAsObject();
@Override
public String toString() {
return getName() + "-" + getValueAsObject();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static Object convertJsonNode(JsonNode jsonNode) {
}

public static List<Map<String, Object>> expandServiceParameters(Source serviceSource) {
return expandConfigTemplate(serviceSource.getPatternConfigs());
return expandConfigTemplate(serviceSource.getPatternConfig());
}

public static List<Scenario> create(Benchmark benchmark, Workload workload) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,31 @@

import br.unifor.ppgia.resiliencebench.resources.PatternConfig;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.JsonNode;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Source {

private String service;

@JsonInclude(JsonInclude.Include.NON_EMPTY)
private List<PatternConfig> patternConfigs = new ArrayList<>();
private PatternConfig patternConfig = new PatternConfig();

public Source() { }

public Source(String service, List<PatternConfig> patternConfig) {
public Source(String service, PatternConfig patternConfig) {
this.service = service;
this.patternConfigs = patternConfig;
this.patternConfig = patternConfig;
}

public String getService() {
return service;
}


public List<PatternConfig> getPatternConfigs() {
return patternConfigs;
public PatternConfig getPatternConfig() {
return patternConfig;
}

public void setPatternConfigs(List<PatternConfig> patternConfigs) {
this.patternConfigs = patternConfigs;
public void setPatternConfig(PatternConfig patternConfig) {
this.patternConfig = patternConfig;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -77,11 +76,11 @@ public void should_expand_multiple_templates_as_json() {

@Test
public void should_expand_template_as_patternConfig() {
List<PatternConfig> configTemplate = new ArrayList<>();
PatternConfig configTemplate = new PatternConfig();

configTemplate.add(new PatternConfig("slowCallRateThreshold", objectMapper.valueToTree(100)));
configTemplate.add(new PatternConfig("slowCallDurationThreshold", objectMapper.valueToTree(1000)));
configTemplate.add(new PatternConfig("waitDurationInOpenState", objectMapper.valueToTree(List.of(50, 100, 200))));
configTemplate.add(new PatternConfig.Attribute("slowCallRateThreshold", objectMapper.valueToTree(100)));
configTemplate.add(new PatternConfig.Attribute("slowCallDurationThreshold", objectMapper.valueToTree(1000)));
configTemplate.add(new PatternConfig.Attribute("waitDurationInOpenState", objectMapper.valueToTree(List.of(50, 100, 200))));

var expandedConfigs = ListExpansion.expandConfigTemplate(configTemplate);

Expand All @@ -93,11 +92,11 @@ public void should_expand_template_as_patternConfig() {

@Test
public void should_expand_simple_template_as_patternConfig() {
List<PatternConfig> configTemplate = new ArrayList<>();
PatternConfig configTemplate = new PatternConfig();

configTemplate.add(new PatternConfig("slowCallRateThreshold",100));
configTemplate.add(new PatternConfig("slowCallDurationThreshold",1000));
configTemplate.add(new PatternConfig("waitDurationInOpenState", 200));
configTemplate.add(new PatternConfig.Attribute("slowCallRateThreshold",100));
configTemplate.add(new PatternConfig.Attribute("slowCallDurationThreshold",1000));
configTemplate.add(new PatternConfig.Attribute("waitDurationInOpenState", 200));

var expandedConfigs = ListExpansion.expandConfigTemplate(configTemplate);

Expand All @@ -109,10 +108,10 @@ public void should_expand_simple_template_as_patternConfig() {

@Test
public void should_expand_multiple_templates_as_patternConfig() {
List<PatternConfig> configTemplate = new ArrayList<>();
configTemplate.add(new PatternConfig("slowCallRateThreshold", 100));
configTemplate.add(new PatternConfig("slowCallDurationThreshold", List.of(1000, 2000)));
configTemplate.add(new PatternConfig("waitDurationInOpenState", List.of(50, 100, 200)));
PatternConfig configTemplate = new PatternConfig();
configTemplate.add(new PatternConfig.Attribute("slowCallRateThreshold", 100));
configTemplate.add(new PatternConfig.Attribute("slowCallDurationThreshold", List.of(1000, 2000)));
configTemplate.add(new PatternConfig.Attribute("waitDurationInOpenState", List.of(50, 100, 200)));
var expandedConfigs = ListExpansion.expandConfigTemplate(configTemplate);

Assertions.assertEquals(6, expandedConfigs.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public static Benchmark createBenchmark() {

public static Connection createConnection(String connectionName) {
var source = new Source("api-gateway",
List.of(
new PatternConfig("maxAttempts", List.of(1, 2, 3)),
new PatternConfig("backoffLimit", List.of(1000, 2000, 3000))
new PatternConfig(
new PatternConfig.Attribute("maxAttempts", List.of(1, 2, 3)),
new PatternConfig.Attribute("backoffLimit", List.of(1000, 2000, 3000))
)
);
var delay = new DelayFault(1000);
Expand All @@ -52,9 +52,9 @@ public void expandServiceParametersTest() {
var parameters =
ScenarioFactory.expandServiceParameters(
new Source("api-gateway",
List.of(
new PatternConfig("maxAttempts", List.of(1, 2, 3)),
new PatternConfig("backoffLimit", List.of(1000, 2000, 3000))
new PatternConfig(
new PatternConfig.Attribute("maxAttempts", List.of(1, 2, 3)),
new PatternConfig.Attribute("backoffLimit", List.of(1000, 2000, 3000))
)
));
assertEquals(9, parameters.size());
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/benchmark-sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ spec:
connections:
- name: connection-1
source:
patternConfigs:
patternConfig:
- name: intValue
value: 1
- name: stringValue
Expand Down

0 comments on commit c0cb4e9

Please sign in to comment.