Search in sources :

Example 1 with Calculator

use of org.apache.jmeter.util.Calculator in project jmeter by apache.

the class TestSampleResult method testSubResults.

private void testSubResults(boolean nanoTime, long nanoThreadSleep, long pause) throws Exception {
    // This test tries to emulate a http sample, with two
    // subsamples, representing images that are downloaded for the
    // page representing the first sample.
    // Sample that will get two sub results, simulates a web page load 
    SampleResult parent = new SampleResult(nanoTime, nanoThreadSleep);
    JMeterTestCase.assertPrimitiveEquals(nanoTime, parent.useNanoTime);
    assertEquals(nanoThreadSleep, parent.nanoThreadSleep);
    long beginTest = parent.currentTimeInMillis();
    parent.sampleStart();
    Thread.sleep(100);
    parent.setBytes(300L);
    parent.setSampleLabel("Parent Sample");
    parent.setSuccessful(true);
    parent.sampleEnd();
    long parentElapsed = parent.getTime();
    // Sample with no sub results, simulates an image download
    SampleResult child1 = new SampleResult(nanoTime);
    child1.sampleStart();
    Thread.sleep(100);
    child1.setBytes(100L);
    child1.setSampleLabel("Child1 Sample");
    child1.setSuccessful(true);
    child1.sampleEnd();
    long child1Elapsed = child1.getTime();
    assertTrue(child1.isSuccessful());
    assertEquals(100, child1.getBytesAsLong());
    assertEquals("Child1 Sample", child1.getSampleLabel());
    assertEquals(1, child1.getSampleCount());
    assertEquals(0, child1.getSubResults().length);
    long actualPause = 0;
    if (pause > 0) {
        long t1 = parent.currentTimeInMillis();
        Thread.sleep(pause);
        actualPause = parent.currentTimeInMillis() - t1;
    }
    // Sample with no sub results, simulates an image download 
    SampleResult child2 = new SampleResult(nanoTime);
    child2.sampleStart();
    Thread.sleep(100);
    child2.setBytes(200L);
    child2.setSampleLabel("Child2 Sample");
    child2.setSuccessful(true);
    child2.sampleEnd();
    long child2Elapsed = child2.getTime();
    assertTrue(child2.isSuccessful());
    assertEquals(200, child2.getBytesAsLong());
    assertEquals("Child2 Sample", child2.getSampleLabel());
    assertEquals(1, child2.getSampleCount());
    assertEquals(0, child2.getSubResults().length);
    // Now add the subsamples to the sample
    parent.addSubResult(child1);
    parent.addSubResult(child2);
    assertTrue(parent.isSuccessful());
    assertEquals(600, parent.getBytesAsLong());
    assertEquals("Parent Sample", parent.getSampleLabel());
    assertEquals(1, parent.getSampleCount());
    assertEquals(2, parent.getSubResults().length);
    long parentElapsedTotal = parent.getTime();
    long overallTime = parent.currentTimeInMillis() - beginTest;
    long sumSamplesTimes = parentElapsed + child1Elapsed + actualPause + child2Elapsed;
    /*
             * Parent elapsed total should be no smaller than the sum of the individual samples.
             * It may be greater by the timer granularity.
             */
    long diff = parentElapsedTotal - sumSamplesTimes;
    // TimeMillis has granularity of 10-20
    long maxDiff = nanoTime ? 3 : 16;
    if (diff < 0 || diff > maxDiff) {
        fail("ParentElapsed: " + parentElapsedTotal + " - " + " sum(samples): " + sumSamplesTimes + " = " + diff + " not in [0," + maxDiff + "]; nanotime=" + nanoTime);
    }
    /**
             * The overall time to run the test must be no less than, 
             * and may be greater (but not much greater) than the parent elapsed time
             */
    diff = overallTime - parentElapsedTotal;
    if (diff < 0 || diff > maxDiff) {
        fail("TestElapsed: " + overallTime + " - " + " ParentElapsed: " + parentElapsedTotal + " = " + diff + " not in [0," + maxDiff + "]; nanotime=" + nanoTime);
    }
    // Check that calculator gets the correct statistics from the sample
    Calculator calculator = new Calculator();
    calculator.addSample(parent);
    assertEquals(600, calculator.getTotalBytes());
    assertEquals(1, calculator.getCount());
    // Allow for some margin of error
    assertEquals(1d / (parentElapsedTotal / 1000d), calculator.getRate(), 0.0001d);
    // Check that the throughput uses the time elapsed for the sub results
    assertFalse(1d / (parentElapsed / 1000d) <= calculator.getRate());
}
Also used : Calculator(org.apache.jmeter.util.Calculator)

Example 2 with Calculator

use of org.apache.jmeter.util.Calculator in project jmeter by apache.

the class SummaryReport method clearData.

/**
     * Clears this visualizer and its model, and forces a repaint of the table.
     */
@Override
public void clearData() {
    //Synch is needed because a clear can occur while add occurs
    synchronized (lock) {
        model.clearData();
        newRows.clear();
        tableRows.clear();
        tableRows.put(TOTAL_ROW_LABEL, new Calculator(TOTAL_ROW_LABEL));
        model.addRow(tableRows.get(TOTAL_ROW_LABEL));
    }
    dataChanged = true;
}
Also used : Calculator(org.apache.jmeter.util.Calculator)

Example 3 with Calculator

use of org.apache.jmeter.util.Calculator in project jmeter by apache.

the class SummaryReport method add.

@Override
public void add(final SampleResult res) {
    Calculator row = tableRows.computeIfAbsent(res.getSampleLabel(useGroupName.isSelected()), label -> {
        Calculator newRow = new Calculator(label);
        newRows.add(newRow);
        return newRow;
    });
    /*
         * Synch is needed because multiple threads can update the counts.
         */
    synchronized (row) {
        row.addSample(res);
    }
    Calculator tot = tableRows.get(TOTAL_ROW_LABEL);
    synchronized (lock) {
        tot.addSample(res);
    }
    dataChanged = true;
}
Also used : Calculator(org.apache.jmeter.util.Calculator)

Aggregations

Calculator (org.apache.jmeter.util.Calculator)3