Created
March 3, 2013 17:02
-
-
Save anonymous/5076963 to your computer and use it in GitHub Desktop.
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
import java.util.Arrays; | |
import java.util.ArrayList; | |
import java.util.Random; | |
import java.util.regex.Pattern; | |
import java.util.regex.Matcher; | |
import java.lang.reflect.Method; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
import java.lang.annotation.Target; | |
import java.lang.annotation.ElementType; | |
class Test_15187583 { | |
@Retention(RetentionPolicy.RUNTIME) | |
@Target(ElementType.METHOD) | |
private @interface Test {}; | |
// Number of times to run all methods | |
private static final int NUM_OUTER = 3; | |
// Number of times to run each method | |
private static final int NUM_REPEAT = 10; | |
public static void main(String args[]) { | |
runTestReflection(); | |
} | |
private static Pattern p1 = Pattern.compile("[0123]123456|98765"); | |
@Test | |
public static int match_1(String input) { | |
Matcher m = p1.matcher(input); | |
int count = 0; | |
while (m.find()) { | |
count++; | |
} | |
return count; | |
} | |
private static Pattern p2a = Pattern.compile("[0123]123456"); | |
private static Pattern p2b = Pattern.compile("98765"); | |
@Test | |
public static int match_2(String input) { | |
int count = 0; | |
Matcher m1 = p2a.matcher(input); | |
while (m1.find()) { | |
count++; | |
} | |
Matcher m2 = p2b.matcher(input); | |
while (m2.find()) { | |
count++; | |
} | |
return count; | |
} | |
// Half definitely has a match | |
private static final double HAS_PATTERN = 0.50; | |
private static Random rand = new Random(); | |
// Number of strings to test on | |
private static final int NUM_INPUT = 50000; | |
// From 10 - 90 characters | |
private static final int LENGTH = 50; | |
private static final int LENGTH_FLUCTUATION = 40; | |
private static final String[] matchPatterns = {"0123456","1123456","2123456","3123456","98765"}; | |
// When the string should have a match, the number of matches in the string will be at least 1 (and on average around 1) | |
// When the string shouldn't have a match, there might be a match if the digit generated coincide with the match (low probability) | |
public static String[] generateInput() { | |
String output[] = new String[NUM_INPUT]; | |
for (int i = 0; i < NUM_INPUT; i++) { | |
StringBuilder s = new StringBuilder(); | |
int length = LENGTH - rand.nextInt(LENGTH_FLUCTUATION * 2 + 1) + LENGTH_FLUCTUATION; | |
if (rand.nextDouble() < HAS_PATTERN) { | |
int patternToUse = rand.nextInt(matchPatterns.length); | |
int randGenCount = length - matchPatterns[patternToUse].length(); | |
for (int j = 0; j < randGenCount; j++) { | |
s.append((char) ('0' + rand.nextInt(10))); | |
} | |
int insertPos = rand.nextInt(randGenCount); | |
s.insert(insertPos, matchPatterns[patternToUse]); | |
} else { | |
for (int j = 0; j < length; j++) { | |
s.append((char) ('0' + rand.nextInt(10))); | |
} | |
} | |
assert(s.length() == length); | |
output[i] = s.toString(); | |
} | |
return output; | |
} | |
private static ArrayList<Method> getTestMethods() { | |
Class<?> klass = null; | |
try { | |
klass = Class.forName(Thread.currentThread().getStackTrace()[1].getClassName()); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
System.err.println("Something really bad happened. Bailling out..."); | |
System.exit(1); | |
} | |
Method[] methods = klass.getMethods(); | |
// System.out.println(klass); | |
// System.out.println(Arrays.toString(methods)); | |
ArrayList<Method> testMethods = new ArrayList<Method>(); | |
for (Method method: methods) { | |
if (method.isAnnotationPresent(Test.class)) { | |
testMethods.add(method); | |
} | |
} | |
// System.out.println(testMethods); | |
return testMethods; | |
} | |
public static void runTestReflection() { | |
ArrayList<Method> methods = getTestMethods(); | |
for (int t = 0; t < NUM_OUTER; t++) { | |
String inputSet[] = generateInput(); | |
Integer compareSet[] = null; | |
for (Method method: methods) { | |
System.out.print(method.getName() + ": "); | |
if (compareSet == null) { | |
compareSet = new Integer[inputSet.length]; | |
try { | |
for (int i = 0; i < inputSet.length; i++) { | |
compareSet[i] = (Integer) method.invoke(null, inputSet[i]); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
System.exit(1); | |
} | |
} else { | |
try { | |
for (int i = 0; i < inputSet.length; i++) { | |
Integer result = (Integer) method.invoke(null, inputSet[i]); | |
if (!compareSet[i].equals(result)) { | |
System.err.println(compareSet[i] + " != " + result + " for input " + inputSet[i]); | |
throw new AssertionError(); | |
} | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
System.exit(1); | |
} | |
} | |
long sum = 0; | |
for (int i = 0; i < NUM_REPEAT; i++) { | |
long start, end; | |
Object result; | |
try { | |
start = System.nanoTime(); | |
for (String input: inputSet) { | |
result = method.invoke(null, input); | |
} | |
end = System.nanoTime(); | |
System.out.print((end - start) / 1000 + " "); | |
sum += (end - start) / 1000; | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
System.out.println("| " + sum / NUM_REPEAT); | |
} | |
System.out.println(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment