Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map performance tests #79

Merged
merged 4 commits into from
Sep 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package com.hubspot.jinjava.el;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.Collection;
import java.util.Map;
import java.util.Set;

import com.hubspot.jinjava.Jinjava;
import com.hubspot.jinjava.interpret.Context;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;

public class ExpressionResolverPerformanceTest {

public static void main(String[] args) {
PerformanceTester tester = new PerformanceTester();
tester.run();
}

public static class PerformanceTester {

private JinjavaInterpreter interpreter;
private Context context;
private long startTime;

public PerformanceTester() {

interpreter = new Jinjava().newInterpreter();
context = interpreter.getContext();
context.put("customMap", new CustomMap());
context.put("customObject", new CustomObject());
}

public void run() {

int iterations = 1000000;

testMapResolver(iterations);
testMethodResolver(iterations);
}

public void startTimer() {
startTime = System.currentTimeMillis();
}

public void stopTimer() {
System.out.println(String.format("%d msec", System.currentTimeMillis() - startTime));
}

public void testMapResolver(int iterations) {
System.out.println("map resolver with " + iterations + " iterations");
startTimer();

for (int i = 0; i < iterations; i++) {
Object val = interpreter.resolveELExpression("customMap.get(\"thing\")", -1);
assertThat(val).isEqualTo("hey");
}

stopTimer();
}

public void testMethodResolver(int iterations) {
System.out.println("method resolver with " + iterations + " iterations");
startTimer();

for (int i = 0; i < iterations; i++) {
Object val = interpreter.resolveELExpression("customObject.getThing()", -1);
assertThat(val).isEqualTo("hey");
}

stopTimer();
}

}

static class CustomObject {

public CustomObject() {
}

public String getThing() {
return "hey";
}

}

static class CustomMap implements Map<String, String> {

@Override
public String get(Object key) {
return "hey";
}

@Override
public int size() {
return 0;
}

@Override
public boolean isEmpty() {
return false;
}

@Override
public boolean containsKey(Object key) {
return false;
}

@Override
public boolean containsValue(Object value) {
return false;
}

@Override
public String put(String key, String value) {
return null;
}

@Override
public String remove(Object key) {
return null;
}

@Override
public void putAll(Map<? extends String, ? extends String> m) {

}

@Override
public void clear() {

}

@Override
public Set<String> keySet() {
return null;
}

@Override
public Collection<String> values() {
return null;
}

@Override
public Set<Entry<String, String>> entrySet() {
return null;
}
}

}
99 changes: 99 additions & 0 deletions src/test/java/com/hubspot/jinjava/el/ExpressionResolverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@
import static org.assertj.core.api.Assertions.assertThat;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

import org.junit.Before;
import org.junit.Test;

import com.google.common.collect.ForwardingList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.hubspot.jinjava.Jinjava;
Expand Down Expand Up @@ -108,6 +111,102 @@ public void itResolvesDictValWithDotParam() throws Exception {
assertThat(interpreter.getContext().wasExpressionResolved("thedict.foo")).isTrue();
}

@Test
public void itResolvesMapValOnCustomObject() throws Exception {

MyCustomMap dict = new MyCustomMap();
context.put("thedict", dict);

Object val = interpreter.resolveELExpression("thedict['foo']", -1);
assertThat(val).isEqualTo("bar");
assertThat(interpreter.getContext().wasExpressionResolved("thedict['foo']")).isTrue();

Object val2 = interpreter.resolveELExpression("thedict.two", -1);
assertThat(val2).isEqualTo("2");
assertThat(interpreter.getContext().wasExpressionResolved("thedict.two")).isTrue();
}

@Test
public void itResolvesOtherMethodsOnCustomMapObject() throws Exception {

MyCustomMap dict = new MyCustomMap();
context.put("thedict", dict);

Object val = interpreter.resolveELExpression("thedict.size", -1);
assertThat(val).isEqualTo("777");

Object val1 = interpreter.resolveELExpression("thedict.size()", -1);
assertThat(val1).isEqualTo(3);

Object val2 = interpreter.resolveELExpression("thedict.items()", -1);
assertThat(val2.toString()).isEqualTo("[foo=bar, two=2, size=777]");
}

public static final class MyCustomMap implements Map<String, String> {

Map<String, String> data = ImmutableMap.of("foo", "bar", "two", "2", "size", "777");

@Override
public int size() {
return data.size();
}

@Override
public boolean isEmpty() {
return data.isEmpty();
}

@Override
public boolean containsKey(Object key) {
return data.containsKey(key);
}

@Override
public boolean containsValue(Object value) {
return data.containsValue(value);
}

@Override
public String get(Object key) {
return data.get(key);
}

@Override
public String put(String key, String value) {
return null;
}

@Override
public String remove(Object key) {
return null;
}

@Override
public void putAll(Map<? extends String, ? extends String> m) {

}

@Override
public void clear() {

}

@Override
public Set<String> keySet() {
return data.keySet();
}

@Override
public Collection<String> values() {
return data.values();
}

@Override
public Set<Entry<String, String>> entrySet() {
return data.entrySet();
}
}

@Test
public void itResolvesInnerDictVal() throws Exception {
Map<String, Object> dict = Maps.newHashMap();
Expand Down