jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From alexparvule...@apache.org
Subject svn commit: r1160584 - in /jackrabbit/branches/JCR-2936/jackrabbit-core/src/main/java/org/apache/jackrabbit/core: jmx/ stats/ stats/util/
Date Tue, 23 Aug 2011 08:43:17 GMT
Author: alexparvulescu
Date: Tue Aug 23 08:43:17 2011
New Revision: 1160584

URL: http://svn.apache.org/viewvc?rev=1160584&view=rev
Log:
JCR-2936 JMX Bindings for Jackrabbit
 - stats: new branch, take 1

Added:
    jackrabbit/branches/JCR-2936/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/CoreStatManager.java
  (with props)
    jackrabbit/branches/JCR-2936/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/QueryStatManager.java
  (with props)
    jackrabbit/branches/JCR-2936/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/
    jackrabbit/branches/JCR-2936/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/CoreStatImpl.java
  (with props)
    jackrabbit/branches/JCR-2936/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/QueryStatDtoComparator.java
  (with props)
    jackrabbit/branches/JCR-2936/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/QueryStatDtoImpl.java
  (with props)
    jackrabbit/branches/JCR-2936/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/QueryStatImpl.java
  (with props)
    jackrabbit/branches/JCR-2936/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/StatManager.java
  (with props)
    jackrabbit/branches/JCR-2936/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/util/
    jackrabbit/branches/JCR-2936/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/util/CachingOpsPerSecondDto.java
  (with props)

Added: jackrabbit/branches/JCR-2936/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/CoreStatManager.java
URL: http://svn.apache.org/viewvc/jackrabbit/branches/JCR-2936/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/CoreStatManager.java?rev=1160584&view=auto
==============================================================================
--- jackrabbit/branches/JCR-2936/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/CoreStatManager.java
(added)
+++ jackrabbit/branches/JCR-2936/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/CoreStatManager.java
Tue Aug 23 08:43:17 2011
@@ -0,0 +1,70 @@
+/*
+ * 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;
+
+import org.apache.jackrabbit.api.jmx.CoreStatManagerMBean;
+import org.apache.jackrabbit.api.stats.CoreStat;
+
+/**
+ * The CoreStatManagerMBean default implementation
+ * 
+ */
+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 reset() {
+        this.coreStat.reset();
+    }
+
+    public double getReadOpsPerSecond() {
+        return this.coreStat.getReadOpsPerSecond();
+    }
+
+    public double getWriteOpsPerSecond() {
+        return this.coreStat.getWriteOpsPerSecond();
+    }
+
+    public void resetNumberOfOperations() {
+        this.coreStat.resetNumberOfOperations();
+
+    }
+
+    public void enable() {
+        this.coreStat.setEnabled(true);
+    }
+
+    public void disable() {
+        this.coreStat.setEnabled(false);
+    }
+}

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

