Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGreg Wilkins2015-09-24 07:19:22 +0000
committerGreg Wilkins2015-09-24 07:19:48 +0000
commit131a471c8faafafb33a0240575d8660b5b127c83 (patch)
tree17e7315188d6f6a86b4a8f3cc2e3c3865c610d14
parent06fbf447fb11fef73b413d7ff42d3f651b4464c8 (diff)
downloadorg.eclipse.jetty.project-131a471c8faafafb33a0240575d8660b5b127c83.tar.gz
org.eclipse.jetty.project-131a471c8faafafb33a0240575d8660b5b127c83.tar.xz
org.eclipse.jetty.project-131a471c8faafafb33a0240575d8660b5b127c83.zip
478008 Do not reset current value of CounterStatistics
-rw-r--r--jetty-util/src/main/java/org/eclipse/jetty/util/statistic/CounterStatistic.java15
-rw-r--r--jetty-util/src/test/java/org/eclipse/jetty/util/statistic/CounterStatisticTest.java80
2 files changed, 92 insertions, 3 deletions
diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/statistic/CounterStatistic.java b/jetty-util/src/main/java/org/eclipse/jetty/util/statistic/CounterStatistic.java
index cb8dd194f1..e03338bd64 100644
--- a/jetty-util/src/main/java/org/eclipse/jetty/util/statistic/CounterStatistic.java
+++ b/jetty-util/src/main/java/org/eclipse/jetty/util/statistic/CounterStatistic.java
@@ -40,15 +40,24 @@ public class CounterStatistic
/* ------------------------------------------------------------ */
public void reset()
{
- reset(0);
+ _total.set(0);
+ _max.set(0);
+ long current=_curr.get();
+ _total.addAndGet(current);
+ Atomics.updateMax(_max,current);
}
/* ------------------------------------------------------------ */
public void reset(final long value)
{
- _max.set(value);
+ _total.set(0);
+ _max.set(0);
_curr.set(value);
- _total.set(0); // total always set to 0 to properly calculate cumulative total
+ if (value>0)
+ {
+ _total.addAndGet(value);
+ Atomics.updateMax(_max,value);
+ }
}
/* ------------------------------------------------------------ */
diff --git a/jetty-util/src/test/java/org/eclipse/jetty/util/statistic/CounterStatisticTest.java b/jetty-util/src/test/java/org/eclipse/jetty/util/statistic/CounterStatisticTest.java
new file mode 100644
index 0000000000..f04d39bc9b
--- /dev/null
+++ b/jetty-util/src/test/java/org/eclipse/jetty/util/statistic/CounterStatisticTest.java
@@ -0,0 +1,80 @@
+//
+// ========================================================================
+// Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
+// ------------------------------------------------------------------------
+// All rights reserved. This program and the accompanying materials
+// are made available under the terms of the Eclipse Public License v1.0
+// and Apache License v2.0 which accompanies this distribution.
+//
+// The Eclipse Public License is available at
+// http://www.eclipse.org/legal/epl-v10.html
+//
+// The Apache License v2.0 is available at
+// http://www.opensource.org/licenses/apache2.0.php
+//
+// You may elect to redistribute this code under either of these licenses.
+// ========================================================================
+//
+
+package org.eclipse.jetty.util.statistic;
+
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.greaterThan;
+import static org.hamcrest.Matchers.lessThan;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThat;
+
+import org.hamcrest.Matchers;
+import org.junit.Assert;
+import org.junit.Test;
+
+
+/* ------------------------------------------------------------ */
+public class CounterStatisticTest
+{
+
+ @Test
+ public void testCounter()
+ throws Exception
+ {
+ CounterStatistic count = new CounterStatistic();
+
+ assertThat(count.getCurrent(),equalTo(0L));
+ assertThat(count.getMax(),equalTo(0L));
+ assertThat(count.getTotal(),equalTo(0L));
+
+ count.increment();
+ count.increment();
+ count.decrement();
+ count.add(4);
+ count.add(-2);
+
+ assertThat(count.getCurrent(),equalTo(3L));
+ assertThat(count.getMax(),equalTo(5L));
+ assertThat(count.getTotal(),equalTo(6L));
+
+ count.reset();
+ assertThat(count.getCurrent(),equalTo(3L));
+ assertThat(count.getMax(),equalTo(3L));
+ assertThat(count.getTotal(),equalTo(3L));
+
+ count.increment();
+ count.decrement();
+ count.add(-2);
+ count.decrement();
+ assertThat(count.getCurrent(),equalTo(0L));
+ assertThat(count.getMax(),equalTo(4L));
+ assertThat(count.getTotal(),equalTo(4L));
+
+ count.decrement();
+ assertThat(count.getCurrent(),equalTo(-1L));
+ assertThat(count.getMax(),equalTo(4L));
+ assertThat(count.getTotal(),equalTo(4L));
+
+ count.increment();
+ assertThat(count.getCurrent(),equalTo(0L));
+ assertThat(count.getMax(),equalTo(4L));
+ assertThat(count.getTotal(),equalTo(5L));
+ }
+
+}

Back to the top