import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; 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 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 BinarySearchTree buildBinarySearchTree() { System.out.println("building a tree!"); BinarySearchTree tree; tree = new BinarySearchTree(); long t0 = System.currentTimeMillis(), elapsed; for (int i=0; i tree, String value) { 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 tree, String value) { 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; } BinarySearchTree tree; tree = buildBinarySearchTree(); String message = String.format("%010d", numberOfKeys); byte[] hash = mac.doFinal(message.getBytes()); String value = encoder.encodeToString(hash); testContains(tree, value); testAdd(tree, value); testContains(tree, value); } } // > nohup java -Xmx128g TestBST 1000000000 > TestBST-$$.out &