Skip to content

Commit

Permalink
Merge pull request #291 from KoltonAndrus/stringOptimization1.3
Browse files Browse the repository at this point in the history
String optimization for HystrixRequestLog
  • Loading branch information
benjchristensen committed Aug 8, 2014
2 parents 34ad426 + b5aafba commit cbad977
Showing 1 changed file with 27 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,22 +146,31 @@ public String getExecutedCommandsAsString() {
LinkedHashMap<String, Integer> aggregatedCommandsExecuted = new LinkedHashMap<String, Integer>();
Map<String, Integer> aggregatedCommandExecutionTime = new HashMap<String, Integer>();

StringBuilder builder = new StringBuilder();
int estimatedLength = 0;
for (HystrixCommand<?> command : executedCommands) {
StringBuilder displayString = new StringBuilder();
displayString.append(command.getCommandKey().name());
builder.setLength(0);
builder.append(command.getCommandKey().name());

List<HystrixEventType> events = new ArrayList<HystrixEventType>(command.getExecutionEvents());
if (events.size() > 0) {
Collections.sort(events);
displayString.append(Arrays.toString(events.toArray()));
//replicate functionality of Arrays.toString(events.toArray()) to append directly to existing StringBuilder
builder.append("[");
for (HystrixEventType event : events) {
builder.append(event).append(", ");
}
builder.setCharAt(builder.length() - 2, ']');
builder.setLength(builder.length() - 1);
} else {
displayString.append("[Executed]");
builder.append("[Executed]");
}

String display = displayString.toString();
if (aggregatedCommandsExecuted.containsKey(display)) {
// increment the count
aggregatedCommandsExecuted.put(display, aggregatedCommandsExecuted.get(display) + 1);
String display = builder.toString();
estimatedLength += display.length() + 12; //add 12 chars to display length for appending totalExecutionTime and count below
Integer counter = aggregatedCommandsExecuted.get(display);
if( counter != null){
aggregatedCommandsExecuted.put(display, counter + 1);
} else {
// add it
aggregatedCommandsExecuted.put(display, 1);
Expand All @@ -172,7 +181,8 @@ public String getExecutedCommandsAsString() {
// do this so we don't create negative values or subtract values
executionTime = 0;
}
if (aggregatedCommandExecutionTime.containsKey(display)) {
counter = aggregatedCommandExecutionTime.get(display);
if( counter != null && executionTime > 0){
// add to the existing executionTime (sum of executionTimes for duplicate command displayNames)
aggregatedCommandExecutionTime.put(display, aggregatedCommandExecutionTime.get(display) + executionTime);
} else {
Expand All @@ -182,22 +192,23 @@ public String getExecutedCommandsAsString() {

}

StringBuilder header = new StringBuilder();
builder.setLength(0);
builder.ensureCapacity(estimatedLength);
for (String displayString : aggregatedCommandsExecuted.keySet()) {
if (header.length() > 0) {
header.append(", ");
if (builder.length() > 0) {
builder.append(", ");
}
header.append(displayString);
builder.append(displayString);

int totalExecutionTime = aggregatedCommandExecutionTime.get(displayString);
header.append("[").append(totalExecutionTime).append("ms]");
builder.append("[").append(totalExecutionTime).append("ms]");

int count = aggregatedCommandsExecuted.get(displayString);
if (count > 1) {
header.append("x").append(count);
builder.append("x").append(count);
}
}
return header.toString();
return builder.toString();
} catch (Exception e) {
logger.error("Failed to create HystrixRequestLog response header string.", e);
// don't let this cause the entire app to fail so just return "Unknown"
Expand Down

0 comments on commit cbad977

Please sign in to comment.