jmeter-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mchassagn...@apache.org
Subject svn commit: r1802933 - in /jmeter/trunk: src/components/org/apache/jmeter/visualizers/backend/ test/src/org/apache/jmeter/visualizers/backend/
Date Tue, 25 Jul 2017 12:59:56 GMT
Author: mchassagneux
Date: Tue Jul 25 12:59:56 2017
New Revision: 1802933

URL: http://svn.apache.org/viewvc?rev=1802933&view=rev
Log:
Fix reset metric on timed mode and split JUnit test ( for timed et fixed mode ) to handle
correctly the real behavior of JMeter
Bugzilla Id: 61339

Added:
    jmeter/trunk/test/src/org/apache/jmeter/visualizers/backend/SamplerMetricFixedModeTest.java
    jmeter/trunk/test/src/org/apache/jmeter/visualizers/backend/SamplerMetricTimedModeTest.java
Removed:
    jmeter/trunk/test/src/org/apache/jmeter/visualizers/backend/SamplerMetricTest.java
Modified:
    jmeter/trunk/src/components/org/apache/jmeter/visualizers/backend/SamplerMetric.java

Modified: jmeter/trunk/src/components/org/apache/jmeter/visualizers/backend/SamplerMetric.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/src/components/org/apache/jmeter/visualizers/backend/SamplerMetric.java?rev=1802933&r1=1802932&r2=1802933&view=diff
==============================================================================
--- jmeter/trunk/src/components/org/apache/jmeter/visualizers/backend/SamplerMetric.java (original)
+++ jmeter/trunk/src/components/org/apache/jmeter/visualizers/backend/SamplerMetric.java Tue
Jul 25 12:59:56 2017
@@ -18,9 +18,7 @@
 
 package org.apache.jmeter.visualizers.backend;
 
-import java.util.ArrayList;
 import java.util.Arrays;
-import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -53,13 +51,13 @@ public class SamplerMetric {
      */
     private DescriptiveStatistics allResponsesStats = new DescriptiveStatistics(LARGE_SLIDING_WINDOW_SIZE);
     /**
-     *  OK, KO, ALL stats if WindowMode is FIXED
+     *  OK, KO, ALL stats
      */
     private List<DescriptiveStatistics> windowedStats = initWindowedStats();
     /**
      * Timeboxed percentiles don't makes sense
      */
-    private DescriptiveStatistics pctResponseStats = new DescriptiveStatistics(LARGE_SLIDING_WINDOW_SIZE);
+    private DescriptiveStatistics pctResponseStats = new DescriptiveStatistics(SLIDING_WINDOW_SIZE);
     private int successes;
     private int failures;
     private int hits;
@@ -70,12 +68,11 @@ public class SamplerMetric {
      * 
      */
     public SamplerMetric() {
-        List<DescriptiveStatistics> stats = new ArrayList<>(4);
-        stats.add(pctResponseStats);
-        stats.addAll(windowedStats);
-        // Limit to sliding window of SLIDING_WINDOW_SIZE values
-        for (DescriptiveStatistics stat : stats) {
-            stat.setWindowSize(SLIDING_WINDOW_SIZE);
+        // Limit to sliding window of SLIDING_WINDOW_SIZE values for FIXED mode
+        if (WINDOW_MODE == WindowMode.FIXED) {
+            for (DescriptiveStatistics stat : windowedStats) {
+                stat.setWindowSize(SLIDING_WINDOW_SIZE);
+            }
         }
     }
 
@@ -83,11 +80,7 @@ public class SamplerMetric {
      * @return List of {@link DescriptiveStatistics}
      */
     private List<DescriptiveStatistics> initWindowedStats() {
-        if (WINDOW_MODE == WindowMode.FIXED) {
-            return Arrays.asList(okResponsesStats, koResponsesStats, allResponsesStats);
-        } else {
-            return Collections.emptyList();
-        }
+        return Arrays.asList(okResponsesStats, koResponsesStats, allResponsesStats);
     }
 
     /**

Added: jmeter/trunk/test/src/org/apache/jmeter/visualizers/backend/SamplerMetricFixedModeTest.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/test/src/org/apache/jmeter/visualizers/backend/SamplerMetricFixedModeTest.java?rev=1802933&view=auto
==============================================================================
--- jmeter/trunk/test/src/org/apache/jmeter/visualizers/backend/SamplerMetricFixedModeTest.java
(added)
+++ jmeter/trunk/test/src/org/apache/jmeter/visualizers/backend/SamplerMetricFixedModeTest.java
Tue Jul 25 12:59:56 2017
@@ -0,0 +1,122 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.jmeter.visualizers.backend;
+
+import static org.junit.Assert.assertEquals;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
+
+import org.apache.jmeter.samplers.SampleResult;
+import org.junit.Before;
+import org.junit.Test;
+
+public class SamplerMetricFixedModeTest {
+
+    private static final int DEFAULT_ELAPSED_TIME = 1_000;
+
+    /**
+     * Method to change a static final field
+     * 
+     * @param field
+     * @param newValue
+     * @throws Exception
+     */
+    static void setFinalStatic(Field field, Object newValue) throws Exception {
+        field.setAccessible(true);
+        Field modifiersField = Field.class.getDeclaredField("modifiers");
+        modifiersField.setAccessible(true);
+        modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
+        field.set(null, newValue);
+    }
+
+    @Before
+    public void initMode() throws NoSuchFieldException, SecurityException, Exception {
+        setFinalStatic(SamplerMetric.class.getDeclaredField("WINDOW_MODE"), WindowMode.FIXED);
+    }
+
+    @Test
+    public void checkResetOkAndAllStats() throws NoSuchFieldException, SecurityException,
Exception {
+
+        SamplerMetric metric = new SamplerMetric();
+        metric.add(createSampleResult(true));
+        assertEquals("Before reset  ok.max", DEFAULT_ELAPSED_TIME, metric.getOkMaxTime(),
0.001);
+        assertEquals("Before reset all.max", DEFAULT_ELAPSED_TIME, metric.getAllMaxTime(),
0.001);
+        assertEquals("Before reset failure", 1, metric.getHits(), 0.0);
+
+        // In fixed mode DescriptiveStatistics are not reset, just sliding on a
+        // window
+        metric.resetForTimeInterval();
+
+        assertEquals("After reset in FIXED mode ok.max", DEFAULT_ELAPSED_TIME, metric.getOkMaxTime(),
0.001);
+        assertEquals("After reset in FIXED mode all.max", DEFAULT_ELAPSED_TIME, metric.getAllMaxTime(),
0.0);
+        assertEquals("After reset failure", 0, metric.getHits(), 0.0);
+        metric = null;
+    }
+
+    @Test
+    public void checkResetKoAndAllStats() throws NoSuchFieldException, SecurityException,
Exception {
+
+        SamplerMetric metric = new SamplerMetric();
+        metric.add(createSampleResult(false));
+        assertEquals("Before reset  ko.max", DEFAULT_ELAPSED_TIME, metric.getKoMaxTime(),
0.001);
+        assertEquals("Before reset all.max", DEFAULT_ELAPSED_TIME, metric.getAllMaxTime(),
0.001);
+        assertEquals("Before reset failure", 1, metric.getFailures(), 0.0);
+
+        // In fixed mode DescriptiveStatistics are not reset, just sliding on a
+        // window
+        metric.resetForTimeInterval();
+
+        assertEquals("After reset in FIXED mode  ko.max", DEFAULT_ELAPSED_TIME, metric.getKoMaxTime(),
0.0);
+        assertEquals("After reset in FIXED mode all.max", DEFAULT_ELAPSED_TIME, metric.getAllMaxTime(),
0.0);
+        assertEquals("After reset failure", 0, metric.getFailures(), 0.001);
+        metric = null;
+    }
+
+    @Test
+    public void checkErrorsDetailStat() {
+
+        SamplerMetric metric = new SamplerMetric();
+        metric.add(createSampleResult("400", "bad request"));
+        metric.add(createSampleResult("400", "Bad Request "));
+        metric.add(createSampleResult("500", "Internal Server Error"));
+        ErrorMetric error = new ErrorMetric(createSampleResult("400", "Bad request"));
+        assertEquals("Count for '400 - bad request' error ", 2, metric.getErrors().get(error),
0.0);
+        error = new ErrorMetric(createSampleResult("500", "Internal Server Error"));
+        assertEquals("Count for '500 - Internal Server Error' error ", 1, metric.getErrors().get(error),
0.0);
+        metric = null;
+    }
+
+    private SampleResult createSampleResult(boolean success) {
+        SampleResult result = new SampleResult();
+        result.setSuccessful(success);
+        result.setSampleCount(1);
+        result.setErrorCount(success ? 0 : 1);
+        result.sampleStart();
+        result.setEndTime(result.getStartTime() + DEFAULT_ELAPSED_TIME);
+        return result;
+    }
+
+    private SampleResult createSampleResult(String errorCode, String errorMessage) {
+        SampleResult result = createSampleResult(false);
+        result.setResponseCode(errorCode);
+        result.setResponseMessage(errorMessage);
+        return result;
+    }
+}

Added: jmeter/trunk/test/src/org/apache/jmeter/visualizers/backend/SamplerMetricTimedModeTest.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/test/src/org/apache/jmeter/visualizers/backend/SamplerMetricTimedModeTest.java?rev=1802933&view=auto
==============================================================================
--- jmeter/trunk/test/src/org/apache/jmeter/visualizers/backend/SamplerMetricTimedModeTest.java
(added)
+++ jmeter/trunk/test/src/org/apache/jmeter/visualizers/backend/SamplerMetricTimedModeTest.java
Tue Jul 25 12:59:56 2017
@@ -0,0 +1,93 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.jmeter.visualizers.backend;
+
+import static org.junit.Assert.assertEquals;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
+
+import org.apache.jmeter.samplers.SampleResult;
+import org.junit.Before;
+import org.junit.Test;
+
+public class SamplerMetricTimedModeTest {
+
+    private static final int DEFAULT_ELAPSED_TIME = 1_000;
+
+    static void setFinalStatic(Field field, Object newValue) throws Exception {
+        field.setAccessible(true);
+        Field modifiersField = Field.class.getDeclaredField("modifiers");
+        modifiersField.setAccessible(true);
+        modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
+        field.set(null, newValue);
+    }
+
+    @Before
+    public void initMode() throws NoSuchFieldException, SecurityException, Exception {
+        setFinalStatic(SamplerMetric.class.getDeclaredField("WINDOW_MODE"), WindowMode.TIMED);
+    }
+
+
+    @Test
+    public void checkResetOkAndAllStats() throws NoSuchFieldException, SecurityException,
Exception {
+
+        SamplerMetric metric = new SamplerMetric();
+
+        metric.add(createSampleResult(true));
+        assertEquals("Before reset  ok.max", DEFAULT_ELAPSED_TIME, metric.getOkMaxTime(),
0.001);
+        assertEquals("Before reset all.max", DEFAULT_ELAPSED_TIME, metric.getAllMaxTime(),
0.001);
+        assertEquals("Before reset failure", 1, metric.getHits(), 0.0);
+
+        metric.resetForTimeInterval();
+
+        assertEquals("After reset in TIMED mode ok.max", Double.NaN, metric.getOkMaxTime(),
0.0);
+        assertEquals("After reset in TIMED mode all.max", Double.NaN, metric.getAllMaxTime(),
0.0);
+        assertEquals("After reset failure", 0, metric.getHits(), 0.0);
+        metric = null;
+    }
+
+    @Test
+    public void checkResetKoAndAllStats() throws NoSuchFieldException, SecurityException,
Exception {
+
+        SamplerMetric metric = new SamplerMetric();
+        metric.add(createSampleResult(false));
+        assertEquals("Before reset  ko.max", DEFAULT_ELAPSED_TIME, metric.getKoMaxTime(),
0.001);
+        assertEquals("Before reset all.max", DEFAULT_ELAPSED_TIME, metric.getAllMaxTime(),
0.001);
+        assertEquals("Before reset failure", 1, metric.getFailures(), 0.0);
+
+        metric.resetForTimeInterval();
+
+        assertEquals("After reset in TIMED mode  ko.max", Double.NaN, metric.getKoMaxTime(),
0.0);
+        assertEquals("After reset in TIMED mode all.max", Double.NaN, metric.getAllMaxTime(),
0.0);
+        assertEquals("After reset failure", 0, metric.getFailures(), 0.001);
+        metric = null;
+    }
+
+    private SampleResult createSampleResult(boolean success) {
+        SampleResult result = new SampleResult();
+        result.setSuccessful(success);
+        result.setSampleCount(1);
+        result.setErrorCount(success ? 0 : 1);
+        result.sampleStart();
+        result.setEndTime(result.getStartTime() + DEFAULT_ELAPSED_TIME);
+        return result;
+    }
+
+}



Mime
View raw message