Skip to content

Instantly share code, notes, and snippets.

@szarnekow
Created November 27, 2012 09:41
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 szarnekow/4153356 to your computer and use it in GitHub Desktop.
Save szarnekow/4153356 to your computer and use it in GitHub Desktop.
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