You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sirona.apache.org by rm...@apache.org on 2013/11/19 17:54:59 UTC

svn commit: r1543492 - in /incubator/sirona/trunk/core/src: main/java/org/apache/sirona/counters/ test/java/org/apache/sirona/counters/

Author: rmannibucau
Date: Tue Nov 19 16:54:58 2013
New Revision: 1543492

URL: http://svn.apache.org/r1543492
Log:
mocking stats we don't use in SummaryStatistics

Added:
    incubator/sirona/trunk/core/src/main/java/org/apache/sirona/counters/NoopStat.java
    incubator/sirona/trunk/core/src/main/java/org/apache/sirona/counters/OptimizedStatistics.java
    incubator/sirona/trunk/core/src/test/java/org/apache/sirona/counters/OptimizedStatisticsTest.java
Modified:
    incubator/sirona/trunk/core/src/main/java/org/apache/sirona/counters/DefaultCounter.java

Modified: incubator/sirona/trunk/core/src/main/java/org/apache/sirona/counters/DefaultCounter.java
URL: http://svn.apache.org/viewvc/incubator/sirona/trunk/core/src/main/java/org/apache/sirona/counters/DefaultCounter.java?rev=1543492&r1=1543491&r2=1543492&view=diff
==============================================================================
--- incubator/sirona/trunk/core/src/main/java/org/apache/sirona/counters/DefaultCounter.java (original)
+++ incubator/sirona/trunk/core/src/main/java/org/apache/sirona/counters/DefaultCounter.java Tue Nov 19 16:54:58 2013
@@ -30,14 +30,15 @@ public class DefaultCounter implements C
     private final Key key;
     private final CounterDataStore dataStore;
     private volatile int maxConcurrency = 0;
-    protected SummaryStatistics statistics;
+    protected OptimizedStatistics statistics;
     protected ReadWriteLock lock = new ReentrantReadWriteLock();
     private ObjectName jmx = null;
 
     public DefaultCounter(final Key key, final CounterDataStore store) {
         this.key = key;
-        this.statistics = new SummaryStatistics();
         this.dataStore = store;
+
+        this.statistics = new OptimizedStatistics();
     }
 
     public void addInternal(final double delta) { // should be called from a thread safe environment

Added: incubator/sirona/trunk/core/src/main/java/org/apache/sirona/counters/NoopStat.java
URL: http://svn.apache.org/viewvc/incubator/sirona/trunk/core/src/main/java/org/apache/sirona/counters/NoopStat.java?rev=1543492&view=auto
==============================================================================
--- incubator/sirona/trunk/core/src/main/java/org/apache/sirona/counters/NoopStat.java (added)
+++ incubator/sirona/trunk/core/src/main/java/org/apache/sirona/counters/NoopStat.java Tue Nov 19 16:54:58 2013
@@ -0,0 +1,73 @@
+/*
+ * 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.sirona.counters;
+
+import org.apache.commons.math3.exception.MathIllegalArgumentException;
+import org.apache.commons.math3.stat.descriptive.StorelessUnivariateStatistic;
+
+public class NoopStat implements StorelessUnivariateStatistic {
+    public static final NoopStat INSTANCE = new NoopStat();
+
+    public NoopStat() {
+        // no-op
+    }
+
+    @Override
+    public void increment(final double d) {
+        // no-op
+    }
+
+    @Override
+    public void incrementAll(final double[] values) throws MathIllegalArgumentException {
+        // no-op
+    }
+
+    @Override
+    public void incrementAll(final double[] values, final int start, final int length) throws MathIllegalArgumentException {
+        // no-op
+    }
+
+    @Override
+    public double getResult() {
+        return 0;
+    }
+
+    @Override
+    public long getN() {
+        return 0;
+    }
+
+    @Override
+    public void clear() {
+        // no-op
+    }
+
+    @Override
+    public double evaluate(final double[] values) throws MathIllegalArgumentException {
+        return 0;
+    }
+
+    @Override
+    public double evaluate(final double[] values, final int begin, final int length) throws MathIllegalArgumentException {
+        return 0;
+    }
+
+    @Override
+    public StorelessUnivariateStatistic copy() {
+        return null;
+    }
+}

Added: incubator/sirona/trunk/core/src/main/java/org/apache/sirona/counters/OptimizedStatistics.java
URL: http://svn.apache.org/viewvc/incubator/sirona/trunk/core/src/main/java/org/apache/sirona/counters/OptimizedStatistics.java?rev=1543492&view=auto
==============================================================================
--- incubator/sirona/trunk/core/src/main/java/org/apache/sirona/counters/OptimizedStatistics.java (added)
+++ incubator/sirona/trunk/core/src/main/java/org/apache/sirona/counters/OptimizedStatistics.java Tue Nov 19 16:54:58 2013
@@ -0,0 +1,27 @@
+/*
+ * 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.sirona.counters;
+
+import org.apache.commons.math3.stat.descriptive.SummaryStatistics;
+
+public class OptimizedStatistics extends SummaryStatistics {
+    public OptimizedStatistics() {
+        // we don't use sumsq and sumlog so mock them to gain a lot of time in concurrent environments
+        setSumsqImpl(NoopStat.INSTANCE);
+        setSumLogImpl(NoopStat.INSTANCE);
+    }
+}

Added: incubator/sirona/trunk/core/src/test/java/org/apache/sirona/counters/OptimizedStatisticsTest.java
URL: http://svn.apache.org/viewvc/incubator/sirona/trunk/core/src/test/java/org/apache/sirona/counters/OptimizedStatisticsTest.java?rev=1543492&view=auto
==============================================================================
--- incubator/sirona/trunk/core/src/test/java/org/apache/sirona/counters/OptimizedStatisticsTest.java (added)
+++ incubator/sirona/trunk/core/src/test/java/org/apache/sirona/counters/OptimizedStatisticsTest.java Tue Nov 19 16:54:58 2013
@@ -0,0 +1,46 @@
+/*
+ * 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.sirona.counters;
+
+import org.apache.commons.math3.stat.descriptive.SummaryStatistics;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class OptimizedStatisticsTest {
+    @Test
+    public void checkConsistency() {
+        final SummaryStatistics normal = new SummaryStatistics();
+        final OptimizedStatistics optimized = new OptimizedStatistics();
+
+        for (int i = 0; i < 100; i++) {
+            final double value = Math.random() * 100;
+            normal.addValue(value);
+            optimized.addValue(value);
+        }
+
+        // Counter only relies on a subset of all stats of SummarryStatistics
+        assertEquals(normal.getN(), optimized.getN(), 0.);
+        assertEquals(normal.getMax(), optimized.getMax(), 0.);
+        assertEquals(normal.getMin(), optimized.getMin(), 0.);
+        assertEquals(normal.getSum(), optimized.getSum(), 0.);
+        assertEquals(normal.getStandardDeviation(), optimized.getStandardDeviation(), 0.);
+        assertEquals(normal.getVariance(), optimized.getVariance(), 0.);
+        assertEquals(normal.getMean(), optimized.getMean(), 0.);
+        assertEquals(normal.getSecondMoment(), optimized.getSecondMoment(), 0.);
+    }
+}