Author: j16sdiz
Date: 2008-04-11 11:59:31 +0000 (Fri, 11 Apr 2008)
New Revision: 19176
Added:
trunk/freenet/test/net/
trunk/freenet/test/net/i2p/
trunk/freenet/test/net/i2p/util/
trunk/freenet/test/net/i2p/util/NativeBigIntegerTest.java
Modified:
trunk/freenet/src/net/i2p/util/NativeBigInteger.java
Log:
move unit test to where it belongs (NativeBigInteger)
Modified: trunk/freenet/src/net/i2p/util/NativeBigInteger.java
===================================================================
--- trunk/freenet/src/net/i2p/util/NativeBigInteger.java 2008-04-11
11:39:43 UTC (rev 19175)
+++ trunk/freenet/src/net/i2p/util/NativeBigInteger.java 2008-04-11
11:59:31 UTC (rev 19176)
@@ -74,16 +74,6 @@
* implementation, as above. This way the user would download the correct
jbigi.jar
* (and not all of the libraries for platforms/OSes they don't need) and would
specify
* -Djbigi.impl=native.</p>
- *
- * <p>Running this class by itself does a basic unit test and benchmarks the
- * NativeBigInteger.modPow/doubleValue vs. the BigInteger.modPow/doubleValue
by running a 2Kbit op 100
- * times. At the end of each test, if the native implementation is loaded
this will output
- * something like:</p>
- * <pre>
- * native run time: 6090ms (60ms each)
- * java run time: 68067ms (673ms each)
- * native = 8.947066860593239% of pure java time
- * </pre>
*
* <p>If the native implementation is not loaded, it will start by saying:</p>
* <pre>
@@ -269,124 +259,6 @@
}
/**
- * <p>Compare the BigInteger.modPow/doubleValue vs the
NativeBigInteger.modPow/doubleValue of some
- * really big (2Kbit) numbers 100 different times and benchmark the
- * performance (or shit a brick if they don't match). </p>
- *
- */
- public static void main(String args[]) {
- runModPowTest(100);
- runDoubleValueTest(100);
- }
-
- /* the sample numbers are elG generator/prime so we can test with
reasonable numbers */
- private final static byte[] _sampleGenerator = new
BigInteger("2").toByteArray();
- private final static byte[] _samplePrime = new
BigInteger("FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" +
"29024E088A67CC74020BBEA63B139B22514A08798E3404DD" +
"EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" +
"E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" +
"EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" +
"C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" +
"83655D23DCA3AD961C62F356208552BB9ED529077096966D" +
"670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" +
"E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" +
"DE2BCBF6955817183995497CEA956AE515D2261898FA0510" +
"15728E5A8AACAA68FFFFFFFFFFFFFFFF", 16).toByteArray();
-
- private static void runModPowTest(int numRuns) {
- System.out.println("DEBUG: Warming up the random number
generator...");
- SecureRandom rand = new SecureRandom();
- rand.nextBoolean();
- System.out.println("DEBUG: Random number generator warmed up");
-
- BigInteger jg = new BigInteger(_sampleGenerator);
- BigInteger jp = new BigInteger(_samplePrime);
-
- long totalTime = 0;
- long javaTime = 0;
-
- int runsProcessed = 0;
- for(runsProcessed = 0; runsProcessed < numRuns;
runsProcessed++) {
- BigInteger bi = new BigInteger(2048, rand);
- NativeBigInteger g = new
NativeBigInteger(_sampleGenerator);
- NativeBigInteger p = new NativeBigInteger(_samplePrime);
- NativeBigInteger k = new NativeBigInteger(1,
bi.toByteArray());
- long beforeModPow = System.currentTimeMillis();
- BigInteger myValue = g.modPow(k, p);
- long afterModPow = System.currentTimeMillis();
- BigInteger jval = jg.modPow(bi, jp);
- long afterJavaModPow = System.currentTimeMillis();
-
- totalTime += (afterModPow - beforeModPow);
- javaTime += (afterJavaModPow - afterModPow);
- if(!myValue.equals(jval)) {
- System.err.println("ERROR: [" + runsProcessed +
"]\tnative modPow != java modPow");
- System.err.println("ERROR: native modPow value:
" + myValue.toString());
- System.err.println("ERROR: java modPow value: "
+ jval.toString());
- System.err.println("ERROR: run time: " +
totalTime + "ms (" + (totalTime / (runsProcessed + 1)) + "ms each)");
- break;
- } else
- System.out.println("DEBUG: current run time: "
+ (afterModPow - beforeModPow) + "ms (total: " + totalTime + "ms, " +
(totalTime / (runsProcessed + 1)) + "ms each)");
- }
- System.out.println("INFO: run time: " + totalTime + "ms (" +
(totalTime / (runsProcessed + 1)) + "ms each)");
- if(numRuns == runsProcessed)
- System.out.println("INFO: " + runsProcessed + " runs
complete without any errors");
- else
- System.out.println("ERROR: " + runsProcessed + " runs
until we got an error");
-
- if(_nativeOk) {
- System.out.println("native run time: \t" + totalTime +
"ms (" + (totalTime / (runsProcessed + 1)) + "ms each)");
- System.out.println("java run time: \t" + javaTime +
"ms (" + (javaTime / (runsProcessed + 1)) + "ms each)");
- System.out.println("native = " + ((totalTime * 100.0d)
/ (double) javaTime) + "% of pure java time");
- } else {
- System.out.println("java run time: \t" + javaTime + "ms
(" + (javaTime / (runsProcessed + 1)) + "ms each)");
- System.out.println("However, we couldn't load the
native library, so this doesn't test much");
- }
- }
-
- private static void runDoubleValueTest(int numRuns) {
- System.out.println("DEBUG: Warming up the random number
generator...");
- SecureRandom rand = new SecureRandom();
- rand.nextBoolean();
- System.out.println("DEBUG: Random number generator warmed up");
-
- BigInteger jg = new BigInteger(_sampleGenerator);
-
- long totalTime = 0;
- long javaTime = 0;
-
- int MULTIPLICATOR = 50000; //Run the doubleValue() calls within
a loop since they are pretty fast..
- int runsProcessed = 0;
- for(runsProcessed = 0; runsProcessed < numRuns;
runsProcessed++) {
- NativeBigInteger g = new
NativeBigInteger(_sampleGenerator);
- long beforeDoubleValue = System.currentTimeMillis();
- double dNative = 0;
- for(int mult = 0; mult < MULTIPLICATOR; mult++)
- dNative = g.doubleValue();
- long afterDoubleValue = System.currentTimeMillis();
- double jval = 0;
- for(int mult = 0; mult < MULTIPLICATOR; mult++)
- jval = jg.doubleValue();
- long afterJavaDoubleValue = System.currentTimeMillis();
-
- totalTime += (afterDoubleValue - beforeDoubleValue);
- javaTime += (afterJavaDoubleValue - afterDoubleValue);
- if(dNative != jval) {
- System.err.println("ERROR: [" + runsProcessed +
"]\tnative double != java double");
- System.err.println("ERROR: native double value:
" + dNative);
- System.err.println("ERROR: java double value: "
+ jval);
- System.err.println("ERROR: run time: " +
totalTime + "ms (" + (totalTime / (runsProcessed + 1)) + "ms each)");
- break;
- } else
- System.out.println("DEBUG: current run time: "
+ (afterDoubleValue - beforeDoubleValue) + "ms (total: " + totalTime + "ms, " +
(totalTime / (runsProcessed + 1)) + "ms each)");
- }
- System.out.println("INFO: run time: " + totalTime + "ms (" +
(totalTime / (runsProcessed + 1)) + "ms each)");
- if(numRuns == runsProcessed)
- System.out.println("INFO: " + runsProcessed + " runs
complete without any errors");
- else
- System.out.println("ERROR: " + runsProcessed + " runs
until we got an error");
-
- if(_nativeOk) {
- System.out.println("native run time: \t" + totalTime +
"ms (" + (totalTime / (runsProcessed + 1)) + "ms each)");
- System.out.println("java run time: \t" + javaTime +
"ms (" + (javaTime / (runsProcessed + 1)) + "ms each)");
- System.out.println("native = " + ((totalTime * 100.0d)
/ (double) javaTime) + "% of pure java time");
- } else {
- System.out.println("java run time: \t" + javaTime + "ms
(" + (javaTime / (runsProcessed + 1)) + "ms each)");
- System.out.println("However, we couldn't load the
native library, so this doesn't test much");
- }
- }
-
- /**
* <p>Do whatever we can to load up the native library backing this
BigInteger's native methods.
* If it can find a custom built jbigi.dll / libjbigi.so, it'll use
that. Otherwise
* it'll try to look in the classpath for the correct library (see
loadFromResource).
Added: trunk/freenet/test/net/i2p/util/NativeBigIntegerTest.java
===================================================================
--- trunk/freenet/test/net/i2p/util/NativeBigIntegerTest.java
(rev 0)
+++ trunk/freenet/test/net/i2p/util/NativeBigIntegerTest.java 2008-04-11
11:59:31 UTC (rev 19176)
@@ -0,0 +1,60 @@
+package net.i2p.util;
+
+import java.math.BigInteger;
+import java.security.SecureRandom;
+
+import net.i2p.util.NativeBigInteger;
+
+import junit.framework.TestCase;
+
+public class NativeBigIntegerTest extends TestCase {
+ private final static int NUM_TEST = 5;
+
+ /* the sample numbers are elG generator/prime so we can test with
reasonable numbers */
+ private final static byte[] _sampleGenerator = new
BigInteger("2").toByteArray();
+ private final static byte[] _samplePrime = new
BigInteger("FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" +
"29024E088A67CC74020BBEA63B139B22514A08798E3404DD" +
"EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" +
"E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" +
"EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" +
"C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" +
"83655D23DCA3AD961C62F356208552BB9ED529077096966D" +
"670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" +
"E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" +
"DE2BCBF6955817183995497CEA956AE515D2261898FA0510" +
"15728E5A8AACAA68FFFFFFFFFFFFFFFF", 16).toByteArray();
+
+ public void testDoubleValue() {
+ SecureRandom rand = new SecureRandom();
+ rand.nextBoolean();
+
+ BigInteger jg = new BigInteger(_sampleGenerator);
+
+ int MULTIPLICATOR = 50000; //Run the doubleValue() calls within
a loop since they are pretty fast..
+ int runsProcessed = 0;
+ for (runsProcessed = 0; runsProcessed < NUM_TEST;
runsProcessed++) {
+ NativeBigInteger g = new
NativeBigInteger(_sampleGenerator);
+
+ double dNative = 0;
+ for (int mult = 0; mult < MULTIPLICATOR; mult++)
+ dNative = g.doubleValue();
+ double jval = 0;
+ for (int mult = 0; mult < MULTIPLICATOR; mult++)
+ jval = jg.doubleValue();
+
+ assertEquals(jval, dNative, Double.MIN_VALUE);
+ }
+ }
+
+ public void testModPow() {
+ // Warming up the random number generator...
+ SecureRandom rand = new SecureRandom();
+ rand.nextBoolean();
+
+ BigInteger jg = new BigInteger(_sampleGenerator);
+ BigInteger jp = new BigInteger(_samplePrime);
+
+ int runsProcessed = 0;
+ for (runsProcessed = 0; runsProcessed < NUM_TEST;
runsProcessed++) {
+ BigInteger bi = new BigInteger(2048, rand);
+ NativeBigInteger g = new
NativeBigInteger(_sampleGenerator);
+ NativeBigInteger p = new NativeBigInteger(_samplePrime);
+ NativeBigInteger k = new NativeBigInteger(1,
bi.toByteArray());
+
+ BigInteger myValue = g.modPow(k, p);
+ BigInteger jval = jg.modPow(bi, jp);
+
+ assertEquals(jval, myValue);
+ }
+ }
+}