-
Notifications
You must be signed in to change notification settings - Fork 461
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor DefaultSplitCharacter#checkDatePattern to increase perfomance
DEVSIX-4680
- Loading branch information
Showing
3 changed files
with
5,062 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
54 changes: 54 additions & 0 deletions
54
itext/src/test/java/com/itextpdf/text/pdf/DefaultSplitCharacterProfilingTest.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.itextpdf.text.pdf; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.FileReader; | ||
import java.text.MessageFormat; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public class DefaultSplitCharacterProfilingTest { | ||
|
||
private static final String INPUT_DIR = "./src/test/resources/com/itextpdf/text/pdf/DefaultSplitCharacterProfilingTest/"; | ||
|
||
private static final String CHECK_DATE_PATTERN_FAIL_MESSAGE = | ||
"The test verifies the optimization of the checkDatePattern method. This failure indicates that the optimization was broken."; | ||
|
||
private static final String READ_FILE_FAIL_MESSAGE = "Failed to read test file {0}. The test could not be completed."; | ||
|
||
private static final int TIME_LIMIT = 20000; | ||
|
||
@Test(timeout = 30000) | ||
public void checkDatePatternProfilingTest() { | ||
String testFile = INPUT_DIR + "profilingText.txt"; | ||
String str = readFile(testFile); | ||
if (str == null) { | ||
Assert.fail(MessageFormat.format(READ_FILE_FAIL_MESSAGE, testFile)); | ||
} | ||
long startTime = System.currentTimeMillis(); | ||
for (int i = 0; i < 70000; i++) { | ||
isSplitCharacter(str); | ||
} | ||
long time = System.currentTimeMillis() - startTime; | ||
System.out.println("Test run time: " + time); | ||
Assert.assertTrue(CHECK_DATE_PATTERN_FAIL_MESSAGE, time < TIME_LIMIT); | ||
} | ||
|
||
private static void isSplitCharacter(String text) { | ||
new DefaultSplitCharacter().isSplitCharacter(0, 0, text.length() + 1, text.toCharArray(), null); | ||
} | ||
|
||
private static String readFile(String fileName) { | ||
StringBuilder stringBuilder = new StringBuilder(); | ||
try { | ||
BufferedReader reader = new BufferedReader(new FileReader(fileName)); | ||
String line; | ||
while ((line = reader.readLine()) != null) { | ||
stringBuilder.append(line); | ||
} | ||
reader.close(); | ||
return stringBuilder.toString(); | ||
} catch (Exception e) { | ||
return null; | ||
} | ||
} | ||
} |
Oops, something went wrong.