Skip to content

Instantly share code, notes, and snippets.

@rotty3000
Last active January 1, 2016 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rotty3000/8187520 to your computer and use it in GitHub Desktop.
Save rotty3000/8187520 to your computer and use it in GitHub Desktop.
Benchmark use case: - obtain, and execute, a collection of event handlers (registered whiteboard style) - try not to be slower than EventProcessorUtil ("eventProcessorUtil_process" case, invokes the current impl) To avoid the loop testing problems use a fixed size of 4 handlers in all tests. We're not trying to measure the loop speed itself, but…
[java] Benchmark Mode Thr Count Sec Mean Mean error Units
[java] c.l.j.p.e.EventsPerformanceTest.array thrpt 4 200 1 40868.206 68.055 ops/ms
[java] c.l.j.p.e.EventsPerformanceTest.eventProcessorUtil_process_classNames thrpt 4 200 1 16099.645 28.735 ops/ms
[java] c.l.j.p.e.EventsPerformanceTest.eventProcessorUtil_process_registered thrpt 4 200 1 32784.652 60.586 ops/ms
[java] c.l.j.p.e.EventsPerformanceTest.list thrpt 4 200 1 41045.476 82.463 ops/ms
[java] c.l.j.p.e.EventsPerformanceTest.serviceTrackerCollection thrpt 4 200 1 41143.900 69.304 ops/ms
[java] c.l.j.p.e.EventsPerformanceTest.serviceTrackerCollection_ToArray thrpt 4 200 1 35950.619 223.727 ops/ms
[java] c.l.j.p.e.EventsPerformanceTest.serviceTrackerCollection_ToArray_Typed thrpt 4 200 1 8069.174 34.354 ops/ms
[java] c.l.j.p.e.EventsPerformanceTest.serviceTracker_getServices thrpt 4 200 1 986.573 2.329 ops/ms
[java] c.l.j.p.e.EventsPerformanceTest.serviceTracker_getServices_Typed thrpt 4 200 1 824.367 1.683 ops/ms
[java] c.l.j.p.e.EventsPerformanceTest.serviceTracker_getTracked_values thrpt 4 200 1 9243.379 282.008 ops/ms
/**
* Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.jmh.portal.event;
import com.liferay.jmh.portal.event.dependency.BeanchmarkLifecycleEvent;
import com.liferay.jmh.portal.event.dependency.BenchmarkLifecycleAction;
import com.liferay.portal.bean.BeanLocatorImpl;
import com.liferay.portal.events.EventsProcessorUtil;
import com.liferay.portal.kernel.bean.BeanLocator;
import com.liferay.portal.kernel.bean.PortalBeanLocatorUtil;
import com.liferay.portal.kernel.events.ActionException;
import com.liferay.portal.kernel.events.LifecycleAction;
import com.liferay.portal.kernel.events.LifecycleEvent;
import com.liferay.portal.kernel.util.SystemProperties;
import com.liferay.portal.module.framework.ModuleFrameworkUtilAdapter;
import com.liferay.portal.spring.util.SpringUtil;
import com.liferay.portal.util.InitUtil;
import com.liferay.portal.util.PropsValues;
import com.liferay.registry.Filter;
import com.liferay.registry.Registry;
import com.liferay.registry.RegistryUtil;
import com.liferay.registry.ServiceTracker;
import com.liferay.registry.ServiceTrackerCollection;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.TearDown;
/**
* @author Raymond Augé
*/
@State(Scope.Benchmark)
public class EventsPerformanceTest {
@GenerateMicroBenchmark
public LifecycleEvent array() throws ActionException {
BeanchmarkLifecycleEvent lifecycleEvent =
new BeanchmarkLifecycleEvent();
for (LifecycleAction lifecycleAction : _array) {
lifecycleAction.processEvent(lifecycleEvent);
}
assert lifecycleEvent.counter.get() == 4;
return lifecycleEvent;
}
@GenerateMicroBenchmark
public LifecycleEvent eventProcessorUtil_process_classNames()
throws ActionException {
BeanchmarkLifecycleEvent lifecycleEvent =
new BeanchmarkLifecycleEvent();
EventsProcessorUtil.process(null, _EVENT_CLASSNAMES_B, lifecycleEvent);
assert lifecycleEvent.counter.get() == 4;
return lifecycleEvent;
}
@GenerateMicroBenchmark
public LifecycleEvent eventProcessorUtil_process_registered()
throws ActionException {
BeanchmarkLifecycleEvent lifecycleEvent =
new BeanchmarkLifecycleEvent();
EventsProcessorUtil.process(
"a.key", _EVENT_CLASSNAMES_A, lifecycleEvent);
assert lifecycleEvent.counter.get() == 4;
return lifecycleEvent;
}
@GenerateMicroBenchmark
public LifecycleEvent list() throws ActionException {
BeanchmarkLifecycleEvent lifecycleEvent =
new BeanchmarkLifecycleEvent();
for (LifecycleAction lifecycleAction : _list) {
lifecycleAction.processEvent(lifecycleEvent);
}
assert lifecycleEvent.counter.get() == 4;
return lifecycleEvent;
}
@GenerateMicroBenchmark
public LifecycleEvent serviceTracker_getServices()
throws ActionException {
BeanchmarkLifecycleEvent lifecycleEvent =
new BeanchmarkLifecycleEvent();
for (Object service : _serviceTracker.getServices()) {
((LifecycleAction)service).processEvent(lifecycleEvent);
}
assert lifecycleEvent.counter.get() == 4;
return lifecycleEvent;
}
@GenerateMicroBenchmark
public LifecycleEvent serviceTracker_getServices_Typed()
throws ActionException {
BeanchmarkLifecycleEvent lifecycleEvent =
new BeanchmarkLifecycleEvent();
for (LifecycleAction lifecycleAction : _serviceTracker.getServices(
new LifecycleAction[_serviceTracker.size()])) {
lifecycleAction.processEvent(lifecycleEvent);
}
assert lifecycleEvent.counter.get() == 4;
return lifecycleEvent;
}
@GenerateMicroBenchmark
public LifecycleEvent serviceTracker_getTracked_values()
throws ActionException {
BeanchmarkLifecycleEvent lifecycleEvent =
new BeanchmarkLifecycleEvent();
for (LifecycleAction lifecycleAction :
_serviceTracker.getTracked().values()) {
lifecycleAction.processEvent(lifecycleEvent);
}
assert lifecycleEvent.counter.get() == 4;
return lifecycleEvent;
}
@GenerateMicroBenchmark
public LifecycleEvent serviceTrackerCollection()
throws ActionException {
BeanchmarkLifecycleEvent lifecycleEvent =
new BeanchmarkLifecycleEvent();
for (LifecycleAction lifecycleAction : _serviceTrackerCollection) {
lifecycleAction.processEvent(lifecycleEvent);
}
assert lifecycleEvent.counter.get() == 4;
return lifecycleEvent;
}
@GenerateMicroBenchmark
public LifecycleEvent serviceTrackerCollection_ToArray()
throws ActionException {
BeanchmarkLifecycleEvent lifecycleEvent =
new BeanchmarkLifecycleEvent();
for (Object object : _serviceTrackerCollection.toArray()) {
((LifecycleAction)object).processEvent(lifecycleEvent);
}
assert lifecycleEvent.counter.get() == 4;
return lifecycleEvent;
}
@GenerateMicroBenchmark
public LifecycleEvent serviceTrackerCollection_ToArray_Typed()
throws ActionException {
BeanchmarkLifecycleEvent lifecycleEvent =
new BeanchmarkLifecycleEvent();
for (LifecycleAction lifecycleAction :
_serviceTrackerCollection.toArray(
new LifecycleAction[_serviceTrackerCollection.size()])) {
lifecycleAction.processEvent(lifecycleEvent);
}
assert lifecycleEvent.counter.get() == 4;
return lifecycleEvent;
}
@SuppressWarnings("deprecation")
@Setup(Level.Trial)
public void setup() throws Exception {
System.setProperty("catalina.base", ".");
System.setProperty("external-properties", "portal-test.properties");
InitUtil.init();
PropsValues.LIFERAY_WEB_PORTAL_CONTEXT_TEMPDIR =
System.getProperty(SystemProperties.TMP_DIR);
ModuleFrameworkUtilAdapter.startFramework();
SpringUtil.loadContext(null);
BeanLocator beanLocator = PortalBeanLocatorUtil.getBeanLocator();
ModuleFrameworkUtilAdapter.registerContext(
((BeanLocatorImpl)beanLocator).getApplicationContext());
ModuleFrameworkUtilAdapter.startRuntime();
_array = new LifecycleAction[4];
_array[0] = new BenchmarkLifecycleAction();
_array[1] = new BenchmarkLifecycleAction();
_array[2] = new BenchmarkLifecycleAction();
_array[3] = new BenchmarkLifecycleAction();
_list = new ArrayList<LifecycleAction>();
_list.add(new BenchmarkLifecycleAction());
_list.add(new BenchmarkLifecycleAction());
_list.add(new BenchmarkLifecycleAction());
_list.add(new BenchmarkLifecycleAction());
EventsProcessorUtil.registerEvent(
"a.key", new BenchmarkLifecycleAction());
EventsProcessorUtil.registerEvent(
"a.key", new BenchmarkLifecycleAction());
EventsProcessorUtil.registerEvent(
"a.key", new BenchmarkLifecycleAction());
EventsProcessorUtil.registerEvent(
"a.key", new BenchmarkLifecycleAction());
String filterString = "(lifecycle.event=a.key)";
Hashtable<String,Object> map = new Hashtable<String,Object>();
map.put("lifecycle.event", "a.key");
// Registry
Registry registry = RegistryUtil.getRegistry();
registry.registerService(
LifecycleAction.class, new BenchmarkLifecycleAction(), map);
registry.registerService(
LifecycleAction.class, new BenchmarkLifecycleAction(), map);
registry.registerService(
LifecycleAction.class, new BenchmarkLifecycleAction(), map);
registry.registerService(
LifecycleAction.class, new BenchmarkLifecycleAction(), map);
Filter filter = registry.getFilter(filterString);
_serviceTrackerCollection =
new ServiceTrackerCollection<LifecycleAction>(
LifecycleAction.class, filter, map);
_serviceTracker = registry.trackServices(filter);
_serviceTracker.open();
}
@TearDown(Level.Trial)
public void tearDown() throws Exception {
BeanLocator beanLocator = PortalBeanLocatorUtil.getBeanLocator();
beanLocator.destroy();
ModuleFrameworkUtilAdapter.stopFramework();
}
private static final String[] _EVENT_CLASSNAMES_A = new String[0];
private static final String[] _EVENT_CLASSNAMES_B = new String[] {
BenchmarkLifecycleAction.class.getName(),
BenchmarkLifecycleAction.class.getName(),
BenchmarkLifecycleAction.class.getName(),
BenchmarkLifecycleAction.class.getName()
};
LifecycleAction[] _array;
List<LifecycleAction> _list;
ServiceTracker<LifecycleAction, LifecycleAction> _serviceTracker;
ServiceTrackerCollection<LifecycleAction> _serviceTrackerCollection;
}
/**
* Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.registry;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* @author Raymond Augé
*/
public class ServiceTrackerCollection <S> implements Collection<S> {
public ServiceTrackerCollection(Class<S> clazz) {
this(clazz, (Filter)null, null, new HashMap<String, Object>());
}
public ServiceTrackerCollection(Class<S> clazz, Filter filter) {
this(clazz, filter, null, new HashMap<String, Object>());
}
public ServiceTrackerCollection(
Class<S> clazz, Filter filter, Map<String, Object> properties) {
this(clazz, filter, null, properties);
}
public ServiceTrackerCollection(
Class<S> clazz, Filter filter,
ServiceTrackerCustomizer<S, S> customizer) {
this(clazz, filter, customizer, new HashMap<String, Object>());
}
@SuppressWarnings("unchecked")
public ServiceTrackerCollection(
Class<S> clazz, Filter filter,
ServiceTrackerCustomizer<S, S> customizer,
Map<String, Object> properties) {
_clazz = clazz;
_filter = filter;
_properties = Collections.unmodifiableMap(properties);
_cachedCollection = new CopyOnWriteArrayList<S>();
if (filter != null) {
_serviceTracker = getRegistry().trackServices(
filter, new CachingCustomizer(customizer));
}
else {
_serviceTracker = getRegistry().trackServices(
clazz, new CachingCustomizer(customizer));
}
_serviceTracker.open();
_serviceRegistrations =
new ConcurrentHashMap<S, ServiceRegistration<S>>();
}
public ServiceTrackerCollection(
Class<S> clazz, Map<String, Object> properties) {
this(clazz, (Filter)null, null, properties);
}
public ServiceTrackerCollection(
Class<S> clazz, ServiceTrackerCustomizer<S, S> customizer) {
this(clazz, (Filter)null, customizer, new HashMap<String, Object>());
}
public ServiceTrackerCollection(
Class<S> clazz, ServiceTrackerCustomizer<S, S> customizer,
Map<String, Object> properties) {
this(clazz, (Filter)null, customizer, properties);
}
public ServiceTrackerCollection(Class<S> clazz, String filterString) {
this(
clazz, getRegistry().getFilter(filterString), null,
new HashMap<String, Object>());
}
public ServiceTrackerCollection(
Class<S> clazz, String filterString, Map<String, Object> properties) {
this(clazz, getRegistry().getFilter(filterString), null, properties);
}
public ServiceTrackerCollection(
Class<S> clazz, String filterString,
ServiceTrackerCustomizer<S, S> customizer) {
this(
clazz, getRegistry().getFilter(filterString), customizer,
new HashMap<String, Object>());
}
public ServiceTrackerCollection(
Class<S> clazz, String filterString,
ServiceTrackerCustomizer<S, S> customizer,
Map<String, Object> properties) {
this(
clazz, getRegistry().getFilter(filterString), customizer,
properties);
}
@Override
public boolean add(S element) {
Map<String, Object> map = new HashMap<String, Object>(_properties);
if ((_filter != null) && (!_filter.matches(map))) {
return false;
}
ServiceRegistration<S> serviceRegistration =
getRegistry().registerService(_clazz, element, map);
_serviceRegistrations.put(element, serviceRegistration);
return true;
}
public boolean add(S element, Map<String, Object> properties) {
Map<String, Object> map = new HashMap<String, Object>(properties);
map.putAll(_properties);
if ((_filter != null) && (!_filter.matches(map))) {
return false;
}
ServiceRegistration<S> serviceRegistration =
getRegistry().registerService(_clazz, element, map);
_serviceRegistrations.put(element, serviceRegistration);
return true;
}
@Override
public boolean addAll(Collection<? extends S> collection) {
if (this == collection) {
return false;
}
boolean modified = false;
for (S element : collection) {
modified = add(element);
}
return modified;
}
@Override
public void clear() {
Iterator<Entry<S, ServiceRegistration<S>>> iterator =
_serviceRegistrations.entrySet().iterator();
while (iterator.hasNext()) {
Entry<S, ServiceRegistration<S>> entry = iterator.next();
entry.getValue().unregister();
iterator.remove();
}
}
@Override
public boolean contains(Object element) {
return _cachedCollection.contains(element);
}
@Override
public boolean containsAll(Collection<?> collection) {
if (this == collection) {
return true;
}
for (Object element : collection) {
if (!contains(element)) {
return false;
}
}
return true;
}
@Override
public boolean isEmpty() {
return _cachedCollection.isEmpty();
}
@Override
public Iterator<S> iterator() {
return _cachedCollection.iterator();
}
@Override
public boolean remove(Object element) {
ServiceRegistration<S> serviceRegistration =
_serviceRegistrations.remove(element);
if (serviceRegistration == null) {
return false;
}
serviceRegistration.unregister();
return true;
}
@Override
public boolean removeAll(Collection<?> collection) {
return false;
}
@Override
@SuppressWarnings("unchecked")
public boolean retainAll(Collection<?> collection) {
return false;
}
@Override
public int size() {
return _cachedCollection.size();
}
@Override
public Object[] toArray() {
return _cachedCollection.toArray();
}
@Override
@SuppressWarnings("unchecked")
public <T> T[] toArray(T[] array) {
return _cachedCollection.toArray(array);
}
private static Registry getRegistry() {
return RegistryUtil.getRegistry();
}
private final CopyOnWriteArrayList<S> _cachedCollection;
private final Class<S> _clazz;
private final Filter _filter;
private final Map<String, Object> _properties;
private final Map<S, ServiceRegistration<S>> _serviceRegistrations;
private final ServiceTracker<S, S> _serviceTracker;
private class CachingCustomizer
implements ServiceTrackerCustomizer<S, S> {
public CachingCustomizer(
ServiceTrackerCustomizer<S, S> serviceTrackerCustomizer) {
_delegate = serviceTrackerCustomizer;
}
@Override
public S addingService(ServiceReference<S> serviceReference) {
S service;
if (_delegate != null) {
service = _delegate.addingService(serviceReference);
}
else {
service = getRegistry().getService(serviceReference);
}
ServiceTrackerCollection.this._cachedCollection.add(service);
return service;
}
@Override
public void modifiedService(
ServiceReference<S> serviceReference, S service) {
if (_delegate != null) {
_delegate.modifiedService(serviceReference, service);
}
}
@Override
public void removedService(
ServiceReference<S> serviceReference, S service) {
if (_delegate != null) {
_delegate.removedService(serviceReference, service);
}
else {
getRegistry().ungetService(serviceReference);
}
ServiceTrackerCollection.this._cachedCollection.remove(service);
}
private final ServiceTrackerCustomizer<S, S> _delegate;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment