Author: ieb Date: Fri Mar 1 07:13:44 2013 New Revision: 1451503 URL: http://svn.apache.org/r1451503 Log: Replaced the Log based implementation of statistics monitoring with a simple service based on atomic longs.
Added: sling/whiteboard/ieb/monitor/ sling/whiteboard/ieb/monitor/pom.xml (contents, props changed) - copied, changed from r1450722, sling/whiteboard/ieb/log/pom.xml sling/whiteboard/ieb/monitor/src/ sling/whiteboard/ieb/monitor/src/main/ sling/whiteboard/ieb/monitor/src/main/java/ sling/whiteboard/ieb/monitor/src/main/java/org/ sling/whiteboard/ieb/monitor/src/main/java/org/apache/ sling/whiteboard/ieb/monitor/src/main/java/org/apache/sling/ sling/whiteboard/ieb/monitor/src/main/java/org/apache/sling/commons/ sling/whiteboard/ieb/monitor/src/main/java/org/apache/sling/commons/monitor/ sling/whiteboard/ieb/monitor/src/main/java/org/apache/sling/commons/monitor/Statistics.java (with props) sling/whiteboard/ieb/monitor/src/main/java/org/apache/sling/commons/monitor/StatisticsFactory.java (with props) sling/whiteboard/ieb/monitor/src/main/java/org/apache/sling/commons/monitor/impl/ sling/whiteboard/ieb/monitor/src/main/java/org/apache/sling/commons/monitor/impl/StatisticsImpl.java (with props) sling/whiteboard/ieb/monitor/src/main/java/org/apache/sling/commons/monitor/impl/StatisticsServlet.java (contents, props changed) - copied, changed from r1450722, sling/whiteboard/ieb/log/src/main/java/org/apache/sling/commons/log/internal/monitor/CounterServlet.java sling/whiteboard/ieb/monitor/src/main/java/org/apache/sling/commons/monitor/package-info.java (with props) sling/whiteboard/ieb/monitor/src/test/ sling/whiteboard/ieb/monitor/src/test/java/ sling/whiteboard/ieb/monitor/src/test/java/org/ sling/whiteboard/ieb/monitor/src/test/java/org/apache/ sling/whiteboard/ieb/monitor/src/test/java/org/apache/sling/ sling/whiteboard/ieb/monitor/src/test/java/org/apache/sling/commons/ sling/whiteboard/ieb/monitor/src/test/java/org/apache/sling/commons/monitor/ sling/whiteboard/ieb/monitor/src/test/java/org/apache/sling/commons/monitor/impl/ sling/whiteboard/ieb/monitor/src/test/java/org/apache/sling/commons/monitor/impl/CounterTest.java (contents, props changed) - copied, changed from r1450722, sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/monitor/CounterTest.java sling/whiteboard/ieb/monitor/src/test/java/org/apache/sling/commons/monitor/impl/StatisticsServletTest.java (with props) Removed: sling/whiteboard/ieb/log/README.txt sling/whiteboard/ieb/log/pom.xml sling/whiteboard/ieb/log/src/main/java/org/apache/sling/commons/log/internal/Activator.java sling/whiteboard/ieb/log/src/main/java/org/apache/sling/commons/log/internal/LogManager.java sling/whiteboard/ieb/log/src/main/java/org/apache/sling/commons/log/internal/config/ConfigurationException.java sling/whiteboard/ieb/log/src/main/java/org/apache/sling/commons/log/internal/config/ConfigurationServiceFactory.java sling/whiteboard/ieb/log/src/main/java/org/apache/sling/commons/log/internal/config/GlobalConfigurator.java sling/whiteboard/ieb/log/src/main/java/org/apache/sling/commons/log/internal/config/LogConfigurator.java sling/whiteboard/ieb/log/src/main/java/org/apache/sling/commons/log/internal/config/LogWriterManagedServiceFactory.java sling/whiteboard/ieb/log/src/main/java/org/apache/sling/commons/log/internal/config/LoggerManagedServiceFactory.java sling/whiteboard/ieb/log/src/main/java/org/apache/sling/commons/log/internal/monitor/Counter.java sling/whiteboard/ieb/log/src/main/java/org/apache/sling/commons/log/internal/monitor/CounterServlet.java sling/whiteboard/ieb/log/src/main/java/org/apache/sling/commons/log/internal/slf4j/FileRotator.java sling/whiteboard/ieb/log/src/main/java/org/apache/sling/commons/log/internal/slf4j/LogConfigManager.java sling/whiteboard/ieb/log/src/main/java/org/apache/sling/commons/log/internal/slf4j/PrivilegedWriter.java sling/whiteboard/ieb/log/src/main/java/org/apache/sling/commons/log/internal/slf4j/ScheduledFileRotator.java sling/whiteboard/ieb/log/src/main/java/org/apache/sling/commons/log/internal/slf4j/SizeLimitedFileRotator.java sling/whiteboard/ieb/log/src/main/java/org/apache/sling/commons/log/internal/slf4j/SlingConfigurationPrinter.java sling/whiteboard/ieb/log/src/main/java/org/apache/sling/commons/log/internal/slf4j/SlingLogPanel.java sling/whiteboard/ieb/log/src/main/java/org/apache/sling/commons/log/internal/slf4j/SlingLogger.java sling/whiteboard/ieb/log/src/main/java/org/apache/sling/commons/log/internal/slf4j/SlingLoggerConfig.java sling/whiteboard/ieb/log/src/main/java/org/apache/sling/commons/log/internal/slf4j/SlingLoggerLevel.java sling/whiteboard/ieb/log/src/main/java/org/apache/sling/commons/log/internal/slf4j/SlingLoggerWriter.java sling/whiteboard/ieb/log/src/main/java/org/slf4j/impl/StaticLoggerBinder.java sling/whiteboard/ieb/log/src/main/java/org/slf4j/impl/StaticMarkerBinder.java sling/whiteboard/ieb/log/src/main/resources/OSGI-INF/metatype/metatype.properties sling/whiteboard/ieb/log/src/main/resources/OSGI-INF/metatype/metatype.xml sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/ActivatorTest.java sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/config/AbstractSlingConfigTest.java sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/config/GlobalConfigurationTest.java sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/config/LogWriterManagedServiceFactoryTest.java sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/monitor/CounterTest.java sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/AbstractSlingLogTest.java sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/PrivilegedWriterTest.java sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SizeLimitedFileRotatorTest.java sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingConfigurationPrinterTest.java sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingLogPanelTest.java sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingLogWriterTest.java sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingLoggerLevelTest.java sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingLoggerTest.java Copied: sling/whiteboard/ieb/monitor/pom.xml (from r1450722, sling/whiteboard/ieb/log/pom.xml) URL: http://svn.apache.org/viewvc/sling/whiteboard/ieb/monitor/pom.xml?p2=sling/whiteboard/ieb/monitor/pom.xml&p1=sling/whiteboard/ieb/log/pom.xml&r1=1450722&r2=1451503&rev=1451503&view=diff ============================================================================== --- sling/whiteboard/ieb/log/pom.xml (original) +++ sling/whiteboard/ieb/monitor/pom.xml Fri Mar 1 07:13:44 2013 @@ -27,121 +27,74 @@ <relativePath>../../../parent/pom.xml</relativePath> </parent> - <artifactId>org.apache.sling.commons.log</artifactId> - <version>3.0.1-SNAPSHOT</version> + <artifactId>org.apache.sling.commons.monitor</artifactId> + <version>1.0.0-SNAPSHOT</version> <packaging>bundle</packaging> <name>Apache Sling SLF4J Implementation</name> <description> - This bundle implements the SLF4J API with support for dynamic - configuration through OSGi Configuration Admin Service. + This bundle provides a monitoring service to allow components to record statistics, which may be queried by clients. </description> <scm> - <connection>scm:svn:http://svn.apache.org/repos/asf/sling/trunk/bundles/commons/log</connection> - <developerConnection>scm:svn:https://svn.apache.org/repos/asf/sling/trunk/bundles/commons/log</developerConnection> - <url>http://svn.apache.org/viewvc/sling/trunk/bundles/commons/log</url> + <connection>scm:svn:http://svn.apache.org/repos/asf/sling/whiteboard/ieb/monitor</connection> + <developerConnection>scm:svn:https://svn.apache.org/repos/asf/sling/whiteboard/ieb/monitor</developerConnection> + <url>http://svn.apache.org/viewvc/sling/whiteboard/ieb/monitor</url> </scm> - <properties> - <slf4j.version>1.6.4</slf4j.version> - </properties> <build> <plugins> <plugin> <groupId>org.apache.felix</groupId> + <artifactId>maven-scr-plugin</artifactId> + </plugin> + <plugin> + <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> - <version>2.3.6</version> <extensions>true</extensions> <configuration> <instructions> - <Bundle-Activator> - org.apache.sling.commons.log.internal.Activator - </Bundle-Activator> - <Bundle-DocURL> - http://sling.apache.org/site/logging.html - </Bundle-DocURL> - <Export-Package> - org.slf4j.impl;version=${slf4j.version} - </Export-Package> - <Private-Package> - org.apache.sling.commons.log.*, - </Private-Package> <Import-Package> - !org.slf4j.impl, - org.osgi.framework;version=1.3, - org.slf4j;provide:=true, - org.slf4j.spi;provide:=true, - * + * </Import-Package> - <DynamicImport-Package> - org.osgi.service.cm;version=1.2, - javax.servlet;javax.servlet.http;version=2.3 - </DynamicImport-Package> - <Embed-Dependency> - jul-to-slf4j;inline="org/slf4j/bridge/SLF4JBridgeHandler.class" - </Embed-Dependency> </instructions> - </configuration> + </configuration> </plugin> </plugins> </build> - <reporting> - <plugins> - <plugin> - <groupId>org.apache.maven.plugins</groupId> - <artifactId>maven-javadoc-plugin</artifactId> - <configuration> - <!-- No javadocs at all --> - <skip>true</skip> - </configuration> - </plugin> - </plugins> - </reporting> - <dependencies> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> - <version>${slf4j.version}</version> <scope>provided</scope> </dependency> - <dependency> - <groupId>org.slf4j</groupId> - <artifactId>jul-to-slf4j</artifactId> - <version>${slf4j.version}</version> - <scope>compile</scope> - <optional>true</optional> - </dependency> <!-- OSGi Libraries not included here --> <dependency> <groupId>org.osgi</groupId> <artifactId>org.osgi.core</artifactId> - <version>4.0.0</version> </dependency> <dependency> <groupId>org.osgi</groupId> <artifactId>org.osgi.compendium</artifactId> - <version>4.0.0</version> </dependency> <!-- servlet API for the web console plugin --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> - <version>2.3</version> </dependency> - - <!-- OSGi Command Line Shell support --> <dependency> <groupId>org.apache.felix</groupId> - <artifactId>org.apache.felix.gogo.runtime</artifactId> - <version>0.6.1</version> - <scope>provided</scope> + <artifactId>org.apache.felix.scr.annotations</artifactId> + </dependency> + <dependency> + <groupId>biz.aQute</groupId> + <artifactId>bndlib</artifactId> </dependency> + <!-- testing --> <!-- using mockito because its a bit more relaxed and makes it easier to maintain the test cases if dependencies change --> @@ -152,6 +105,17 @@ <scope>test</scope> </dependency> <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-simple</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.sling</groupId> + <artifactId>org.apache.sling.commons.json</artifactId> + <version>2.0.6</version> + <scope>test</scope> + </dependency> + <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> Propchange: sling/whiteboard/ieb/monitor/pom.xml ------------------------------------------------------------------------------ svn:eol-style = native Propchange: sling/whiteboard/ieb/monitor/pom.xml ------------------------------------------------------------------------------ svn:mime-type = text/xml Added: sling/whiteboard/ieb/monitor/src/main/java/org/apache/sling/commons/monitor/Statistics.java URL: http://svn.apache.org/viewvc/sling/whiteboard/ieb/monitor/src/main/java/org/apache/sling/commons/monitor/Statistics.java?rev=1451503&view=auto ============================================================================== --- sling/whiteboard/ieb/monitor/src/main/java/org/apache/sling/commons/monitor/Statistics.java (added) +++ sling/whiteboard/ieb/monitor/src/main/java/org/apache/sling/commons/monitor/Statistics.java Fri Mar 1 07:13:44 2013 @@ -0,0 +1,37 @@ +/* + * 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 SF 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.sling.commons.monitor; + +import java.util.Map; +import java.util.concurrent.atomic.AtomicLong; + +/** + * A Collection of statistics registered by the user of the interface. + */ +public interface Statistics { + /** + * Get a statistic by name of type T. Type is not checked. + * @param name the name of the statistic + * @return return the statistic. + */ + <T> AtomicLong get(String name); + /** + * @return an read only map of all statistics at the time the call method was invoked. + */ + Map<String, AtomicLong> all(); +} Propchange: sling/whiteboard/ieb/monitor/src/main/java/org/apache/sling/commons/monitor/Statistics.java ------------------------------------------------------------------------------ svn:eol-style = native Added: sling/whiteboard/ieb/monitor/src/main/java/org/apache/sling/commons/monitor/StatisticsFactory.java URL: http://svn.apache.org/viewvc/sling/whiteboard/ieb/monitor/src/main/java/org/apache/sling/commons/monitor/StatisticsFactory.java?rev=1451503&view=auto ============================================================================== --- sling/whiteboard/ieb/monitor/src/main/java/org/apache/sling/commons/monitor/StatisticsFactory.java (added) +++ sling/whiteboard/ieb/monitor/src/main/java/org/apache/sling/commons/monitor/StatisticsFactory.java Fri Mar 1 07:13:44 2013 @@ -0,0 +1,40 @@ +/* + * 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 SF 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.sling.commons.monitor; + +import org.apache.sling.commons.monitor.impl.StatisticsImpl; + + +/** + * A statistics factory that manages a singleton collection of statistics. + */ +public abstract class StatisticsFactory { + + public static final Statistics statisticsSingleton = new StatisticsImpl(); + + private StatisticsFactory() { + // cant create a factory class. + } + /** + * @return the singleton instance of the collection of statistics. + */ + public static Statistics instance() { + return statisticsSingleton; + } + +} Propchange: sling/whiteboard/ieb/monitor/src/main/java/org/apache/sling/commons/monitor/StatisticsFactory.java ------------------------------------------------------------------------------ svn:eol-style = native Added: sling/whiteboard/ieb/monitor/src/main/java/org/apache/sling/commons/monitor/impl/StatisticsImpl.java URL: http://svn.apache.org/viewvc/sling/whiteboard/ieb/monitor/src/main/java/org/apache/sling/commons/monitor/impl/StatisticsImpl.java?rev=1451503&view=auto ============================================================================== --- sling/whiteboard/ieb/monitor/src/main/java/org/apache/sling/commons/monitor/impl/StatisticsImpl.java (added) +++ sling/whiteboard/ieb/monitor/src/main/java/org/apache/sling/commons/monitor/impl/StatisticsImpl.java Fri Mar 1 07:13:44 2013 @@ -0,0 +1,83 @@ +/* + * 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 SF 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.sling.commons.monitor.impl; + +import java.util.Collections; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicLong; + +import org.apache.sling.commons.monitor.Statistics; + +/** + * Simple implementation using a ConcurrentHashMap to store the statistics. + */ +public class StatisticsImpl implements Statistics { + + + // setup for 100 counters initially. + private static final int NCOUNTERS = 100; + // not likely to meet a machine with > 32 cores and see all trying to + // update a counter within the same few ms. + private static final int CONCURRENCY = 32; + // not expecting much map growth, so up the occupancy factor a bit. + private float LOAD_FACTOR = 0.80F; + + private Map<String, AtomicLong> statisticsMap = new ConcurrentHashMap<String, AtomicLong>(NCOUNTERS, LOAD_FACTOR, CONCURRENCY); + private int lastSize = 100; + + + public AtomicLong get(String name) { + AtomicLong statistic = statisticsMap.get(name); + if ( statistic == null ) { + statistic = new AtomicLong(); + statisticsMap.put(name,statistic); + } + return statistic; + } + + public Map<String, AtomicLong> all() { + return Collections.unmodifiableMap(statisticsMap); + } + + + public String toJson() { + // create a string buffer every time a bit larger than the last time + // Each response is new as _timenanos must be updated. + StringBuilder sb = new StringBuilder(lastSize + 10); + sb.append("{"); + Set<Entry<String, AtomicLong>> es = statisticsMap.entrySet(); + sb.append("\"_timenanos\":").append(System.nanoTime()); + for (Entry<String, AtomicLong> e : es) { + sb.append(",\"").append(e.getKey()).append("\":").append(e.getValue().get()); + } + sb.append("}"); + // save the last size so next time we can make the StringBuffer about + // the right size. + lastSize = sb.length(); + return sb.toString(); + } + + protected void clear() { + statisticsMap.clear(); + } + + +} Propchange: sling/whiteboard/ieb/monitor/src/main/java/org/apache/sling/commons/monitor/impl/StatisticsImpl.java ------------------------------------------------------------------------------ svn:eol-style = native Copied: sling/whiteboard/ieb/monitor/src/main/java/org/apache/sling/commons/monitor/impl/StatisticsServlet.java (from r1450722, sling/whiteboard/ieb/log/src/main/java/org/apache/sling/commons/log/internal/monitor/CounterServlet.java) URL: http://svn.apache.org/viewvc/sling/whiteboard/ieb/monitor/src/main/java/org/apache/sling/commons/monitor/impl/StatisticsServlet.java?p2=sling/whiteboard/ieb/monitor/src/main/java/org/apache/sling/commons/monitor/impl/StatisticsServlet.java&p1=sling/whiteboard/ieb/log/src/main/java/org/apache/sling/commons/log/internal/monitor/CounterServlet.java&r1=1450722&r2=1451503&rev=1451503&view=diff ============================================================================== --- sling/whiteboard/ieb/log/src/main/java/org/apache/sling/commons/log/internal/monitor/CounterServlet.java (original) +++ sling/whiteboard/ieb/monitor/src/main/java/org/apache/sling/commons/monitor/impl/StatisticsServlet.java Fri Mar 1 07:13:44 2013 @@ -15,7 +15,7 @@ * KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ -package org.apache.sling.commons.log.internal.monitor; +package org.apache.sling.commons.monitor.impl; import java.io.IOException; @@ -24,19 +24,26 @@ import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.apache.felix.scr.annotations.sling.SlingServlet; +import org.apache.sling.commons.monitor.StatisticsFactory; -public class CounterServlet extends HttpServlet { +@SlingServlet(methods={"GET"},paths={"/system/stats"}) +public class StatisticsServlet extends HttpServlet { + + /** + * + */ + private static final long serialVersionUID = 6822640315530691809L; /** * */ - private static final long serialVersionUID = 4824982332082956436L; + private StatisticsImpl statisticsImpl = (StatisticsImpl) StatisticsFactory.instance(); @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - - String response = Counter.getInstance().toJson(); + String response = statisticsImpl.toJson(); resp.setContentType("application/json; charset=utf-8"); resp.setHeader("Cache-control", "no-cache"); resp.setContentLength(response.length()); Propchange: sling/whiteboard/ieb/monitor/src/main/java/org/apache/sling/commons/monitor/impl/StatisticsServlet.java ------------------------------------------------------------------------------ svn:eol-style = native Added: sling/whiteboard/ieb/monitor/src/main/java/org/apache/sling/commons/monitor/package-info.java URL: http://svn.apache.org/viewvc/sling/whiteboard/ieb/monitor/src/main/java/org/apache/sling/commons/monitor/package-info.java?rev=1451503&view=auto ============================================================================== --- sling/whiteboard/ieb/monitor/src/main/java/org/apache/sling/commons/monitor/package-info.java (added) +++ sling/whiteboard/ieb/monitor/src/main/java/org/apache/sling/commons/monitor/package-info.java Fri Mar 1 07:13:44 2013 @@ -0,0 +1,22 @@ +/* + * 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 SF 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. + */ + +@Version("1.0.0") +package org.apache.sling.commons.monitor; + +import aQute.bnd.annotation.Version; Propchange: sling/whiteboard/ieb/monitor/src/main/java/org/apache/sling/commons/monitor/package-info.java ------------------------------------------------------------------------------ svn:eol-style = native Copied: sling/whiteboard/ieb/monitor/src/test/java/org/apache/sling/commons/monitor/impl/CounterTest.java (from r1450722, sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/monitor/CounterTest.java) URL: http://svn.apache.org/viewvc/sling/whiteboard/ieb/monitor/src/test/java/org/apache/sling/commons/monitor/impl/CounterTest.java?p2=sling/whiteboard/ieb/monitor/src/test/java/org/apache/sling/commons/monitor/impl/CounterTest.java&p1=sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/monitor/CounterTest.java&r1=1450722&r2=1451503&rev=1451503&view=diff ============================================================================== --- sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/monitor/CounterTest.java (original) +++ sling/whiteboard/ieb/monitor/src/test/java/org/apache/sling/commons/monitor/impl/CounterTest.java Fri Mar 1 07:13:44 2013 @@ -15,14 +15,12 @@ * KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ -package org.apache.sling.commons.log.internal.monitor; +package org.apache.sling.commons.monitor.impl; import java.lang.management.ManagementFactory; import java.lang.management.ThreadInfo; -import java.lang.management.ThreadMXBean; import java.text.MessageFormat; import java.util.concurrent.atomic.AtomicInteger; -import java.util.concurrent.atomic.AtomicLong; import javax.management.Attribute; import javax.management.AttributeNotFoundException; @@ -30,7 +28,6 @@ import javax.management.InstanceNotFound import javax.management.InvalidAttributeValueException; import javax.management.MBeanException; import javax.management.MBeanServer; -import javax.management.MBeanServerFactory; import javax.management.MalformedObjectNameException; import javax.management.ObjectName; import javax.management.ReflectionException; @@ -38,70 +35,46 @@ import javax.management.openmbean.Compos import junit.framework.Assert; -import org.apache.sling.commons.log.internal.Activator.DummyLogManagerConfiguration; +import org.apache.sling.commons.monitor.Statistics; +import org.apache.sling.commons.monitor.StatisticsFactory; import org.junit.Test; -import org.osgi.service.cm.ManagedServiceFactory; -import org.slf4j.Marker; -import org.slf4j.MarkerFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class CounterTest { + + private static final Logger LOGGER = LoggerFactory.getLogger(CounterTest.class); private long lastBlockedTime; private long lastBlockedCount; private long lastCheck; - /** - * Give a very rough idea of performance single threaded. NB: This is a - * totally unscientific benchmark. - */ - @Test - public void testCounter() { - Marker m = MarkerFactory.getMarker("count"); - Counter counter = Counter.getInstance(); - int loops = 100000; - int counters = 100; - long ncounts = loops * counters; - // attemt to warm up hotspot by causing the code to be executed > 10k - // times. - for (int j = 0; j < loops + 1; j++) { - for (int i = 0; i < counters + 1; i++) { - counter.increment(m, "cou\"nter" + i); - } - } - - long l = System.nanoTime(); - for (int j = 0; j < loops; j++) { - for (int i = 0; i < counters; i++) { - counter.increment(m, "cou\"nter" + i); - } - } - l = System.nanoTime() - l; - System.err.println("Time Taken for 1 count operation is " - + (l / ncounts) + " nanoseconds"); - String json = counter.toJson(); - Assert.assertNotNull(json); - } @Test - public void testCounterMultiThreaded() throws InterruptedException, + public void testLongCounterMultiThreaded() throws InterruptedException, MalformedObjectNameException, InstanceNotFoundException, NullPointerException, ReflectionException, MBeanException, InvalidAttributeValueException, AttributeNotFoundException { - doMulti(Runtime.getRuntime().availableProcessors(), 10000000, 100, Runtime.getRuntime().availableProcessors()); + Statistics statistics = (StatisticsImpl) StatisticsFactory.instance(); + int counters = 100; + doMulti(Runtime.getRuntime().availableProcessors(), 100000, counters, Runtime.getRuntime().availableProcessors()); + String json = ((StatisticsImpl) statistics).toJson(); + Assert.assertNotNull(json); + LOGGER.info(json); } - public void doMulti(int threads, final int loops, final int counters, + + public <T> void doMulti(int threads, final int loops, final int counters, int cores) throws InterruptedException, MalformedObjectNameException, InstanceNotFoundException, NullPointerException, ReflectionException, MBeanException, InvalidAttributeValueException, AttributeNotFoundException { - final Marker m = MarkerFactory.getMarker("count"); - final Counter counter = Counter.getInstance(); + final Statistics statistics = StatisticsFactory.instance(); // attemt to warm up hotspot by causing the code to be executed > 10k // times. for (int j = 0; j < 10000 + 1; j++) { for (int i = 0; i < counters + 1; i++) { - counter.increment(m, "cou\"nter" + i); - } + statistics.get("counter"+i).incrementAndGet(); + } } final int ncounts = loops * counters; final int tcounts = ncounts * threads; @@ -116,7 +89,7 @@ public class CounterTest { l[threadn] = System.nanoTime(); for (int j = 0; j < loops; j++) { for (int i = 0; i < counters; i++) { - counter.increment(m, "cou\"nter" + i); + statistics.get("counter"+i).incrementAndGet(); } } l[threadn] = System.nanoTime() - l[threadn]; @@ -133,7 +106,7 @@ public class CounterTest { while (running.get() > 0) { dumpThreadWaits(mbeanServer); - Thread.sleep(10000); + Thread.sleep(1000); } for (int i = 0; i < t.length; i++) { @@ -149,7 +122,7 @@ public class CounterTest { // this number should remain constant if no threads block other threads // and both cores on the box are // fully utilized. - System.err.println("Time Taken for 1 count operation over " + t.length + LOGGER.info("Time Taken for 1 count operation over " + t.length + " threads is " + (overall / tcounts) + " ns "); } @@ -189,7 +162,7 @@ public class CounterTest { lastBlockedTime = blockedTime; lastBlockedCount = blockedCount; lastCheck = System.currentTimeMillis(); - System.err.println(MessageFormat.format("Total Blocked time over all threads {0,number} ms in {1,number} ms threads blocked for {2,number,#.###}% of the time or on average {3,number,#.##} ms ",deltaTime,deltaRealTime,((double)deltaTime*100)/((double)deltaRealTime),((deltaCount==0)?0:((double)deltaTime)/((double)deltaCount)))); + LOGGER.info(MessageFormat.format("Total Blocked time over all threads {0,number} ms in {1,number} ms threads blocked for {2,number,#.###}% of the time or on average {3,number,#.##} ms ",deltaTime,deltaRealTime,((double)deltaTime*100)/((double)deltaRealTime),((deltaCount==0)?0:((double)deltaTime)/((double)deltaCount)))); } } Propchange: sling/whiteboard/ieb/monitor/src/test/java/org/apache/sling/commons/monitor/impl/CounterTest.java ------------------------------------------------------------------------------ svn:eol-style = native Added: sling/whiteboard/ieb/monitor/src/test/java/org/apache/sling/commons/monitor/impl/StatisticsServletTest.java URL: http://svn.apache.org/viewvc/sling/whiteboard/ieb/monitor/src/test/java/org/apache/sling/commons/monitor/impl/StatisticsServletTest.java?rev=1451503&view=auto ============================================================================== --- sling/whiteboard/ieb/monitor/src/test/java/org/apache/sling/commons/monitor/impl/StatisticsServletTest.java (added) +++ sling/whiteboard/ieb/monitor/src/test/java/org/apache/sling/commons/monitor/impl/StatisticsServletTest.java Fri Mar 1 07:13:44 2013 @@ -0,0 +1,81 @@ +/* + * 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 SF 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.sling.commons.monitor.impl; + +import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.io.Writer; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import junit.framework.Assert; + +import org.apache.sling.commons.json.JSONException; +import org.apache.sling.commons.json.JSONObject; +import org.apache.sling.commons.monitor.Statistics; +import org.apache.sling.commons.monitor.StatisticsFactory; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Mockito; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class StatisticsServletTest { + + private static final Logger LOGGER = LoggerFactory.getLogger(StatisticsServletTest.class); + + @Test + public void testGet() throws ServletException, IOException, JSONException { + StatisticsServlet statisticsServlet = new StatisticsServlet(); + + Statistics statistics = StatisticsFactory.instance(); + statistics.get("counter").incrementAndGet(); + statistics.get("setvalue").set(System.currentTimeMillis()); + + + HttpServletRequest request = Mockito.mock(HttpServletRequest.class); + HttpServletResponse response = Mockito.mock(HttpServletResponse.class); + StringWriter stringWriter = new StringWriter(); + PrintWriter printer = new PrintWriter(stringWriter); + Mockito.when(response.getWriter()).thenReturn(printer); + Mockito.when(request.getMethod()).thenReturn("GET"); + statisticsServlet.service(request, response); + Assert.assertNotNull(stringWriter.toString()); + JSONObject json = new JSONObject(stringWriter.toString()); + + LOGGER.info(json.toString(4)); + + } + + @Test + public void testPost() throws ServletException, IOException { + StatisticsServlet statisticsServlet = new StatisticsServlet(); + HttpServletRequest request = Mockito.mock(HttpServletRequest.class); + HttpServletResponse response = Mockito.mock(HttpServletResponse.class); + Mockito.when(request.getMethod()).thenReturn("POST"); + statisticsServlet.service(request, response); + ArgumentCaptor<Integer> status = ArgumentCaptor.forClass(Integer.class); + Mockito.verify(response).sendError(status.capture()); + Assert.assertEquals((Integer)HttpServletResponse.SC_METHOD_NOT_ALLOWED, status.getValue()); + + } + +} Propchange: sling/whiteboard/ieb/monitor/src/test/java/org/apache/sling/commons/monitor/impl/StatisticsServletTest.java ------------------------------------------------------------------------------ svn:eol-style = native