[slf4j-dev] svn commit: r1113 - slf4j/trunk/slf4j-api/src/test/java/org/slf4j/helpers
ceki at slf4j.org
ceki at slf4j.org
Mon Aug 18 17:51:13 CEST 2008
Author: ceki
Date: Mon Aug 18 17:51:13 2008
New Revision: 1113
Added:
slf4j/trunk/slf4j-api/src/test/java/org/slf4j/helpers/BogoPerf.java
slf4j/trunk/slf4j-api/src/test/java/org/slf4j/helpers/MessageFormatterPerfTest.java
Log:
- added a BogoPerf based test
Added: slf4j/trunk/slf4j-api/src/test/java/org/slf4j/helpers/BogoPerf.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/slf4j-api/src/test/java/org/slf4j/helpers/BogoPerf.java Mon Aug 18 17:51:13 2008
@@ -0,0 +1,127 @@
+package org.slf4j.helpers;
+
+import java.util.Arrays;
+import java.util.Random;
+
+import junit.framework.AssertionFailedError;
+
+public class BogoPerf {
+
+ private static long NANOS_IN_ONE_SECOND = 1000 * 1000 * 1000;
+ private static int INITIAL_N = 1000;
+ private static int LAST_N = 100;
+ private static int SLACK_FACTOR = 2;
+
+ static {
+ // let the JIT warm up
+ computeBogoIPS(INITIAL_N);
+ double bogo_ips = computeBogoIPS(INITIAL_N);
+ System.out.println("Host runs at "+bogo_ips + " BIPS");
+ }
+
+
+ /**
+ * Compute bogoInstructions per second
+ * <p>
+ * on a 3.2 Ghz Pentium D CPU (around 2007), we obtain about 10'000 bogoIPS.
+ *
+ * @param N
+ * number of bogoInstructions to average over in order to
+ * compute the result
+ * @return bogo Instructions Per Second
+ */
+ private static double computeBogoIPS(int N) {
+ long begin = System.nanoTime();
+
+ for (int i = 0; i < N; i++) {
+ bogoInstruction();
+ }
+ long end = System.nanoTime();
+
+ // duration
+ double D = end - begin;
+ // average duration per instruction
+ double avgDPIS = D / N;
+ // System.out.println(D + " nanos for " + N + " instructions");
+ // System.out.println(avgD + " nanos per instruction");
+
+ double bogoIPS = NANOS_IN_ONE_SECOND / avgDPIS;
+ // System.out.println(bogoIPS + " bogoIPS");
+
+ return bogoIPS;
+ }
+
+ private static void bogoInstruction() {
+ Random random = new Random(100);
+ int len = 500;
+ int[] intArray = new int[len];
+ for (int i = 0; i < len; i++) {
+ intArray[i] = random.nextInt();
+ }
+ Arrays.sort(intArray);
+ }
+
+ /**
+ * Computed the BogoIPS for this host CPU.
+ *
+ * @return
+ */
+ public static double currentBIPS() {
+ return computeBogoIPS(LAST_N);
+ }
+
+ static double min(double a, double b) {
+ return (a <= b) ? a : b;
+ }
+
+ /**
+ * Assertion used for values that <b>decrease</b> with faster CPUs,
+ * typically the time (duration) needed to perform a task.
+ *
+ * @param currentDuration
+ * @param referenceDuraion
+ * @param referenceBIPS
+ * @throws AssertionFailedError
+ */
+ public static void assertDuration(double currentDuration,
+ long referenceDuraion, double referenceBIPS)
+ throws AssertionFailedError {
+ double ajustedDuration = adjustExpectedDuration(referenceDuraion,
+ referenceBIPS);
+ if (currentDuration > ajustedDuration * SLACK_FACTOR) {
+ throw new AssertionFailedError(currentDuration + " exceeded expected "
+ + ajustedDuration + " (adjusted), " + referenceDuraion + " (raw)");
+ }
+ }
+ /**
+ * Assertion used for values that <b>increase<b> with faster CPUs, typically
+ * the number of operations accomplished per unit of time.
+ *
+ * @param currentPerformance
+ * @param referencePerformance
+ * @param referenceBIPS
+ * @throws AssertionFailedError
+ */
+ public static void assertPerformance(double currentPerformance,
+ long referencePerformance, double referenceBIPS)
+ throws AssertionFailedError {
+ double ajustedPerf = adjustExpectedPerformance(referencePerformance,
+ referenceBIPS);
+ if (currentPerformance*SLACK_FACTOR < ajustedPerf) {
+ throw new AssertionFailedError(currentPerformance + " below expected "
+ + ajustedPerf + " (adjusted), " + referencePerformance + " (raw)");
+ }
+ }
+
+ private static double adjustExpectedPerformance(long referenceDuration,
+ double referenceBIPS) {
+ double currentBIPS = currentBIPS();
+ return referenceDuration * (currentBIPS/referenceBIPS);
+ }
+
+ private static double adjustExpectedDuration(long referenceDuration,
+ double referenceBIPS) {
+ double currentBIPS = currentBIPS();
+ return referenceDuration * (referenceBIPS / currentBIPS);
+ }
+}
Added: slf4j/trunk/slf4j-api/src/test/java/org/slf4j/helpers/MessageFormatterPerfTest.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/slf4j-api/src/test/java/org/slf4j/helpers/MessageFormatterPerfTest.java Mon Aug 18 17:51:13 2008
@@ -0,0 +1,59 @@
+package org.slf4j.helpers;
+
+import java.text.MessageFormat;
+
+import junit.framework.TestCase;
+
+public class MessageFormatterPerfTest extends TestCase {
+
+ Integer i1 = new Integer(1);
+ static long RUN_LENGTH = 100000;
+ static long REFERENCE_BIPS = 9629;
+
+ public MessageFormatterPerfTest(String name) {
+ super(name);
+ }
+
+ protected void setUp() throws Exception {
+ }
+
+ protected void tearDown() throws Exception {
+ }
+
+ public void XtestJDKFormatterPerf() {
+ jdkMessageFormatter(RUN_LENGTH);
+ double avg = jdkMessageFormatter(RUN_LENGTH);
+ System.out.println("jdk avg = "+avg+" nanos");
+ }
+
+ public void testSLF4JPerf() {
+ slf4jMessageFormatter(RUN_LENGTH);
+ double avgDuration = slf4jMessageFormatter(RUN_LENGTH);
+
+ long referencePerf = 1700;
+ BogoPerf.assertDuration(avgDuration, referencePerf, REFERENCE_BIPS);
+ }
+
+ public double slf4jMessageFormatter(long len) {
+ String s = "";
+ s += ""; // keep compiler happy
+ long start = System.nanoTime();
+ for (int i = 0; i < len; i++) {
+ s = MessageFormatter.format("This is some rather short message {} ", i1);
+ }
+ long end = System.nanoTime();
+ return (1.0*end - start) / len;
+ }
+ public double jdkMessageFormatter(long len) {
+ String s = "";
+ s += ""; // keep compiler happy
+ long start = System.nanoTime();
+ Object[] oa = new Object[] {i1};
+ for (int i = 0; i < len; i++) {
+ s = MessageFormat.format("This is some rather short message {0}", oa);
+ }
+ long end = System.nanoTime();
+ return (1.0*end - start) / len;
+ }
+
+}
More information about the slf4j-dev
mailing list