@Test
  /**
   * PS: Test will fail if for some reason we cannot do 50 ops/sec against a hash map. So yeah,
   * pretty unlikely.
   */
  public void testQuotaPctUsageCalculation() throws Exception {
    File tempDir = TestUtils.createTempDir();

    FileBackedCachingStorageEngine quotaStore =
        new FileBackedCachingStorageEngine("quota-usage-test-store", tempDir.getAbsolutePath());
    InMemoryStorageEngine<ByteArray, byte[], byte[]> inMemoryEngine =
        new InMemoryStorageEngine<ByteArray, byte[], byte[]>("inMemoryBackingStore");
    QuotaLimitStats quotaStats = new QuotaLimitStats(null, 1000);
    StatTrackingStore statTrackingStore = new StatTrackingStore(inMemoryEngine, null);

    QuotaLimitingStore quotaLimitingStore =
        new QuotaLimitingStore(
            statTrackingStore, statTrackingStore.getStats(), quotaStats, quotaStore);

    int targetRate = 50;
    // provide a quota of 100 gets/sec
    quotaStore.put(
        new ByteArray(
            QuotaUtils.makeQuotaKey(statTrackingStore.getName(), QuotaType.GET_THROUGHPUT)
                .getBytes()),
        new Versioned<byte[]>("100.0".getBytes()),
        null);

    long testIntervalMs = 5000;
    long timeToSleepMs = 1000 / targetRate;
    long startMs = System.currentTimeMillis();
    ByteArray key = new ByteArray("some key".getBytes());
    while ((System.currentTimeMillis() - startMs) <= testIntervalMs) {
      quotaLimitingStore.get(key, null);
      Thread.sleep(timeToSleepMs);
    }

    assertEquals("No get operations should be throttled", 0, quotaStats.getRateLimitedGets());
    assertEquals("Put usage should be 0", 0, quotaStats.getQuotaPctUsedPut());
    assertEquals("delete usage should be 0", 0, quotaStats.getQuotaPctUsedDelete());
    assertEquals("getall usage should be 0", 0, quotaStats.getQuotaPctUsedGetAll());

    assertEquals(
        "Computed usage pct must be close to actual observed qps",
        statTrackingStore.getStats().getThroughput(Tracked.GET),
        quotaStats.getQuotaPctUsedGet(),
        1.0);
  }
  protected void outputJSON(HttpServletResponse response) {
    StringBuilder sb = new StringBuilder("{\n");

    sb.append("  \"servertime\": \"");
    sb.append(new Date());
    sb.append("\",");

    sb.append("\n  \"server\": \"");
    sb.append(myMachine);
    sb.append("\",");

    sb.append("\n  \"node\": \"");
    sb.append(server.getMetadataStore().getNodeId());
    sb.append("\",");

    sb.append("\n  \"uptime\": \"");
    sb.append(abstractSocketService.getStatusManager().getFormattedUptime());
    sb.append("\",");

    sb.append("\n  \"num_workers\": ");
    sb.append(abstractSocketService.getStatusManager().getActiveWorkersCount());
    sb.append(",");

    sb.append("\n  \"pool_size\": ");
    sb.append(abstractSocketService.getStatusManager().getWorkerPoolSize());
    sb.append(",");

    sb.append("\n  \"stores\": {");

    int i = 0;
    for (Store<ByteArray, byte[], byte[]> store : server.getStoreRepository().getAllLocalStores()) {

      if (i++ > 0) {
        sb.append(",");
      }
      sb.append("\n    \"");
      sb.append(store.getName());
      sb.append("\" : {\n");

      if (store instanceof StatTrackingStore) {
        StatTrackingStore statStore = (StatTrackingStore) store;
        Map<Tracked, RequestCounter> stats = statStore.getStats().getCounters();
        for (Tracked t : Tracked.values()) {
          if (t == Tracked.EXCEPTION) {
            continue;
          }
          sb.append(fillCommonStats(stats, t));
        }
        sb.append(",\n        \"num_exceptions\": ");
        sb.append(statStore.getStats().getCount(Tracked.EXCEPTION));
        sb.append("\n");
        sb.append("    }");
      }

      if (store instanceof QuotaLimitingStore) {
        QuotaLimitingStore quotaStore = (QuotaLimitingStore) store;
        Map<Tracked, RequestCounter> stats = quotaStore.getStats().getCounters();
        for (Tracked t : Tracked.values()) {
          if (t == Tracked.EXCEPTION) {
            continue;
          }
          sb.append(fillCommonStats(stats, t));
        }
        sb.append(",\n        \"num_exceptions\": ");
        sb.append(quotaStore.getStats().getCount(Tracked.EXCEPTION));
        sb.append("\n");
        sb.append("    }");
      }
    }

    sb.append("\n  }\n");
    sb.append("}\n");

    try {
      response.setContentType("text/plain");
      OutputStreamWriter writer = new OutputStreamWriter(response.getOutputStream());
      writer.write(sb.toString());
      writer.flush();
    } catch (Exception e) {
      throw new VoldemortException(e);
    }
  }