Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Show std dev for benchmark times

  • Loading branch information...
commit 0a96c7b12037e03ece4a199a81ad729488555fcd 1 parent c744bc4
@poetix poetix authored
View
6 src/main/java/com/youdevise/hsd/EnumIndexedCursor.java
@@ -5,7 +5,11 @@
public interface EnumIndexedCursor<E extends Enum<E>> {
<T> T get(E key);
int getInt(E key);
- String getString(E key);
+ short getShort(E key);
+ long getLong(E key);
+ float getFloat(E key);
+ double getDouble(E key);
+ byte getByte(E key);
EnumMap<E, Object> values();
boolean next();
}
View
41 src/main/java/com/youdevise/hsd/ResultSetAdapter.java
@@ -68,12 +68,47 @@ public int getInt(E key) {
}
@Override
- public String getString(E key) {
+ public short getShort(E key) {
try {
- return resultSet.getString(indices.get(key));
+ return resultSet.getShort(indices.get(key));
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Override
+ public long getLong(E key) {
+ try {
+ return resultSet.getLong(indices.get(key));
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Override
+ public float getFloat(E key) {
+ try {
+ return resultSet.getFloat(indices.get(key));
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Override
+ public double getDouble(E key) {
+ try {
+ return resultSet.getDouble(indices.get(key));
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Override
+ public byte getByte(E key) {
+ try {
+ return resultSet.getByte(indices.get(key));
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
-
}
View
66 src/test/java/com/youdevise/hsd/HSQLDBIntegrationTest.java
@@ -104,7 +104,7 @@ public boolean handle(int id, String name) {
@Override
public Boolean handle(EnumIndexedCursor<Fields> cursor) {
int id = cursor.getInt(Fields.id);
- String name = cursor.getString(Fields.name);
+ String name = cursor.<String>get(Fields.name);
if (name.equals("Zalgo")) {
throw new RuntimeException("He comes!");
}
@@ -112,29 +112,42 @@ public Boolean handle(EnumIndexedCursor<Fields> cursor) {
}
}
+ private static final int TOTAL_RUNS = 10;
+
public void runBenchmark(final String description, Runnable benchmark) {
long warmupTime = 0;
- long firstTime = 0;
+ long[] times = new long[TOTAL_RUNS];
long totalTime = 0;
- for (int i = 0; i<11; i++) {
+ long fastestTime = 100000L;
+ long slowestTime = 0;
+ for (int i = 0; i<=TOTAL_RUNS; i++) {
long start = System.nanoTime();
benchmark.run();
long end = System.nanoTime();
- switch(i) {
- case 0:
- warmupTime = end - start;
- break;
- case 1:
- firstTime = end - start;
- default:
- totalTime += end - start;
+ long time = (end - start) / 1000000;
+ if (i == 0) {
+ warmupTime = time;
+ } else {
+ totalTime += time;
+ if (time < fastestTime) { fastestTime = time; }
+ if (time > slowestTime) { slowestTime = time; }
+ times[i-1] = time;
}
}
+ double mean = totalTime / TOTAL_RUNS;
+ double numerator = 0;
+ for (long time : times) {
+ numerator += (time * time) + (mean * mean) - (2 * mean * time);
+ }
+ double stdDev = Math.sqrt(numerator / TOTAL_RUNS);
System.out.println(description);
System.out.println(Strings.repeat("=", description.length()));
- System.out.println(String.format("Avg over 10 runs: %d ms", totalTime / 10000000));
- System.out.println(String.format("Warmup: %d ms", warmupTime / 1000000));
- System.out.println(String.format("First run: %d ms", firstTime / 1000000));
+ System.out.println(String.format("Avg over %d runs: %d ms", TOTAL_RUNS, totalTime / TOTAL_RUNS));
+ System.out.println(String.format("Warmup: %d ms", warmupTime));
+ System.out.println(String.format("First run: %d ms", times[0]));
+ System.out.println(String.format("Slowest: %d ms", slowestTime));
+ System.out.println(String.format("Fastest: %d ms", fastestTime));
+ System.out.println(String.format("Standard deviation: %s", stdDev));
System.out.println();
}
@@ -246,31 +259,6 @@ public HibernateRecord(int id, String name) {
session.close();
}
-
- @SuppressWarnings("unchecked")
- @Test public void
- hibernate_with_select_map_is_not_too_shabby() {
- final Session session = createHibernateSession();
-
- runBenchmark("Hibernate with map selector", new Runnable() {
- @Override public void run() {
- final ScrollableResults results = session.createQuery(String.format("SELECT new map(p.id, p.name as name) FROM MyPersistable p",
- HibernateRecord.class.getName()))
- .setReadOnly(true)
- .setCacheable(false)
- .scroll(ScrollMode.FORWARD_ONLY);
- while (results.next()) {
- Map<String, Object> persistable = (Map<String, Object>) results.get()[0];
- if (persistable.get("name").equals("Zalgo")) {
- throw new RuntimeException("He comes!");
- }
- }
- results.close();
- }
- });
-
- session.close();
- }
private Session createHibernateSession() {
AnnotationConfiguration cfg = new AnnotationConfiguration();
View
31 src/test/java/com/youdevise/hsd/asm/ProxyGeneratorTest.java
@@ -20,7 +20,7 @@
public static interface Record {
String getFoo();
- int[] getBar();
+ int getBar();
Date getBaz();
}
@@ -53,8 +53,28 @@ public int getInt(E key) {
}
@Override
- public String getString(E key) {
- return this.<String>get(key);
+ public short getShort(E key) {
+ return this.<Short>get(key);
+ }
+
+ @Override
+ public long getLong(E key) {
+ return this.<Long>get(key);
+ }
+
+ @Override
+ public float getFloat(E key) {
+ return this.<Float>get(key);
+ }
+
+ @Override
+ public double getDouble(E key) {
+ return this.<Double>get(key);
+ }
+
+ @Override
+ public byte getByte(E key) {
+ return this.<Byte>get(key);
}
}
@@ -62,17 +82,16 @@ public String getString(E key) {
@Test public void
generates_proxy_mapping_getters_to_enum_map() {
Date theDate = new Date();
- int[] ints = new int[] { 1, 2, 3 };
EnumMap<Fields, Object> values = new EnumMap<Fields, Object>(Fields.class);
values.put(Fields.foo, "Foo");
- values.put(Fields.bar, ints);
+ values.put(Fields.bar, 23);
values.put(Fields.baz, theDate);
ProxyGenerator<Fields, Record> generator = ProxyGenerator.mapping(Fields.class).to(Record.class);
Record record = generator.generateView(new TestCursorImpl<Fields>(values));
assertThat(record.getFoo(), equalTo("Foo"));
- assertThat(record.getBar(), equalTo(ints));
+ assertThat(record.getBar(), equalTo(23));
assertThat(record.getBaz(), equalTo(theDate));
}
Please sign in to comment.
Something went wrong with that request. Please try again.