// Tests that simple task scheduling and execution works.
  @Test
  public void schedule() throws Exception {
    final int THREAD_COUNT = 4, MEMORY_SIZE = 1024, TASK_COUNT = 100;
    final BoundedThreadPool pool = new BoundedThreadPool(THREAD_COUNT, MEMORY_SIZE);

    final LongAdder counter = new LongAdder();
    for (int i = 0; i < TASK_COUNT; i++) {
      Assert.assertTrue(pool.schedule(0, counter::increment));
    }
    pool.terminate(Terminate.AWAIT);
    Assert.assertEquals(TASK_COUNT, counter.intValue());
  }
Example #2
0
 @Test
 public void test() {
   Command<KVStore> initial = commands.remove(0);
   sm.applyFunction().apply(0L, initial);
   commands.forEach(
       c -> {
         final long index = la.longValue();
         sm.applyFunction().apply(index, c);
         la.increment();
       });
   KVStore kvsm = (KVStore) sm;
   kvsm.getKvMap().forEach((k, v) -> System.out.println(k + " --> " + v));
   assertTrue(kvsm.getKvMap().size() == 10);
 }
  /** Shrinks queue to maximum allowed size. */
  private void shrink() {
    long maxSize = this.maxSize;
    int maxBlocks = this.maxBlocks;

    int cnt = queue.sizex();

    for (int i = 0;
        i < cnt
            && (maxBlocks > 0 && queue.sizex() > maxBlocks
                || maxSize > 0 && curSize.longValue() > maxSize);
        i++) {
      GridCacheEntry<GridGgfsBlockKey, byte[]> entry = queue.poll();

      if (entry == null) break; // Queue is empty.

      byte[] val = entry.peek();

      if (val != null)
        changeSize(-val.length); // Change current size as we polled entry from the queue.

      if (!entry.evict()) {
        // Reorder entries which we failed to evict.
        entry.removeMeta(META_NODE);

        touch(entry);
      }
    }
  }
 @GenerateMicroBenchmark
 @Group("Adder")
 @GroupThreads(1)
 public long adReads() {
   BlackHole.consumeCPU(consumedCPU);
   return longAdder.sum();
 }
  @GenerateMicroBenchmark
  @Group("Adder")
  @GroupThreads(1)
  public void adWrites() {
    BlackHole.consumeCPU(consumedCPU);
    longAdder.add(1);

    BlackHole.consumeCPU(consumedCPU);
  }
Example #6
0
  public void waitForNextTick() throws InterruptedException {
    // no point waiting, just carry on
    if (tickPeriod <= MIN_PERIOD) {
      return;
    }

    synchronized (ticks) {
      ticks.wait();
    }
  }
  // Tests that scheduled timeouts are complied with.
  @Test
  public void timeout() throws Exception {
    final int THREAD_COUNT = 1, MEMORY_SIZE = 1024, TASK_COUNT = 10, TIMEOUT = 100;
    final BoundedThreadPool pool = new BoundedThreadPool(THREAD_COUNT, MEMORY_SIZE);

    final LongAdder counter = new LongAdder();
    for (int i = 0; i < TASK_COUNT; i++) {
      final boolean isScheduled =
          pool.schedule(
              0,
              TIMEOUT,
              () -> {
                counter.increment();
                try {
                  Thread.sleep(2 * TIMEOUT);
                } catch (InterruptedException e) {
                  throw new ProtocolException(e);
                }
              });
      Assert.assertTrue(isScheduled);
    }
    pool.terminate(Terminate.AWAIT);
    Assert.assertEquals(1, counter.intValue());
  }
Example #8
0
 synchronized void stop() {
   if (isRunning()) {
     ticker.cancel(true); // may interrupt if running
     ticker = null;
   }
   // let any threads waiting on the clock fall through
   synchronized (ticks) {
     // one should be sufficient but this is to be safe
     for (int i = 0; i < 5; i++) {
       ticks.notifyAll();
       try {
         Thread.sleep(10);
       } catch (InterruptedException ignored) {
       }
     }
   }
 }
Example #9
0
 public int getInventoryItemCount() {
   if (getSlottedObject("inventory") == null) return 0;
   LongAdder adder = new LongAdder();
   getSlottedObject("inventory").viewChildren(this, true, true, (obj) -> adder.increment());
   return adder.intValue();
 }
 /** @return the total processing time, in milliseconds */
 public long processingTime() {
   return MILLISECONDS.convert(processingTime.sum(), NANOSECONDS);
 }
 /**
  * Increments total processing time.
  *
  * @param time processing time to add, in nanoseconds
  */
 public void addProcessingTime(long time) {
   processingTime.add(time);
 }
Example #12
0
 public long count() {
   return count.sum();
 }
Example #13
0
 /**
  * Mark the occurrence of a given number of events.
  *
  * @param n the number of events
  */
 public void mark(long n) {
   count.add(n);
   m1Rate.update(n);
   m5Rate.update(n);
   m15Rate.update(n);
 }
 /** {@inheritDoc} */
 @Override
 public long getCurrentSize() {
   return curSize.longValue();
 }
 /**
  * Change current size.
  *
  * @param delta Delta in bytes.
  */
 private void changeSize(int delta) {
   if (delta != 0) curSize.add(delta);
 }
Example #16
0
 private void incrementTicks() {
   synchronized (ticks) {
     ticks.increment();
     ticks.notifyAll();
   }
 }
Example #17
0
 void resetTicks() {
   ticks.reset();
 }
Example #18
0
 public long getTicks() {
   return ticks.longValue();
 }