Added: jackrabbit/branches/JCR-2936/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/QueryStatManager.java
URL: http://svn.apache.org/viewvc/jackrabbit/branches/JCR-2936/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/QueryStatManager.java?rev=1160584&view=auto
==============================================================================
--- jackrabbit/branches/JCR-2936/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/QueryStatManager.java
(added)
+++ jackrabbit/branches/JCR-2936/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/QueryStatManager.java
Tue Aug 23 08:43:17 2011
@@ -0,0 +1,134 @@
+/*
+ * 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;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Comparator;
+
+import javax.management.openmbean.CompositeDataSupport;
+import javax.management.openmbean.CompositeType;
+import javax.management.openmbean.OpenDataException;
+import javax.management.openmbean.OpenType;
+import javax.management.openmbean.SimpleType;
+import javax.management.openmbean.TabularData;
+import javax.management.openmbean.TabularDataSupport;
+import javax.management.openmbean.TabularType;
+
+import org.apache.jackrabbit.api.jmx.QueryStatManagerMBean;
+import org.apache.jackrabbit.api.stats.QueryStat;
+import org.apache.jackrabbit.api.stats.QueryStatDto;
+import org.apache.jackrabbit.core.stats.QueryStatDtoComparator;
+
+/**
+ * The QueryStatManagerMBean default implementation
+ * 
+ */
+public class QueryStatManager implements QueryStatManagerMBean {
+
+    private final QueryStat queryStat;
+
+    private final static Comparator<QueryStatDto> comparatorRev = Collections
+            .reverseOrder(new QueryStatDtoComparator());
+
+    public QueryStatManager(final QueryStat queryStat) {
+        this.queryStat = queryStat;
+    }
+
+    public boolean isEnabled() {
+        return this.queryStat.isEnabled();
+    }
+
+    public void enable() {
+        this.queryStat.setEnabled(true);
+    }
+
+    public void disable() {
+        this.queryStat.setEnabled(false);
+    }
+
+    public void reset() {
+        this.queryStat.reset();
+    }
+
+    public int getQueueSize() {
+        return queryStat.getSlowQueriesQueueSize();
+    }
+
+    public void setQueueSize(int size) {
+        this.queryStat.setSlowQueriesQueueSize(size);
+    }
+
+    public void clearQueue() {
+        this.queryStat.clearSlowQueriesQueue();
+    }
+
+    public QueryStatDto[] getTopQueries() {
+        QueryStatDto[] top = this.queryStat.getSlowQueries();
+        Arrays.sort(top, comparatorRev);
+        for (int i = 0; i < top.length; i++) {
+            top[i].setPosition(i + 1);
+        }
+        return top;
+    }
+
+    public TabularData getQueries() {
+        TabularDataSupport tds = null;
+        try {
+            CompositeType ct = QueryStatCompositeTypeFactory.getCompositeType();
+
+            TabularType tt = new TabularType(QueryStatDto.class.getName(),
+                    "Query History", ct, QueryStatCompositeTypeFactory.index);
+            tds = new TabularDataSupport(tt);
+
+            for (QueryStatDto q : getTopQueries()) {
+                tds.put(new CompositeDataSupport(ct,
+                        QueryStatCompositeTypeFactory.names,
+                        QueryStatCompositeTypeFactory.getValues(q)));
+            }
+            return tds;
+        } catch (Exception e) {
+            e.printStackTrace();
+            return null;
+        }
+    }
+
+    private static class QueryStatCompositeTypeFactory {
+
+        private final static String[] index = { "position" };
+
+        private final static String[] names = { "position", "duration",
+                "language", "statement", "creationTime" };
+
+        private final static String[] descriptions = { "position", "duration",
+                "language", "statement", "creationTime" };
+
+        private final static OpenType[] types = { SimpleType.LONG,
+                SimpleType.LONG, SimpleType.STRING, SimpleType.STRING,
+                SimpleType.STRING };
+
+        public static CompositeType getCompositeType() throws OpenDataException {
+            return new CompositeType(QueryStat.class.getName(),
+                    QueryStat.class.getName(), names, descriptions, types);
+        }
+
+        public static Object[] getValues(QueryStatDto q) {
+            return new Object[] { q.getPosition(), q.getDuration(),
+                    q.getLanguage(), q.getStatement(), q.getCreationTime() };
+        }
+    }
+}

Propchange: jackrabbit/branches/JCR-2936/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/QueryStatManager.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: jackrabbit/branches/JCR-2936/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/CoreStatImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/branches/JCR-2936/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/CoreStatImpl.java?rev=1160584&view=auto
==============================================================================
--- jackrabbit/branches/JCR-2936/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/CoreStatImpl.java
(added)
+++ jackrabbit/branches/JCR-2936/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/CoreStatImpl.java
Tue Aug 23 08:43:17 2011
@@ -0,0 +1,103 @@
+/*
+ * 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.stats;
+
+import java.util.concurrent.atomic.AtomicLong;
+
+import org.apache.jackrabbit.api.stats.CoreStat;
+import org.apache.jackrabbit.core.stats.util.CachingOpsPerSecondDto;
+
+/**
+ * Default CoreStat impl
+ * 
+ */
+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/branches/JCR-2936/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/CoreStatImpl.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: jackrabbit/branches/JCR-2936/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/QueryStatDtoComparator.java
URL: http://svn.apache.org/viewvc/jackrabbit/branches/JCR-2936/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/QueryStatDtoComparator.java?rev=1160584&view=auto
==============================================================================
--- jackrabbit/branches/JCR-2936/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/QueryStatDtoComparator.java
(added)
+++ jackrabbit/branches/JCR-2936/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/QueryStatDtoComparator.java
Tue Aug 23 08:43:17 2011
@@ -0,0 +1,31 @@
+/*
+ * 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.stats;
+
+import java.util.Comparator;
+
+import org.apache.jackrabbit.api.stats.QueryStatDto;
+
+/**
+ * QueryStatDto comparator by duration
+ * 
+ */
+public class QueryStatDtoComparator implements Comparator<QueryStatDto> {
+    public int compare(QueryStatDto o1, QueryStatDto o2) {
+        return new Long(o1.getDuration()).compareTo(o2.getDuration());
+    }
+}

Propchange: jackrabbit/branches/JCR-2936/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/QueryStatDtoComparator.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: jackrabbit/branches/JCR-2936/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/QueryStatDtoImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/branches/JCR-2936/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/QueryStatDtoImpl.java?rev=1160584&view=auto
==============================================================================
--- jackrabbit/branches/JCR-2936/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/QueryStatDtoImpl.java
(added)
+++ jackrabbit/branches/JCR-2936/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/QueryStatDtoImpl.java
Tue Aug 23 08:43:17 2011
@@ -0,0 +1,98 @@
+/*
+ * 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.stats;
+
+import java.util.Calendar;
+import java.util.Date;
+
+import org.apache.jackrabbit.api.stats.QueryStatDto;
+
+/**
+ * Object that holds statistical info about a query.
+ * 
+ */
+public class QueryStatDtoImpl implements QueryStatDto {
+
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * lazy, computed at call time
+     */
+    private long position;
+
+    /**
+     * the time that the query was created
+     */
+    private final Date creationTime;
+
+    /**
+     * run duration
+     */
+    private final long duration;
+
+    /**
+     * query language
+     */
+    private final String language;
+
+    /**
+     * query statement
+     */
+    private final String statement;
+
+    public QueryStatDtoImpl(final String language, final String statement,
+            long duration) {
+        this.duration = duration;
+        this.language = language;
+        this.statement = statement;
+
+        Calendar c = Calendar.getInstance();
+        c.setTimeInMillis(System.currentTimeMillis() - duration);
+        this.creationTime = c.getTime();
+    }
+
+    public long getDuration() {
+        return duration;
+    }
+
+    public String getLanguage() {
+        return language;
+    }
+
+    public String getStatement() {
+        return statement;
+    }
+
+    public String getCreationTime() {
+        return creationTime.toString();
+    }
+
+    public long getPosition() {
+        return position;
+    }
+
+    public void setPosition(long position) {
+        this.position = position;
+    }
+
+    @Override
+    public String toString() {
+        return "QueryStat [creationTime=" + creationTime + ", duration="
+                + duration + ", language=" + language + ", statement="
+                + statement + "]";
+    }
+}

Propchange: jackrabbit/branches/JCR-2936/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/QueryStatDtoImpl.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: jackrabbit/branches/JCR-2936/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/QueryStatImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/branches/JCR-2936/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/QueryStatImpl.java?rev=1160584&view=auto
==============================================================================
--- jackrabbit/branches/JCR-2936/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/QueryStatImpl.java
(added)
+++ jackrabbit/branches/JCR-2936/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/QueryStatImpl.java
Tue Aug 23 08:43:17 2011
@@ -0,0 +1,97 @@
+/*
+ * 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.stats;
+
+import java.util.Comparator;
+import java.util.PriorityQueue;
+
+import org.apache.jackrabbit.api.stats.QueryStat;
+import org.apache.jackrabbit.api.stats.QueryStatDto;
+import org.apache.jackrabbit.core.stats.util.CachingOpsPerSecondDto;
+
+/**
+ * Default {@link QueryStat} implementation
+ * 
+ */
+public class QueryStatImpl implements QueryStat {
+
+    private final static Comparator<QueryStatDto> comparator = new QueryStatDtoComparator();
+
+    private int queueSize = 15;
+
+    private PriorityQueue<QueryStatDto> queries = new PriorityQueue<QueryStatDto>(
+            queueSize + 1, comparator);
+
+    private CachingOpsPerSecondDto qps = new CachingOpsPerSecondDto();
+
+    private boolean enabled = false;
+
+    public QueryStatImpl() {
+    }
+
+    public int getSlowQueriesQueueSize() {
+        return queueSize;
+    }
+
+    public synchronized void setSlowQueriesQueueSize(int size) {
+        this.queueSize = size;
+        this.queries = new PriorityQueue<QueryStatDto>(this.queueSize + 1,
+                comparator);
+    }
+
+    public boolean isEnabled() {
+        return enabled;
+    }
+
+    public void setEnabled(boolean enabled) {
+        this.enabled = enabled;
+        this.queries = new PriorityQueue<QueryStatDto>(this.queueSize + 1,
+                comparator);
+    }
+
+    public synchronized void logQuery(final String language,
+            final String statement, long duration) {
+        if (!enabled) {
+            return;
+        }
+        queries.add(new QueryStatDtoImpl(language, statement, duration));
+        if (queries.size() > queueSize) {
+            queries.remove();
+        }
+        qps.onOp(duration * 1000);
+    }
+
+    public void clearSlowQueriesQueue() {
+        this.queries.clear();
+    }
+
+    public void reset() {
+        clearSlowQueriesQueue();
+    }
+
+    public QueryStatDto[] getSlowQueries() {
+        return queries.toArray(new QueryStatDto[queries.size()]);
+    }
+
+    public double getQueriesPerSecond() {
+        return qps.getOpsPerSecond();
+    }
+
+    public double getAvgQueryTime() {
+        return qps.getOpAvgTime();
+    }
+}

Propchange: jackrabbit/branches/JCR-2936/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/QueryStatImpl.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: jackrabbit/branches/JCR-2936/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/StatManager.java
URL: http://svn.apache.org/viewvc/jackrabbit/branches/JCR-2936/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/StatManager.java?rev=1160584&view=auto
==============================================================================
--- jackrabbit/branches/JCR-2936/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/StatManager.java
(added)
+++ jackrabbit/branches/JCR-2936/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/StatManager.java
Tue Aug 23 08:43:17 2011
@@ -0,0 +1,58 @@
+/*
+ * 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.stats;
+
+import org.apache.jackrabbit.api.stats.CoreStat;
+import org.apache.jackrabbit.api.stats.QueryStat;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * StatManager represents a single entry point to the stats objects available.<br>
+ * 
+ */
+public class StatManager {
+
+    private static final Logger log = LoggerFactory
+            .getLogger(StatManager.class);
+
+    /* STATS */
+    private final QueryStat queryStat = new QueryStatImpl();
+
+    private final CoreStat coreStat = new CoreStatImpl();
+
+    public StatManager() {
+    }
+
+    public void init() {
+        coreStat.setEnabled(false);
+        queryStat.setEnabled(false);
+        log.debug("Started StatManager");
+    }
+
+    public void stop() {
+
+    }
+
+    public CoreStat getCoreStat() {
+        return coreStat;
+    }
+
+    public QueryStat getQueryStat() {
+        return queryStat;
+    }
+}

Propchange: jackrabbit/branches/JCR-2936/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/StatManager.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: jackrabbit/branches/JCR-2936/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/util/CachingOpsPerSecondDto.java
URL: http://svn.apache.org/viewvc/jackrabbit/branches/JCR-2936/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/util/CachingOpsPerSecondDto.java?rev=1160584&view=auto
==============================================================================
--- jackrabbit/branches/JCR-2936/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/util/CachingOpsPerSecondDto.java
(added)
+++ jackrabbit/branches/JCR-2936/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/util/CachingOpsPerSecondDto.java
Tue Aug 23 08:43:17 2011
@@ -0,0 +1,152 @@
+/*
+ * 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.stats.util;
+
+import java.math.BigDecimal;
+import java.math.MathContext;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+/**
+ * Statistical data structure, use to compute stats on operations
+ * 
+ */
+public class CachingOpsPerSecondDto {
+
+    // @ 1 min
+    public static long DEFAULT_UPDATE_FREQ_MS = 1000 * 60 * 1;
+
+    private final long updateFreqMs;
+
+    private final ReadWriteLock rwl = new ReentrantReadWriteLock();
+    private final Lock r = rwl.readLock();
+    private final Lock w = rwl.writeLock();
+
+    // intermediary values
+
+    private long lastUpdate = System.currentTimeMillis();
+
+    private long startMs = System.currentTimeMillis();
+
+    private long operations = 0;
+
+    private long totalTimeNs = 0;
+
+    // cached stats
+
+    private double opsPerSecond = 0;
+
+    private double opAvgTime = 0;
+
+    public CachingOpsPerSecondDto(long updateFreqMs) {
+        this.updateFreqMs = updateFreqMs;
+    }
+
+    public CachingOpsPerSecondDto() {
+        this(DEFAULT_UPDATE_FREQ_MS);
+    }
+
+    public void onOp(long timeNs) {
+        w.lock();
+        try {
+            final long localStart = System.currentTimeMillis() - timeNs / 1000;
+            if (localStart < startMs) {
+                startMs = localStart;
+            }
+            operations++;
+            totalTimeNs += timeNs;
+        } finally {
+            w.unlock();
+        }
+    }
+
+    public double getOpsPerSecond() {
+        checkUpdate(false);
+        return opsPerSecond;
+    }
+
+    public double getOpAvgTime() {
+        checkUpdate(false);
+        return opAvgTime;
+    }
+
+    private void checkUpdate(boolean forceUpdate) {
+        r.lock();
+        final long now = System.currentTimeMillis();
+        try {
+            if (!forceUpdate && now - lastUpdate < updateFreqMs) {
+                return;
+            }
+        } finally {
+            r.unlock();
+        }
+        w.lock();
+        try {
+            if (!forceUpdate && now - lastUpdate < updateFreqMs) {
+                return;
+            }
+            update(now);
+        } finally {
+            w.unlock();
+        }
+    }
+
+    private final static BigDecimal thousand = BigDecimal.valueOf(1000);
+
+    private final static MathContext DEFAULT_CONTEXT = new MathContext(3);
+
+    private void update(long now) {
+        if (operations == 0) {
+            opsPerSecond = 0;
+            opAvgTime = 0;
+            return;
+        }
+        long durationMs = now - startMs;
+        if (durationMs == 0) {
+            durationMs = 1000;
+        }
+        opsPerSecond = BigDecimal.valueOf(operations).multiply(thousand)
+                .divide(BigDecimal.valueOf(durationMs), DEFAULT_CONTEXT)
+                .doubleValue();
+        opAvgTime = BigDecimal.valueOf(totalTimeNs)
+                .divide(BigDecimal.valueOf(operations), DEFAULT_CONTEXT)
+                .doubleValue();
+        // reset if needed
+        if (operations > Long.MAX_VALUE - 5000) {
+            reset();
+        }
+    }
+
+    public void reset() {
+        w.lock();
+        try {
+            opsPerSecond = 0;
+            opAvgTime = 0;
+            lastUpdate = System.currentTimeMillis();
+            operations = 0;
+            startMs = lastUpdate;
+            totalTimeNs = 0;
+        } finally {
+            w.unlock();
+        }
+    }
+
+    protected void refresh() {
+        checkUpdate(true);
+    }
+}

Propchange: jackrabbit/branches/JCR-2936/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/util/CachingOpsPerSecondDto.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message