Author: j16sdiz
Date: 2008-04-12 01:49:33 +0000 (Sat, 12 Apr 2008)
New Revision: 19214
Added:
trunk/freenet/test/net/i2p/util/NativeBigIntegerTest.java
Modified:
trunk/freenet/src/net/i2p/util/NativeBigInteger.java
Log:
junit for NativeBigInteger
Modified: trunk/freenet/src/net/i2p/util/NativeBigInteger.java
===================================================================
--- trunk/freenet/src/net/i2p/util/NativeBigInteger.java 2008-04-11
23:30:11 UTC (rev 19213)
+++ trunk/freenet/src/net/i2p/util/NativeBigInteger.java 2008-04-12
01:49:33 UTC (rev 19214)
@@ -10,8 +10,6 @@
import java.math.BigInteger;
import java.util.Random;
-import java.security.SecureRandom;
-
import java.net.URL;
import java.io.FileOutputStream;
import java.io.InputStream;
@@ -267,126 +265,7 @@
public static boolean isNative() {
return _nativeOk;
}
-
/**
- * <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-12
01:49:33 UTC (rev 19214)
@@ -0,0 +1,114 @@
+package net.i2p.util;
+
+import java.math.BigInteger;
+import java.security.SecureRandom;
+
+import junit.framework.TestCase;
+
+public class NativeBigIntegerTest extends TestCase {
+ // Run with <code>ant -Dbenchmark=true</code> to do benchmark
+ private static final boolean BENCHMARK =
Boolean.getBoolean("benchmark");
+ private static int numRuns = BENCHMARK ? 200 : 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();
+
+ private SecureRandom rand;
+ private int runsProcessed;
+
+ private BigInteger jg;
+ private BigInteger jp;
+
+ private long totalTime = 0;
+ private long javaTime = 0;
+
+ protected void setUp() throws Exception {
+ if (!NativeBigInteger.isNative())
+ printError("can't load native code");
+
+ printInfo("DEBUG: Warming up the random number generator...");
+ rand = new SecureRandom();
+ rand.nextBoolean();
+ printInfo("DEBUG: Random number generator warmed up");
+
+ jg = new BigInteger(_sampleGenerator);
+ jp = new BigInteger(_samplePrime);
+
+ totalTime = javaTime = 0;
+ }
+
+ protected void tearDown() throws Exception {
+ printInfo("INFO: run time: " + totalTime + "ms (" + (totalTime
/ (runsProcessed + 1)) + "ms each)");
+ if (numRuns == runsProcessed)
+ printInfo("INFO: " + runsProcessed + " runs complete
without any errors");
+ else
+ printError("ERROR: " + runsProcessed + " runs until we
got an error");
+
+ printInfo("native run time: \t" + totalTime + "ms (" +
(totalTime / (runsProcessed + 1)) + "ms each)");
+ printInfo("java run time: \t" + javaTime + "ms (" + (javaTime
/ (runsProcessed + 1)) + "ms each)");
+ printInfo("native = " + ((totalTime * 100.0d) / (double)
javaTime) + "% of pure java time");
+ }
+
+ public void testModPow() {
+ 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);
+
+ assertEquals(jval, myValue);
+ }
+ }
+
+ public void testDoubleValue() {
+ BigInteger jg = new BigInteger(_sampleGenerator);
+
+ int MULTIPLICATOR = 50000; //Run the doubleValue() calls within
a loop since they are pretty fast..
+ 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);
+
+ assertEquals(jval, dNative, 0);
+ }
+ }
+
+ private static void printInfo(String info) {
+ if (BENCHMARK)
+ System.out.println(info);
+ }
+
+ private static void printError(String info) {
+ if (BENCHMARK)
+ System.err.println(info);
+ }
+}