Skip to content

Instantly share code, notes, and snippets.

@jamie-mcilroy
Last active November 27, 2020 19:43
Show Gist options
  • Save jamie-mcilroy/aeb9a64c55441f7869164a43be74c057 to your computer and use it in GitHub Desktop.
Save jamie-mcilroy/aeb9a64c55441f7869164a43be74c057 to your computer and use it in GitHub Desktop.
regex tests with a loop
@Test
void testTeamRegexInALoop() {
//of note, the regex is outside this method. it's Class level now Pattern pat = Pattern.compile("\\{.*\\}" );
long testStart = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
String expectedStringToFind =String.format("{status: Running, ballpos: %s, distance: 1.143484}",Math.random());
String sfeed = String.format("efewfewfew%sdweewdew", expectedStringToFind);
long startTime = System.currentTimeMillis();
Matcher matcher = pat.matcher(sfeed);
matcher.toMatchResult();
assertTrue(matcher.find());
assertEquals(expectedStringToFind, matcher.group());
long endTime = System.currentTimeMillis();
long totalTime = endTime-startTime;
assertTrue(totalTime<10);
if (totalTime>=5) {
System.out.println(String.format("testTeamRegexInALoop Time to run (in ms): %s [%s]", totalTime,i));
}
}
System.out.println(String.format("Finished in %s ms", System.currentTimeMillis()-testStart));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment