Create ByteUtilTest and move testIncrementPerformance
This commit is contained in:
parent
279e380727
commit
33f887b76d
|
@ -47,19 +47,6 @@ public class ByteUtil {
|
|||
else return Hex.toHexString(data);
|
||||
}
|
||||
|
||||
// The packet size should be 4 byte long
|
||||
public static byte[] calcPacketSize(byte[] packet){
|
||||
|
||||
byte[] size = new byte[4];
|
||||
|
||||
size[3] = (byte)(packet.length >> 0 & 0xFF);
|
||||
size[2] = (byte)(packet.length >> 8 & 0xFF);
|
||||
size[1] = (byte)(packet.length >> 16 & 0xFF);
|
||||
size[0] = (byte)(packet.length >> 24 & 0xFF);
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate packet length
|
||||
* @param msg
|
||||
|
@ -118,19 +105,15 @@ public class ByteUtil {
|
|||
byte[] data;
|
||||
|
||||
// check if the string is numeric
|
||||
if (arg.toString().trim().matches("-?\\d+(\\.\\d+)?")) {
|
||||
if (arg.toString().trim().matches("-?\\d+(\\.\\d+)?"))
|
||||
data = new BigInteger(arg.toString().trim()).toByteArray();
|
||||
|
||||
// check if it's hex number
|
||||
} else if (arg.toString().trim().matches("0[xX][0-9a-fA-F]+")){
|
||||
// check if it's hex number
|
||||
else if (arg.toString().trim().matches("0[xX][0-9a-fA-F]+"))
|
||||
data = new BigInteger(arg.toString().trim().substring(2), 16).toByteArray();
|
||||
|
||||
} else {
|
||||
else
|
||||
data = arg.toString().trim().getBytes();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
if (data.length > 32)
|
||||
throw new RuntimeException("values can't be more than 32 byte");
|
||||
|
||||
|
@ -160,36 +143,29 @@ public class ByteUtil {
|
|||
return baos.toByteArray();
|
||||
}
|
||||
|
||||
public static byte[] stripLeadingZeroes(byte[] data) {
|
||||
|
||||
if (data == null)
|
||||
return null;
|
||||
|
||||
int firstNonZero = 0;
|
||||
int i = 0;
|
||||
for (i = 0; i < data.length; ++i) {
|
||||
if (data[i] != 0) {
|
||||
firstNonZero = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (i == data.length)
|
||||
return new byte[1];
|
||||
if (firstNonZero == 0)
|
||||
return data;
|
||||
|
||||
byte[] result = new byte[data.length - firstNonZero];
|
||||
System.arraycopy(data, firstNonZero, result, 0, data.length - firstNonZero);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static byte[] stripLeadingZeroes(byte[] data){
|
||||
|
||||
if (data == null) return null;
|
||||
|
||||
int firstNonZero = 0;
|
||||
int i = 0;
|
||||
for (i = 0; i < data.length; ++i)
|
||||
if (data[i] != 0){
|
||||
firstNonZero = i;
|
||||
break;
|
||||
}
|
||||
|
||||
if (i == data.length) return new byte[1];
|
||||
if (firstNonZero == 0) return data;
|
||||
|
||||
byte[] result = new byte[data.length - firstNonZero];
|
||||
System.arraycopy(data, firstNonZero, result, 0, data.length - firstNonZero);
|
||||
|
||||
return result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* increment byte array as a number until max is reached
|
||||
|
|
|
@ -3,13 +3,7 @@ package org.ethereum.core;
|
|||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import org.ethereum.util.ByteUtil;
|
||||
import org.ethereum.util.FastByteComparisons;
|
||||
import org.junit.Test;
|
||||
import org.spongycastle.util.BigIntegers;
|
||||
import org.spongycastle.util.encoders.Hex;
|
||||
|
||||
public class MinerTest {
|
||||
|
@ -68,40 +62,4 @@ public class MinerTest {
|
|||
newBlock.setStateRoot(Hex.decode("50188ab86bdf164ac90eb2835a04a8930aae5393c3a2ef1166fb95028f9456b8"));
|
||||
return newBlock;
|
||||
}
|
||||
|
||||
/**
|
||||
* This test shows the difference between iterating over,
|
||||
* and comparing byte[] vs BigInteger value.
|
||||
*
|
||||
* Results indicate that the former has ~15x better performance.
|
||||
* Therefore this is used in the Miner.mine() method.
|
||||
*/
|
||||
@Test
|
||||
public void testIncrementPerformance() {
|
||||
boolean testEnabled = false;
|
||||
|
||||
if(testEnabled) {
|
||||
byte[] counter1 = new byte[4];
|
||||
byte[] max = ByteBuffer.allocate(4).putInt(Integer.MAX_VALUE).array();
|
||||
long start1 = System.currentTimeMillis();
|
||||
while(ByteUtil.increment(counter1)) {
|
||||
if(FastByteComparisons.compareTo(counter1, 0, 4, max, 0, 4) == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
System.out.println(System.currentTimeMillis() - start1 + "ms to reach: " + Hex.toHexString(counter1));
|
||||
|
||||
BigInteger counter2 = BigInteger.ZERO;
|
||||
long start2 = System.currentTimeMillis();
|
||||
while(true) {
|
||||
if(counter2.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) == 0) {
|
||||
break;
|
||||
}
|
||||
counter2 = counter2.add(BigInteger.ONE);
|
||||
|
||||
}
|
||||
System.out.println(System.currentTimeMillis() - start2 + "ms to reach: " + Hex.toHexString(BigIntegers.asUnsignedByteArray(4, counter2)));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,100 @@
|
|||
package org.ethereum.util;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.spongycastle.util.BigIntegers;
|
||||
import org.spongycastle.util.encoders.Hex;
|
||||
|
||||
public class ByteUtilTest {
|
||||
|
||||
@Test
|
||||
public void testAppendByte() {
|
||||
byte[] bytes = "tes".getBytes();
|
||||
byte b = 0x74;
|
||||
assertArrayEquals("test".getBytes(), ByteUtil.appendByte(bytes, b));
|
||||
}
|
||||
|
||||
// @Test
|
||||
// public void testBigIntegerToBytes() {
|
||||
// fail("Not yet implemented");
|
||||
// }
|
||||
|
||||
@Test
|
||||
public void testToHexString() {
|
||||
assertEquals("null", ByteUtil.toHexString(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCalcPacketLength() {
|
||||
byte[] test = new byte[] { 0x0f, 0x10, 0x43 };
|
||||
byte[] expected = new byte[] { 0x00, 0x00, 0x00, 0x03 };
|
||||
assertArrayEquals(expected, ByteUtil.calcPacketLength(test));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testByteArrayToInt() {
|
||||
assertEquals(0, ByteUtil.byteArrayToInt(null));
|
||||
assertEquals(0, ByteUtil.byteArrayToInt(new byte[0]));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNumBytes() {
|
||||
String test1 = "0";
|
||||
String test2 = "1";
|
||||
String test3 = "1000000000"; //3B9ACA00
|
||||
int expected1 = 1;
|
||||
int expected2 = 1;
|
||||
int expected3 = 4;
|
||||
assertEquals(expected1, ByteUtil.numBytes(test1));
|
||||
assertEquals(expected2, ByteUtil.numBytes(test2));
|
||||
assertEquals(expected3, ByteUtil.numBytes(test3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStripLeadingZeroes() {
|
||||
byte[] test1 = new byte[] { 0x00, 0x01 };
|
||||
byte[] test2 = new byte[] { 0x00, 0x00, 0x01 };
|
||||
byte[] expected = new byte[] { 0x01 };
|
||||
assertArrayEquals(expected, ByteUtil.stripLeadingZeroes(test1));
|
||||
assertArrayEquals(expected, ByteUtil.stripLeadingZeroes(test2));
|
||||
}
|
||||
|
||||
/**
|
||||
* This test shows the difference between iterating over,
|
||||
* and comparing byte[] vs BigInteger value.
|
||||
*
|
||||
* Results indicate that the former has ~15x better performance.
|
||||
* Therefore this is used in the Miner.mine() method.
|
||||
*/
|
||||
@Test
|
||||
public void testIncrementPerformance() {
|
||||
boolean testEnabled = false;
|
||||
|
||||
if(testEnabled) {
|
||||
byte[] counter1 = new byte[4];
|
||||
byte[] max = ByteBuffer.allocate(4).putInt(Integer.MAX_VALUE).array();
|
||||
long start1 = System.currentTimeMillis();
|
||||
while(ByteUtil.increment(counter1)) {
|
||||
if(FastByteComparisons.compareTo(counter1, 0, 4, max, 0, 4) == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
System.out.println(System.currentTimeMillis() - start1 + "ms to reach: " + Hex.toHexString(counter1));
|
||||
|
||||
BigInteger counter2 = BigInteger.ZERO;
|
||||
long start2 = System.currentTimeMillis();
|
||||
while(true) {
|
||||
if(counter2.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) == 0) {
|
||||
break;
|
||||
}
|
||||
counter2 = counter2.add(BigInteger.ONE);
|
||||
}
|
||||
System.out.println(System.currentTimeMillis() - start2 + "ms to reach: " + Hex.toHexString(BigIntegers.asUnsignedByteArray(4, counter2)));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue