import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.InvalidKeyException;
import java.util.Base64;
import java.util.Base64.Encoder;
public class TestBST {
private static final String secret = “pKZFy7TpNKegCvxi8bxXftGRaJ8Y6KrGKpkxHVFM2uLBumzi2MVPW9K6m88jXPRq”;
private static final Encoder encoder = Base64.getEncoder().withoutPadding();
private static Mac mac = null;
private static int numberOfKeys = 100000000;
private static MessageDigest digest = null;
private static void initMac() {
try {
mac = Mac.getInstance(“HmacSHA256”);
mac.init(new SecretKeySpec(secret.getBytes(), “HmacSHA256”));
} catch (NoSuchAlgorithmException e) {
System.err.println(e);
System.exit(-1);
} catch (InvalidKeyException e) {
System.err.println(e);
System.exit(-1);
}
}
private static String getSHA256Encoding(String message) {
return encoder.encodeToString(mac.doFinal(message.getBytes()));
}
private static void initDigest() {
try {
digest = MessageDigest.getInstance(“MD5”);
} catch (NoSuchAlgorithmException e) {
System.err.println(e);
System.exit(-1);
}
}
private static String getMD5Encoding(String message) {
digest.update(message.getBytes());
byte messageDigest[] = digest.digest();
StringBuilder hexString = new StringBuilder();
for (byte b : messageDigest) {
hexString.append(Integer.toHexString(0xFF & b));
}
return hexString.toString();
}
private static BinarySearchTree
System.out.println(“building a tree!”);
BinarySearchTree
tree = new BinarySearchTree
long t0 = System.currentTimeMillis(), elapsed;
for (int i=0; i
long t0 = System.currentTimeMillis(), elapsed;
if (tree.contains(value)) {
System.out.println(value + ” was found!”);
} else {
System.out.println(value + ” was not found!”);
}
elapsed = System.currentTimeMillis() – t0;
System.out.println(elapsed + ” ms”);
}
public static void testAdd(BinarySearchTree
System.out.println(“adding ” + value);
long t0 = System.currentTimeMillis(), elapsed;
tree.add(value);
elapsed = System.currentTimeMillis() – t0;
System.out.println(elapsed + ” ms”);
}
public static void main(String[] args) {
// processing the command line arguments
if (args.length == 1) {
try {
numberOfKeys = Integer.parseInt(args[0]);
} catch (NumberFormatException e) {
; // using default value
}
}
initMac();
if (mac == null) {
System.err.println(“No MAC object!”);
return;
}
initDigest();
if (digest == null) {
System.err.println(“No MD5 object!”);
return;
}
BinarySearchTree
tree = buildBinarySearchTree();
String value = getSHA256Encoding(String.format(“%010d”, numberOfKeys++));
testContains(tree, value);
testAdd(tree, value);
testContains(tree, value);
}
}
// > nohup java -Xmx128g TestBST 1000000000 > TestBST-$$.out &