Created
October 23, 2012 16:17
-
-
Save jnraine/3939812 to your computer and use it in GitHub Desktop.
Code Share I (October 25)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ca.sfu.cq.util.filter; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
public class TimerMessageParser { | |
private String message; | |
public TimerMessageParser(String message) { | |
this.message = message; | |
} | |
public boolean hasResponseTime() { | |
return messageValue().matches("^TIMER_END.+"); | |
} | |
public String messageValue() { | |
Pattern pattern = Pattern.compile("^\\s*(\\d+) (\\(.+\\)) (TIMER_END\\{.+\\})"); | |
Matcher matcher = pattern.matcher(message); | |
if(matcher.find()) { | |
return matcher.group(3); | |
} else { | |
return message; | |
} | |
} | |
/** | |
* @return the response time in milliseconds | |
*/ | |
public Integer responseTime() { | |
Matcher matcher = valueMatcher(); | |
matcher.find(); | |
return Integer.parseInt(matcher.group(1)); | |
} | |
private Matcher valueMatcher() { | |
Pattern pattern = Pattern.compile("\\{(\\d+),(.+)\\}"); | |
return pattern.matcher(messageValue()); | |
} | |
public String name() { | |
Matcher matcher = valueMatcher(); | |
matcher.find(); | |
String unformattedName = matcher.group(2); | |
return formatName(unformattedName); | |
} | |
private String formatName(String unformattedName) { | |
String formattedName = unformattedName; | |
// Remove trailing #0 | |
formattedName = unformattedName.replaceAll("#\\d*$", ""); | |
// Remove trailing parens and anything inside them | |
formattedName = formattedName.replaceAll("\\(.+\\)$", ""); | |
// Replace .jsp with -jsp | |
formattedName = formattedName.replaceAll("\\.jsp$", "-jsp"); | |
// Replace first forward slashes with nothing | |
formattedName = formattedName.replaceFirst("\\/", ""); | |
// Replace forward slashes with dots | |
formattedName = formattedName.replaceAll("\\/", "."); | |
// Replace spaces with dashes | |
formattedName = formattedName.replaceAll(" ", "-"); | |
return formattedName; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ca.sfu.cq.util.filter; | |
import org.junit.Test; | |
import static org.hamcrest.CoreMatchers.is; | |
import static org.junit.Assert.assertThat; | |
public class TimerMessageParserTest { | |
private String[] parsableMessages = new String[] {" 9 (2012-10-17 15:07:22) TIMER_END{1,ca.sfu.SampleServlet#0}", | |
" 7 (2012-10-17 15:07:22) TIMER_END{7,ResourceResolution} URI=/snapbuild/homeagain.sampleServlet.html resolves to Resource=NonExistingResource, path=/snapbuild/homeagain.sampleServlet.html", | |
" 7 (2012-10-17 15:07:22) TIMER_END{0,resolveServlet(NonExistingResource, path=/snapbuild/homeagain.sampleServlet.html)} Using servlet ca.sfu.SampleServlet", | |
" 97 (2012-10-18 15:55:52) TIMER_END{97,Request Processing} Request Processing", | |
" 97 (2012-10-18 15:55:52) TIMER_END{13,ca.sfu.cq.programfinder.components.AreaOfStudy#0}"}; | |
private String[] unparsableMessages = new String[] {" 7 (2012-10-17 15:07:22) LOG Applying Requestfilters", | |
" 8 (2012-10-17 15:07:22) LOG RedirectFilter did not redirect (method does not match)", | |
" 0 (2012-10-17 15:07:22) TIMER_START{Request Processing}"}; | |
@Test | |
public void testHasResponseTime() { | |
for(String parsableMessage : parsableMessages) { | |
TimerMessageParser timerMessageParser = new TimerMessageParser(parsableMessage); | |
assertThat(parsableMessage + " should have a response time", timerMessageParser.hasResponseTime(), is(true)); | |
} | |
} | |
@Test public void testHasResponseTimeFalse() { | |
for(String unparsableMessage : unparsableMessages) { | |
TimerMessageParser timerMessageParser = new TimerMessageParser(unparsableMessage); | |
assertThat(unparsableMessage + " should not have a response time", timerMessageParser.hasResponseTime(), is(false)); | |
} | |
} | |
@Test public void testMessageValue() { | |
TimerMessageParser timerMessageParser = new TimerMessageParser(parsableMessages[0]); | |
assertThat("extracts message value", timerMessageParser.messageValue(), is("TIMER_END{1,ca.sfu.SampleServlet#0}")); | |
timerMessageParser = new TimerMessageParser(parsableMessages[1]); | |
assertThat("extracts message value", timerMessageParser.messageValue(), is("TIMER_END{7,ResourceResolution}")); | |
timerMessageParser = new TimerMessageParser(parsableMessages[2]); | |
assertThat("extracts message value", timerMessageParser.messageValue(), is("TIMER_END{0,resolveServlet(NonExistingResource, path=/snapbuild/homeagain.sampleServlet.html)}")); | |
} | |
@Test | |
public void testMessageValueWithUglyMessage() { | |
TimerMessageParser timerMessageParser = new TimerMessageParser("I don't fit into your box, system."); | |
assertThat("returns the entire message when no match is found", timerMessageParser.messageValue(), is("I don't fit into your box, system.")); | |
} | |
@Test | |
public void testResponseTime() { | |
TimerMessageParser timerMessageParser = new TimerMessageParser("9 (2012-10-17 15:07:22) TIMER_END{1,ca.sfu.SampleServlet#0}"); | |
assertThat("return the response time", timerMessageParser.responseTime(), is(1)); | |
} | |
@Test | |
public void testResponseTimeDouble() { | |
TimerMessageParser timerMessageParser = new TimerMessageParser("9 (2012-10-17 15:07:22) TIMER_END{1000,ca.sfu.SampleServlet#0}"); | |
assertThat("return the response time", timerMessageParser.responseTime(), is(1000)); | |
} | |
@Test | |
public void testName() { | |
TimerMessageParser timerMessageParser = new TimerMessageParser(parsableMessages[0]); | |
assertThat("returns the name", timerMessageParser.name(), is("ca.sfu.SampleServlet")); | |
timerMessageParser = new TimerMessageParser(parsableMessages[1]); | |
assertThat("returns the name", timerMessageParser.name(), is("ResourceResolution")); | |
timerMessageParser = new TimerMessageParser(parsableMessages[2]); | |
assertThat("returns the name", timerMessageParser.name(), is("resolveServlet")); | |
} | |
@Test | |
public void testNameWithJsp() { | |
TimerMessageParser timerMessageParser = new TimerMessageParser(" 109 (2012-10-18 16:02:16) TIMER_END{1,/apps/sfu/components/dynamictext/dynamictext.jsp#4}"); | |
assertThat("returns the name", timerMessageParser.name(), is("apps.sfu.components.dynamictext.dynamictext-jsp")); | |
} | |
@Test | |
public void testNameWithSpaces() { | |
TimerMessageParser timerMessageParser = new TimerMessageParser(" 80 (2012-10-18 16:08:40) TIMER_END{80,Request Processing} Request Processing"); | |
assertThat("returns the name", timerMessageParser.name(), is("Request-Processing")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment