public static void main(String... ignored) throws InterruptedException {
    AffinityLock al = AffinityLock.acquireLock();

    // warmup.
    new MicroJitterSampler().sample(1000 * 1000 * 1000);

    MicroJitterSampler microJitterSampler = new MicroJitterSampler();
    while (!Thread.currentThread().isInterrupted()) {
      if (UTIL >= 100) {
        microJitterSampler.sample(30L * 1000 * 1000 * 1000);
      } else {
        long sampleLength = (long) ((1 / (1 - UTIL / 100) - 1) * 1000 * 1000);
        for (int i = 0; i < 30 * 1000; i += 2) {
          microJitterSampler.sample(sampleLength);
          //noinspection BusyWait
          Thread.sleep(1);
        }
      }

      microJitterSampler.print(System.out);
    }
  }
  // TODO test passes but is under development.
  public static void main(String... ignored) throws IOException {
    AffinityLock lock = AffinityLock.acquireCore();
    File file = File.createTempFile("testCHMLatency", "deleteme");
    //        File file = new File("testCHMLatency.deleteme");
    file.delete();
    ChronicleMap<LongValue, LongValue> countersMap =
        ChronicleMapBuilder.of(LongValue.class, LongValue.class)
            .entries(KEYS)
            .createPersistedTo(file);

    // add keys
    LongValue key = Values.newHeapInstance(LongValue.class);
    LongValue value = Values.newNativeReference(LongValue.class);
    for (long i = 0; i < KEYS; i++) {
      key.setValue(i);
      countersMap.acquireUsing(key, value);
      value.setValue(0);
    }
    System.out.println("Keys created");
    //        Monitor monitor = new Monitor();
    LongValue value2 = Values.newNativeReference(LongValue.class);
    for (int t = 0; t < 5; t++) {
      for (int rate :
          new int[] {
            2 * 1000 * 1000, 1000 * 1000, 500 * 1000 /*, 250 * 1000, 100 * 1000, 50 * 1000*/
          }) {
        Histogram times = new Histogram();
        int u = 0;
        long start = System.nanoTime();
        long delay = 1000 * 1000 * 1000L / rate;
        long next = start + delay;
        for (long j = 0; j < RUN_TIME * rate; j += KEYS) {
          int stride = Math.max(1, KEYS / (RUN_TIME * rate));
          // the timed part
          for (int i = 0; i < KEYS && u < RUN_TIME * rate; i += stride) {
            // busy wait for next time.
            while (System.nanoTime() < next - 12) ;
            //                        monitor.sample = System.nanoTime();
            long start0 = next;

            // start the update.
            key.setValue(i);
            LongValue using = countersMap.getUsing(key, value2);
            if (using == null) assertNotNull(using);
            value2.addAtomicValue(1);

            // calculate the time using the time it should have started, not when it was able.
            long elapse = System.nanoTime() - start0;
            times.sample(elapse);
            next += delay;
          }
          //                    monitor.sample = Long.MAX_VALUE;
        }
        System.out.printf("run %d %,9d : ", t, rate);
        times.printPercentiles(" micro-seconds.");
      }
      System.out.println();
    }
    //        monitor.running = false;
    countersMap.close();
    file.delete();
  }