package edu.rice.hj.example.comp322.labs.lab10; public class ListDriver { static final int DEFAULT_THREADS = 8; static final int DEFAULT_MILLIS = 10000; // run for 10 seconds static final int DEFAULT_READ_PCT = 98; static final int DEFAULT_ADD_PCT = 1; static final int DEFAULT_DEL_PCT = 1; static final int DEFAULT_LOAD = 0; public static void main(final String[] args) { int numThreads = DEFAULT_THREADS; int numMillis = DEFAULT_MILLIS; int readPct = DEFAULT_READ_PCT; int addPct = DEFAULT_ADD_PCT; int delPct = DEFAULT_DEL_PCT; int load = DEFAULT_LOAD; String sClassName = ""; Class benchmarkClass = ListTest.class; // Parse and check the args int argc = 0; try { while (argc < args.length) { final String option = args[argc++]; if (option.equals("-t")) { numThreads = Integer.parseInt(args[argc]); } else if (option.equals("-n")) { numMillis = Integer.parseInt(args[argc]); } else if (option.equals("-b")) { benchmarkClass = Class.forName(getPackagePrefixName() + args[argc]); } else if (option.equals("-s")) { sClassName = getPackagePrefixName() + args[argc]; } else if (option.equals("-r")) { readPct = Integer.parseInt(args[argc]); } else if (option.equals("-a")) { addPct = Integer.parseInt(args[argc]); } else if (option.equals("-d")) { delPct = Integer.parseInt(args[argc]); } else if (option.equals("-l")) { load = Integer.parseInt(args[argc]); } else { reportUsageErrorAndDie(); } argc++; } } catch (final ClassNotFoundException e) { System.out.println("Cannot find class " + args[argc]); System.exit(0); } catch (final NumberFormatException e) { System.out.println("Expected a number: " + args[argc]); System.exit(0); } catch (final Exception e) { reportUsageErrorAndDie(); } RWMix mix = null; SBenchmark bm = null; try { bm = (SBenchmark) benchmarkClass.newInstance(); final Class mixClass = Class.forName(getPackagePrefixName() + "RWMix"); mix = (RWMix) mixClass.newInstance(); mix.set(readPct, addPct, delPct); } catch (final ClassCastException e) { System.out.println("Class " + benchmarkClass + " doesn't implement SBenchmark."); } catch (final Exception e) { e.printStackTrace(System.out); System.exit(0); } if (benchmarkClass == null || !mix.validate()) { reportUsageErrorAndDie(); } final ListCounter[] total = new ListCounter[numThreads]; final Thread[] thread = new Thread[numThreads]; final ListCounter loadCtr = new ListCounter(); final Thread[] loadThread = new Thread[load]; System.out.print("ListDriver -t " + numThreads); System.out.print(" -n " + numMillis); System.out.print(" -s " + sClassName); System.out.print(" -r " + readPct); System.out.print(" -a " + addPct); System.out.print(" -d " + delPct); System.out.println(" -l " + load); long startTime = 0; try { // Create test threads for (int i = 0; i < numThreads; i++) { total[i] = new ListCounter(); thread[i] = bm.createTestThread(sClassName, total[i], mix); } // Create load threads for (int i = 0; i < load; i++) { loadThread[i] = bm.createLoadThread(loadCtr); } // Initialize test environment bm.initializeTestEnvironment(128); // Run the test startTime = System.currentTimeMillis(); for (int i = 0; i < numThreads; i++) { thread[i].start(); } for (int i = 0; i < load; i++) { loadThread[i].start(); } // Wait for test to finish Thread.sleep(numMillis); // Stop all threads for (int i = 0; i < numThreads; i++) { total[i].stopNow(); } loadCtr.stopNow(); for (int i = 0; i < numThreads; i++) { thread[i].interrupt(); thread[i].join(10000); } for (int i = 0; i < load; i++) { loadThread[i].interrupt(); loadThread[i].join(10000); } } catch (final Exception e) { e.printStackTrace(System.out); } final long stopTime = System.currentTimeMillis(); final double elapsed = (double) (stopTime - startTime) / 1000.0; int reads = 0; int adds = 0; int deletes = 0; int successes = 0; int failures = 0; for (int i = 0; i < numThreads; i++) { reads += total[i].getReads(); adds += total[i].getAdds(); deletes += total[i].getDeletes(); successes += total[i].getSuccesses(); failures += total[i].getFailures(); } System.out.println("Operations per seconds: " + (reads + adds + deletes) / numMillis * 1000); System.out.println("Reads: " + reads); System.out.println("Adds: " + adds); System.out.println("Deletes: " + deletes); // System.out.println("Successes: " + successes); //System.out.println("Validation Failures: " + failures); //System.out.println("Elapsed time: " + elapsed + " seconds."); System.out.println("----------------------------------------"); } private static String getPackagePrefixName() { final String packageName = ListDriver.class.getPackage().getName(); if (packageName.length() == 0) { return ""; } else { return packageName + "."; } } private static void reportUsageErrorAndDie() { System.out.print("usage: ListDriver [-t #threads] [-n #test-time-in-ms]"); System.out.println(" [-s set class] [-r read%] [-a add%] [-d del%]"); System.out.println(" [-l #num load threads]"); System.exit(0); } public interface SBenchmark { public Thread createTestThread( String sClassName, ListCounter ctr, RWMix mix); public Thread createLoadThread(ListCounter ctr); public void initializeTestEnvironment(int n); } } /* EOF */