Ejemplo n.º 1
0
  // test ResourceUsageMetric's deepCompare() method
  private static void testResourceUsageMetricViaDeepCompare(
      ResourceUsageMetrics metrics,
      long cpuUsage,
      long vMemUsage,
      long pMemUsage,
      long heapUsage,
      boolean shouldPass) {
    ResourceUsageMetrics testMetrics = new ResourceUsageMetrics();
    testMetrics.setCumulativeCpuUsage(cpuUsage);
    testMetrics.setPhysicalMemoryUsage(pMemUsage);
    testMetrics.setVirtualMemoryUsage(vMemUsage);
    testMetrics.setHeapUsage(heapUsage);

    Boolean passed = null;
    try {
      metrics.deepCompare(testMetrics, new TreePath(null, "<root>"));
      passed = true;
    } catch (DeepInequalityException die) {
      passed = false;
    }

    assertEquals("ResourceUsageMetrics deepCompare() failed!", shouldPass, passed);
  }
Ejemplo n.º 2
0
  /** Test {@link ResourceUsageMetrics}. */
  @Test
  public void testResourceUsageMetrics() throws Exception {
    final long cpuUsage = 100;
    final long pMemUsage = 200;
    final long vMemUsage = 300;
    final long heapUsage = 400;

    // test ResourceUsageMetrics's setters
    ResourceUsageMetrics metrics = new ResourceUsageMetrics();
    metrics.setCumulativeCpuUsage(cpuUsage);
    metrics.setPhysicalMemoryUsage(pMemUsage);
    metrics.setVirtualMemoryUsage(vMemUsage);
    metrics.setHeapUsage(heapUsage);
    // test cpu usage value
    assertEquals("Cpu usage values mismatch via set", cpuUsage, metrics.getCumulativeCpuUsage());
    // test pMem usage value
    assertEquals(
        "Physical memory usage values mismatch via set",
        pMemUsage,
        metrics.getPhysicalMemoryUsage());
    // test vMem usage value
    assertEquals(
        "Virtual memory usage values mismatch via set", vMemUsage, metrics.getVirtualMemoryUsage());
    // test heap usage value
    assertEquals("Heap usage values mismatch via set", heapUsage, metrics.getHeapUsage());

    // test deepCompare() (pass case)
    testResourceUsageMetricViaDeepCompare(metrics, cpuUsage, vMemUsage, pMemUsage, heapUsage, true);

    // test deepCompare (fail case)
    // test cpu usage mismatch
    testResourceUsageMetricViaDeepCompare(metrics, 0, vMemUsage, pMemUsage, heapUsage, false);
    // test pMem usage mismatch
    testResourceUsageMetricViaDeepCompare(metrics, cpuUsage, vMemUsage, 0, heapUsage, false);
    // test vMem usage mismatch
    testResourceUsageMetricViaDeepCompare(metrics, cpuUsage, 0, pMemUsage, heapUsage, false);
    // test heap usage mismatch
    testResourceUsageMetricViaDeepCompare(metrics, cpuUsage, vMemUsage, pMemUsage, 0, false);

    // define a metric with a fixed value of size()
    ResourceUsageMetrics metrics2 =
        new ResourceUsageMetrics() {
          @Override
          public int size() {
            return -1;
          }
        };
    metrics2.setCumulativeCpuUsage(cpuUsage);
    metrics2.setPhysicalMemoryUsage(pMemUsage);
    metrics2.setVirtualMemoryUsage(vMemUsage);
    metrics2.setHeapUsage(heapUsage);

    // test with size mismatch
    testResourceUsageMetricViaDeepCompare(
        metrics2, cpuUsage, vMemUsage, pMemUsage, heapUsage, false);
  }