public
Last active

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 the handler retrieval and then iteration.

  • Download Gist
4_thread_summary_result.log
1 2 3 4 5 6 7 8 9 10 11
[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
EventsPerformanceTest.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
/**
* 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;
 
}
ServiceTrackerCollection.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
/**
* 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;
 
}
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.