Exemplo n.º 1
0
  static void tests(Class mapClass, int numTests, int sizeIndex) {
    try {
      String[] key = readWords(sizeIndex);
      int size = key.length;

      System.out.print("n = " + LoopHelpers.rightJustify(size) + " : ");
      long least = Long.MAX_VALUE;

      for (int i = 0; i < numTests; ++i) {
        Map<String, String> m = newMap(mapClass);
        long t = doTest(i, mapClass.getName(), m, key);
        if (t < least) least = t;
        m.clear();
        m = null;
      }

      long nano = Math.round(1000000.0 * (least) / NOPS);
      System.out.println(LoopHelpers.rightJustify(nano) + " ns per op");
    } catch (IOException ignore) {
      return; // skip test if can't read file
    }
  }
  static void oneTest(int iters) throws Exception {
    long startTime = System.nanoTime();
    new Thread(new Producer(ecs, iters)).start();

    int r = 0;
    for (int i = 0; i < iters; ++i) r += ecs.take().get().intValue();

    long elapsed = System.nanoTime() - startTime;
    long tpi = elapsed / iters;

    if (print) System.out.println("\t: " + LoopHelpers.rightJustify(tpi) + " ns per task");

    if (r == 0) // avoid overoptimization
    System.out.println("useless result: " + r);
  }
  static void oneRun(BlockingQueue<Runnable> q, int nThreads, int iters, boolean print)
      throws Exception {

    ThreadPoolExecutor pool =
        new ThreadPoolExecutor(nThreads + 1, Integer.MAX_VALUE, 1L, TimeUnit.SECONDS, q);

    CountDownLatch done = new CountDownLatch(iters);
    remaining.set(nThreads - 1);
    pool.prestartAllCoreThreads();
    Task t = new Task(pool, done);
    long start = System.nanoTime();
    pool.execute(t);
    done.await();
    long time = System.nanoTime() - start;
    if (print) System.out.println("\t: " + LoopHelpers.rightJustify(time / iters) + " ns per task");
    q.clear();
    Thread.sleep(100);
    pool.shutdown();
    Thread.sleep(100);
    pool.shutdownNow();
  }