Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String optimization for HystrixRequestLog #291

Merged
merged 1 commit into from
Aug 8, 2014
Merged
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
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