Skip to content

@szarnekow /Benchmark.java
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Caliper Benchmark for getContainerOfType
import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.xbase.XAbstractFeatureCall;
import org.eclipse.xtext.xbase.XFeatureCall;
import org.eclipse.xtext.xbase.XMemberFeatureCall;
import org.eclipse.xtext.xbase.XNullLiteral;
import org.eclipse.xtext.xbase.XUnaryOperation;
import org.eclipse.xtext.xbase.XbaseFactory;
import com.google.caliper.Param;
import com.google.caliper.Runner;
import com.google.caliper.SimpleBenchmark;
import com.google.common.collect.AbstractIterator;
import com.google.common.collect.Iterators;
import com.google.common.collect.UnmodifiableIterator;
public class ContainerByTypeBenchmark extends SimpleBenchmark {
static class Iter extends AbstractIterator<EObject> {
private EObject current;
Iter(EObject seed) {
this.current = seed;
}
@Override
protected EObject computeNext() {
if (current == null)
return endOfData();
EObject result = current;
current = current.eContainer();
return result;
}
}
enum ContainerByType {
Recursive {
@SuppressWarnings("unchecked")
@Override
<T> T getContainerOfType(EObject object, Class<T> clazz)
throws Exception {
if (clazz.isAssignableFrom(object.getClass()))
return (T) object;
if (object.eContainer() != null)
return getContainerOfType(object.eContainer(), clazz);
return null;
}
},
Iterator {
@Override
<T> T getContainerOfType(EObject object, Class<T> clazz)
throws Exception {
Iter iter = new Iter(object);
UnmodifiableIterator<T> filtered = Iterators.filter(iter, clazz);
return Iterators.getNext(filtered, null);
}
},
Loop {
@SuppressWarnings("unchecked")
@Override
<T> T getContainerOfType(EObject object, Class<T> clazz)
throws Exception {
for(EObject e = object; e != null; e = e.eContainer()) {
if (clazz.isInstance(e)) {
return (T) e;
}
}
return null;
}
};
abstract <T> T getContainerOfType(EObject object, Class<T> clazz) throws Exception;
}
@Param
private ContainerByType testCase;
@Param({ "0", "10", "100", "1000" })
private int containers;
private EObject object;
@Override
protected void setUp() throws Exception {
XNullLiteral leaf = XbaseFactory.eINSTANCE.createXNullLiteral();
XFeatureCall root = XbaseFactory.eINSTANCE.createXFeatureCall();
XMemberFeatureCall intermediate = XbaseFactory.eINSTANCE.createXMemberFeatureCall();
XAbstractFeatureCall current = root;
for(int i = 0; i < containers; i++) {
XAbstractFeatureCall between = XbaseFactory.eINSTANCE.createXBinaryOperation();
current.setImplicitReceiver(between);
current = between;
}
current.setImplicitFirstArgument(intermediate);
current = intermediate;
for(int i = 0; i < containers; i++) {
XAbstractFeatureCall between = XbaseFactory.eINSTANCE.createXBinaryOperation();
current.setImplicitFirstArgument(between);
current = between;
}
current.setImplicitReceiver(leaf);
object = leaf;
}
public int timeRoot(int count) throws Exception {
int result = 0;
for (int i = 0; i < count; i++) {
result ^= testCase.getContainerOfType(object, XFeatureCall.class).eClass().getClassifierID();
}
return result;
}
public int timeNotFound(int count) throws Exception {
int result = 0;
for (int i = 0; i < count; i++) {
if (testCase.getContainerOfType(object, XUnaryOperation.class) != null)
result++;
}
return result;
}
public int timeLeaf(int count) throws Exception {
int result = 0;
for (int i = 0; i < count; i++) {
result ^= testCase.getContainerOfType(object, XNullLiteral.class).eClass().getClassifierID();
}
return result;
}
public int timeIntermediate(int count) throws Exception {
int result = 0;
for (int i = 0; i < count; i++) {
result ^= testCase.getContainerOfType(object, XMemberFeatureCall.class).eClass().getClassifierID();
}
return result;
}
public static void main(String[] args) throws Exception {
Runner.main(ContainerByTypeBenchmark.class, args);
}
}
public static <T extends EObject> T getContainerOfType(EObject ele, Class<T> type) {
if (type.isAssignableFrom(ele.getClass()))
return (T) ele;
if (ele.eContainer() != null)
return getContainerOfType(ele.eContainer(), type);
return null;
}
public static <T extends EObject> T getContainerOfType(EObject object, Class<T> clazz)
throws Exception {
for(EObject e = object; e != null; e = e.eContainer()) {
if (clazz.isInstance(e)) {
return (T) e;
}
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.