jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From alexparvule...@apache.org
Subject svn commit: r1153869 - in /jackrabbit/trunk/jackrabbit-core/src: main/java/org/apache/jackrabbit/core/jmx/core/ test/java/org/apache/jackrabbit/core/jmx/core/ test/java/org/apache/jackrabbit/core/jmx/query/ test/java/org/apache/jackrabbit/core/jmx/util/
Date Thu, 04 Aug 2011 12:43:23 GMT
Author: alexparvulescu
Date: Thu Aug  4 12:43:22 2011
New Revision: 1153869

URL: http://svn.apache.org/viewvc?rev=1153869&view=rev
Log:
JCR-3040 JMX Stats for the Session
 - part 2

Added:
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/core/CoreStat.java
  (with props)
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/core/CoreStatImpl.java
  (with props)
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/core/CoreStatManager.java
  (with props)
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/core/CoreStatManagerMBean.java
  (with props)
    jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/jmx/core/CoreStatSessionTest.java
  (with props)
    jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/jmx/query/QueryStatManagerTest.java
      - copied, changed from r1152795, jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/jmx/QueryStatManagerTest.java
    jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/jmx/util/CachingOpsPerSecondDtoTest.java
  (with props)

Added: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/core/CoreStat.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/core/CoreStat.java?rev=1153869&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/core/CoreStat.java
(added)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/core/CoreStat.java
Thu Aug  4 12:43:22 2011
@@ -0,0 +1,46 @@
+package org.apache.jackrabbit.core.jmx.core;
+
+public interface CoreStat {
+
+    /** -- SESSION INFO -- **/
+
+    void sessionCreated();
+
+    void sessionLoggedOut();
+
+    long getNumberOfSessions();
+
+    void resetNumberOfSessions();
+
+    /**
+     * @param timeNs
+     *            as given by timeNs = System.nanoTime() - timeNs;
+     */
+    void onSessionOperation(boolean isWrite, long timeNs);
+
+    double getReadOpsPerSecond();
+
+    double getWriteOpsPerSecond();
+
+    void resetNumberOfOperations();
+
+    /**
+     * If this service is currently registering stats
+     * 
+     * @return <code>true</code> if the service is enabled
+     */
+    boolean isEnabled();
+
+    /**
+     * Enables/Disables the service
+     * 
+     * @param enabled
+     */
+    void setEnabled(boolean enabled);
+
+    /**
+     * clears all data
+     */
+    void reset();
+
+}

Propchange: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/core/CoreStat.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/core/CoreStatImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/core/CoreStatImpl.java?rev=1153869&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/core/CoreStatImpl.java
(added)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/core/CoreStatImpl.java
Thu Aug  4 12:43:22 2011
@@ -0,0 +1,82 @@
+package org.apache.jackrabbit.core.jmx.core;
+
+import java.util.concurrent.atomic.AtomicLong;
+
+import org.apache.jackrabbit.core.jmx.util.CachingOpsPerSecondDto;
+
+public class CoreStatImpl implements CoreStat {
+
+    /** -- SESSION INFO -- **/
+
+    private final AtomicLong sessions = new AtomicLong(0);
+
+    private boolean enabled = false;
+
+    private final CachingOpsPerSecondDto reads = new CachingOpsPerSecondDto();
+
+    private final CachingOpsPerSecondDto writes = new CachingOpsPerSecondDto();
+
+    public void sessionCreated() {
+        if (!enabled) {
+            return;
+        }
+        sessions.incrementAndGet();
+    }
+
+    public void sessionLoggedOut() {
+        if (!enabled || sessions.get() == 0) {
+            return;
+        }
+        sessions.decrementAndGet();
+    }
+
+    public long getNumberOfSessions() {
+        return sessions.get();
+    }
+
+    public void resetNumberOfSessions() {
+        sessions.set(0);
+    }
+
+    public void onSessionOperation(boolean isWrite, long timeNs) {
+        if (!enabled) {
+            return;
+        }
+        if (isWrite) {
+            writes.onOp(timeNs);
+        } else {
+            reads.onOp(timeNs);
+        }
+    }
+
+    public double getReadOpsPerSecond() {
+        return reads.getOpsPerSecond();
+    }
+
+    public double getWriteOpsPerSecond() {
+        return writes.getOpsPerSecond();
+    }
+
+    public void resetNumberOfOperations() {
+        reads.reset();
+        writes.reset();
+    }
+
+    /** -- GENERAL INFO -- **/
+
+    public boolean isEnabled() {
+        return enabled;
+    }
+
+    public void setEnabled(boolean enabled) {
+        this.enabled = enabled;
+        if (!this.enabled) {
+            reset();
+        }
+    }
+
+    public void reset() {
+        resetNumberOfSessions();
+        resetNumberOfOperations();
+    }
+}

Propchange: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/core/CoreStatImpl.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/core/CoreStatManager.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/core/CoreStatManager.java?rev=1153869&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/core/CoreStatManager.java
(added)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/core/CoreStatManager.java
Thu Aug  4 12:43:22 2011
@@ -0,0 +1,43 @@
+package org.apache.jackrabbit.core.jmx.core;
+
+public class CoreStatManager implements CoreStatManagerMBean {
+
+    private final CoreStat coreStat;
+
+    public CoreStatManager(final CoreStat coreStat) {
+        this.coreStat = coreStat;
+    }
+
+    public long getNumberOfSessions() {
+        return coreStat.getNumberOfSessions();
+    }
+
+    public void resetNumberOfSessions() {
+        this.coreStat.resetNumberOfSessions();
+    }
+
+    public boolean isEnabled() {
+        return this.coreStat.isEnabled();
+    }
+
+    public void setEnabled(boolean enabled) {
+        this.coreStat.setEnabled(enabled);
+    }
+
+    public void reset() {
+        this.coreStat.reset();
+    }
+
+    public double getReadOpsPerSecond() {
+        return this.coreStat.getReadOpsPerSecond();
+    }
+
+    public double getWriteOpsPerSecond() {
+        return this.coreStat.getWriteOpsPerSecond();
+    }
+
+    public void resetNumberOfOperations() {
+        this.coreStat.resetNumberOfOperations();
+
+    }
+}

Propchange: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/core/CoreStatManager.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/core/CoreStatManagerMBean.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/core/CoreStatManagerMBean.java?rev=1153869&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/core/CoreStatManagerMBean.java
(added)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/core/CoreStatManagerMBean.java
Thu Aug  4 12:43:22 2011
@@ -0,0 +1,19 @@
+package org.apache.jackrabbit.core.jmx.core;
+
+import org.apache.jackrabbit.core.jmx.JackrabbitBaseMBean;
+
+public interface CoreStatManagerMBean extends JackrabbitBaseMBean {
+
+    String NAME = BASE_NAME + ":type=CoreStats";
+
+    long getNumberOfSessions();
+
+    void resetNumberOfSessions();
+
+    double getReadOpsPerSecond();
+
+    double getWriteOpsPerSecond();
+
+    void resetNumberOfOperations();
+
+}

Propchange: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/core/CoreStatManagerMBean.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/jmx/core/CoreStatSessionTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/jmx/core/CoreStatSessionTest.java?rev=1153869&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/jmx/core/CoreStatSessionTest.java
(added)
+++ jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/jmx/core/CoreStatSessionTest.java
Thu Aug  4 12:43:22 2011
@@ -0,0 +1,186 @@
+/*
+ * 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.jackrabbit.core.jmx.core;
+
+import java.math.BigDecimal;
+import java.math.MathContext;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+
+import javax.jcr.Session;
+import javax.jcr.SimpleCredentials;
+
+import org.apache.jackrabbit.core.jmx.AbstractJmxTest;
+import org.apache.jackrabbit.core.jmx.util.CachingOpsPerSecondDto;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * JMX test cases for Core Stats.
+ */
+public class CoreStatSessionTest extends AbstractJmxTest {
+
+    private static Logger log = LoggerFactory
+            .getLogger(CoreStatSessionTest.class);
+
+    private CoreStat cs;
+
+    protected void setUp() throws Exception {
+        super.setUp();
+        cs = jmxRegistry.getCoreStat();
+        cs.resetNumberOfSessions();
+    }
+
+    public void testNumberOfSessions1() throws Exception {
+
+        cs.setEnabled(true);
+        assertEquals(0, cs.getNumberOfSessions());
+
+        Session s = superuser.impersonate(new SimpleCredentials("anonymous", ""
+                .toCharArray()));
+        assertNotNull(s);
+        assertEquals(1, cs.getNumberOfSessions());
+
+        s.logout();
+        assertEquals(0, cs.getNumberOfSessions());
+    }
+
+    public void testNumberOfSessions2() throws Exception {
+
+        cs.setEnabled(false);
+        assertEquals(0, cs.getNumberOfSessions());
+
+        Session s = superuser.impersonate(new SimpleCredentials("anonymous", ""
+                .toCharArray()));
+        assertNotNull(s);
+        assertEquals(0, cs.getNumberOfSessions());
+
+        s.logout();
+        assertEquals(0, cs.getNumberOfSessions());
+    }
+
+    public void testNumberOfSessions3() throws Exception {
+
+        cs.setEnabled(true);
+        assertEquals(0, cs.getNumberOfSessions());
+
+        Session s = superuser.impersonate(new SimpleCredentials("anonymous", ""
+                .toCharArray()));
+        assertNotNull(s);
+        assertEquals(1, cs.getNumberOfSessions());
+
+        cs.resetNumberOfSessions();
+        assertEquals(0, cs.getNumberOfSessions());
+
+        s.logout();
+        assertEquals(0, cs.getNumberOfSessions());
+    }
+
+    public void _testNumberOfSessionsImpact() throws Exception {
+
+        int times = 10;
+
+        for (int i = 0; i < times; i++) {
+
+            int operations = 100;
+            int threads = 25;
+
+            // no stats
+            cs.setEnabled(false);
+            long take1 = doLoginLogout(superuser, threads, operations);
+
+            // stats
+            cs.setEnabled(true);
+            cs.resetNumberOfSessions();
+            long take2 = doLoginLogout(superuser, threads, operations);
+
+            long diff = take2 - take1;
+            double extra = (double) diff / operations;
+            BigDecimal p = new BigDecimal(extra * 100 / take1, new MathContext(
+                    2));
+
+            log.info(threads + " thread(s) performing " + operations
+                    + " each: overhead total diff time " + diff + "(" + p
+                    + "%). overhead per op " + extra);
+
+        }
+    }
+
+    private long doLoginLogout(final Session admin, final int threads,
+            final int times) throws Exception {
+
+        ExecutorService es = Executors.newFixedThreadPool(threads);
+        List<Future<Long>> futures = new ArrayList<Future<Long>>();
+
+        long t = System.currentTimeMillis();
+        for (int i = 0; i < threads; i++) {
+            futures.add(es.submit(new Callable<Long>() {
+                public Long call() throws Exception {
+                    return doLoginLogoutOp(admin, times);
+                }
+            }));
+        }
+        es.shutdown();
+        for (Future<Long> f : futures) {
+            f.get();
+        }
+        return System.currentTimeMillis() - t;
+    }
+
+    private long doLoginLogoutOp(Session admin, int times) throws Exception {
+        long t = System.currentTimeMillis();
+
+        for (int i = 0; i < times; i++) {
+            Session s = superuser.impersonate(new SimpleCredentials(
+                    "anonymous", "".toCharArray()));
+            s.logout();
+        }
+        return System.currentTimeMillis() - t;
+    }
+
+    static {
+        CachingOpsPerSecondDto.DEFAULT_UPDATE_FREQ_MS = 4000;
+    }
+
+    public void testNumberOfOpsPerSecond1() throws Exception {
+
+        cs.setEnabled(true);
+
+        int go = 0;
+        while (true) {
+            for (int i = 0; i < 50; i++) {
+                long t = System.currentTimeMillis();
+                int cnt = 10000;
+                for (int j = 0; j < cnt; j++) {
+                    superuser.getNode(testRoot);
+                }
+                t = System.currentTimeMillis() - t;
+                System.out.println("took " + t + " ms for " + cnt);
+            }
+            TimeUnit.SECONDS.sleep(3);
+            go++;
+            if (go == 500) {
+                return;
+            }
+        }
+    }
+}

Propchange: jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/jmx/core/CoreStatSessionTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Copied: jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/jmx/query/QueryStatManagerTest.java
(from r1152795, jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/jmx/QueryStatManagerTest.java)
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/jmx/query/QueryStatManagerTest.java?p2=jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/jmx/query/QueryStatManagerTest.java&p1=jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/jmx/QueryStatManagerTest.java&r1=1152795&r2=1153869&rev=1153869&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/jmx/QueryStatManagerTest.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/jmx/query/QueryStatManagerTest.java
Thu Aug  4 12:43:22 2011
@@ -14,21 +14,18 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.jackrabbit.core.jmx;
+package org.apache.jackrabbit.core.jmx.query;
 
 import javax.jcr.query.Query;
 
-import org.apache.jackrabbit.core.RepositoryImpl;
-import org.apache.jackrabbit.core.jmx.query.QueryStat;
-import org.apache.jackrabbit.core.jmx.query.QueryStatManager;
-import org.apache.jackrabbit.core.query.AbstractQueryTest;
+import org.apache.jackrabbit.core.jmx.AbstractJmxTest;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
  * Performs various JMX test cases.
  */
-public class QueryStatManagerTest extends AbstractQueryTest {
+public class QueryStatManagerTest extends AbstractJmxTest {
 
     private static Logger log = LoggerFactory
             .getLogger(QueryStatManagerTest.class);
@@ -38,9 +35,8 @@ public class QueryStatManagerTest extend
         int queueSize = 15;
         int runTimes = 50;
 
-        RepositoryImpl r = (RepositoryImpl) superuser.getRepository();
-        QueryStatManager qsm = r.getJmxRegistry().getQueryStatManager();
-        qsm.setQueueSize(queueSize);
+        QueryStat qsm = jmxRegistry.getQueryStat();
+        qsm.setSlowQueriesQueueSize(queueSize);
         qsm.setEnabled(false);
 
         // just to warm up the cache
@@ -56,7 +52,7 @@ public class QueryStatManagerTest extend
                 + ", without QueryStatManager = " + off
                 + ", with QueryStatManager = " + on);
 
-        QueryStat[] top = qsm.getTopQueries();
+        QueryStatDto[] top = qsm.getSlowQueries();
         assertNotNull("Query Top should not be null", top);
         assertEquals("Query Top should contain entries ",
                 Math.min(queueSize, runTimes), top.length);
@@ -67,7 +63,8 @@ public class QueryStatManagerTest extend
         long total = 0;
         for (int i = 0; i < times; i++) {
             long start = System.currentTimeMillis();
-            Query q = qm.createQuery("SELECT * FROM [nt:base]", Query.JCR_SQL2);
+            Query q = superuser.getWorkspace().getQueryManager()
+                    .createQuery("SELECT * FROM [nt:base]", Query.JCR_SQL2);
             q.execute();
             long dur = System.currentTimeMillis() - start;
             total += dur;

Added: jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/jmx/util/CachingOpsPerSecondDtoTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/jmx/util/CachingOpsPerSecondDtoTest.java?rev=1153869&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/jmx/util/CachingOpsPerSecondDtoTest.java
(added)
+++ jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/jmx/util/CachingOpsPerSecondDtoTest.java
Thu Aug  4 12:43:22 2011
@@ -0,0 +1,137 @@
+/*
+ * 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.jackrabbit.core.jmx.util;
+
+import java.math.BigDecimal;
+import java.math.MathContext;
+import java.math.RoundingMode;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Random;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.jackrabbit.test.JUnitTest;
+
+public class CachingOpsPerSecondDtoTest extends JUnitTest {
+
+    private final CachingOpsPerSecondDto test = new CachingOpsPerSecondDto(
+            1000 * 10000);
+
+    protected void setUp() throws Exception {
+        super.setUp();
+        test.reset();
+    }
+
+    public void testSimple() throws Exception {
+
+        long t = System.currentTimeMillis();
+        test.reset();
+        test.onOp(10);
+        test.onOp(1);
+        TimeUnit.MILLISECONDS.sleep(1300);
+        test.onOp(2);
+        long d_min = System.currentTimeMillis() - t;
+        test.refresh();
+        long d_max = System.currentTimeMillis() - t;
+
+        // 3 ops in ~ 1.3 seconds = 2.3 ops / sec
+        double expected_min = BigDecimal.valueOf(3 * 1000)
+                .divide(BigDecimal.valueOf(d_min), new MathContext(3))
+                .round(new MathContext(2, RoundingMode.DOWN)).doubleValue();
+        double expected_max = BigDecimal.valueOf(3 * 1000)
+                .divide(BigDecimal.valueOf(d_max), new MathContext(3))
+                .add(new BigDecimal(0.001))
+                .round(new MathContext(2, RoundingMode.UP)).doubleValue();
+        double opsPerSecond = test.getOpsPerSecond();
+
+        assertTrue(opsPerSecond + "?" + expected_min,
+                opsPerSecond >= expected_min);
+        assertTrue(opsPerSecond + "?" + expected_max,
+                opsPerSecond <= expected_max);
+        assertEquals(4.33, test.getOpAvgTime());
+
+    }
+
+    public void testMT() throws Exception {
+        int threads = 20;
+        int ops = 60 * threads;
+
+        final Random r = new Random();
+        test.reset();
+        long t = System.currentTimeMillis();
+        int aggDuration = 0;
+        ExecutorService executor = Executors.newFixedThreadPool(threads);
+        List<Future<Void>> futures = new ArrayList<Future<Void>>();
+        for (int i = 0; i < ops; i++) {
+            int duration = 35 + r.nextInt(10);
+            boolean shouldRefresh = i % 10 == 0;
+            futures.add(executor.submit(newCallable(test, duration, 75,
+                    shouldRefresh)));
+            aggDuration += duration;
+        }
+        executor.shutdown();
+        for (Future<Void> f : futures) {
+            f.get();
+        }
+
+        long d_min = System.currentTimeMillis() - t;
+        test.refresh();
+        long d_max = System.currentTimeMillis() - t;
+
+        double expected_min = BigDecimal.valueOf(ops * 1000)
+                .divide(BigDecimal.valueOf(d_min), new MathContext(3))
+                .round(new MathContext(2, RoundingMode.DOWN)).doubleValue();
+        double expected_max = BigDecimal.valueOf(ops * 1000)
+                .divide(BigDecimal.valueOf(d_max), new MathContext(3))
+                .add(new BigDecimal(0.001))
+                .round(new MathContext(2, RoundingMode.UP)).doubleValue();
+        double opsPerSecond = test.getOpsPerSecond();
+
+        assertTrue(opsPerSecond + "?" + expected_min,
+                opsPerSecond >= expected_min);
+        assertTrue(opsPerSecond + "?" + expected_max,
+                opsPerSecond <= expected_max);
+
+        double expectedAvg = BigDecimal
+                .valueOf(aggDuration)
+                .divide(BigDecimal.valueOf(ops),
+                        new MathContext(2, RoundingMode.DOWN)).doubleValue();
+        assertEquals(expectedAvg, BigDecimal.valueOf(test.getOpAvgTime())
+                .round(new MathContext(2, RoundingMode.DOWN)).doubleValue());
+
+        System.out.println(ops + " / " + test.getOpAvgTime());
+    }
+
+    private Callable<Void> newCallable(final CachingOpsPerSecondDto test,
+            final int duration, final long sleep, final boolean shouldRefresh) {
+        return new Callable<Void>() {
+
+            public Void call() throws Exception {
+                test.onOp(duration);
+                TimeUnit.MILLISECONDS.sleep(sleep);
+                if (shouldRefresh) {
+                    test.refresh();
+                }
+                return null;
+            }
+        };
+    }
+}

Propchange: jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/jmx/util/CachingOpsPerSecondDtoTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message