Skip to content

Commit a462ad1

Browse files
committed
Add "size" property for read tests (not yet sure if I like it or not)
1 parent 9288a89 commit a462ad1

File tree

8 files changed

+95
-11
lines changed

8 files changed

+95
-11
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.fasterxml.jackson.perf;
2+
3+
import org.openjdk.jmh.annotations.AuxCounters;
4+
import org.openjdk.jmh.annotations.Level;
5+
import org.openjdk.jmh.annotations.Scope;
6+
import org.openjdk.jmh.annotations.Setup;
7+
import org.openjdk.jmh.annotations.State;
8+
import org.openjdk.jmh.infra.IterationParams;
9+
import org.openjdk.jmh.runner.IterationType;
10+
11+
/**
12+
* Simple container for additional state: size of input/output.
13+
* Notes
14+
*<ul>
15+
* <li>Type needs to be {@code EVENTS} so as not to be normalized to
16+
* runtime or iterations.<li>
17+
* <li>Ugly state-handling was the only way I could figure out how to
18+
* avoid either accumulation of size values with {@code EVENTS} or
19+
* normalization attempts with {@code OPERATIONS}.
20+
* </li>
21+
*</ul>
22+
*/
23+
@AuxCounters(AuxCounters.Type.EVENTS)
24+
@State(Scope.Thread)
25+
public class AuxStateSize
26+
{
27+
/**
28+
* Actual size we will report once (and only once!) during
29+
* the first run after warmup(s).
30+
*/
31+
public int size;
32+
33+
/**
34+
* Simple state to indicate last iteration type we saw
35+
*/
36+
private IterationType lastType;
37+
38+
/**
39+
* Flag that indicates that we should set size just once
40+
*/
41+
private boolean shouldSetSize;
42+
43+
// Choice is actually clear: "Invocation" way too often, but
44+
// "Trial" not often enough (since it is just once before Warmup).
45+
@Setup(Level.Iteration)
46+
public void clearSize(IterationParams iterParams) {
47+
size = 0;
48+
final IterationType nextType = iterParams.getType();
49+
50+
// Transition...
51+
if (nextType != lastType) {
52+
if (nextType == IterationType.MEASUREMENT) {
53+
shouldSetSize = true;
54+
}
55+
lastType = nextType;
56+
}
57+
}
58+
59+
public void set(int size) {
60+
if (shouldSetSize) {
61+
shouldSetSize = false;
62+
this.size = size;
63+
}
64+
}
65+
}

src/main/java/com/fasterxml/jackson/perf/ReadPerfBaseBasicJackson.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import java.util.concurrent.TimeUnit;
55

66
import org.openjdk.jmh.annotations.Benchmark;
7+
import org.openjdk.jmh.annotations.BenchmarkMode;
8+
import org.openjdk.jmh.annotations.Mode;
79
import org.openjdk.jmh.annotations.OutputTimeUnit;
810
import org.openjdk.jmh.infra.Blackhole;
911

@@ -42,10 +44,15 @@ protected ReadPerfBaseBasicJackson(Class<T> type,
4244
*/
4345

4446
@Benchmark
47+
@BenchmarkMode(Mode.Throughput)
4548
@OutputTimeUnit(TimeUnit.SECONDS)
4649
@Override
47-
public void readPojoMediaItem(Blackhole bh) throws Exception {
48-
bh.consume(read(MINIMAL_CONV.mediaItemAsBytes(), MEDIA_ITEM_READER));
50+
public void readPojoMediaItem(Blackhole bh, AuxStateSize size)
51+
throws Exception
52+
{
53+
final byte[] input = MINIMAL_CONV.mediaItemAsBytes();
54+
size.set(input.length);
55+
bh.consume(read(input, MEDIA_ITEM_READER));
4956
}
5057

5158
/*

src/main/java/com/fasterxml/jackson/perf/ReadPerfTestBasic.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44

55
public interface ReadPerfTestBasic
66
{
7-
public void readPojoMediaItem(Blackhole bh) throws Exception;
7+
public void readPojoMediaItem(Blackhole bh, AuxStateSize size)
8+
throws Exception;
89
}

src/main/java/com/fasterxml/jackson/perf/json/JacksonJrStdReadVanilla.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.openjdk.jmh.infra.Blackhole;
88

99
import com.fasterxml.jackson.jr.ob.JSON;
10+
import com.fasterxml.jackson.perf.AuxStateSize;
1011
import com.fasterxml.jackson.perf.ReadPerfTestBasic;
1112
import com.fasterxml.jackson.perf.model.MediaItem;
1213
import com.fasterxml.jackson.perf.model.MediaItems;
@@ -34,7 +35,8 @@ public JacksonJrStdReadVanilla() { }
3435

3536
@Benchmark
3637
@Override
37-
public void readPojoMediaItem(Blackhole bh) throws Exception {
38+
public void readPojoMediaItem(Blackhole bh, AuxStateSize size) throws Exception {
39+
size.set(_mediaItemBytes.length);
3840
bh.consume(read(_mediaItemBytes));
3941
}
4042

src/main/java/com/fasterxml/jackson/perf/json/JacksonJrWastefulReadVanilla.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ public JacksonJrWastefulReadVanilla() {
3232
}
3333

3434
@Override
35-
public void readPojoMediaItem(Blackhole bh) throws Exception {
36-
bh.consume(read(MINIMAL_CONV.mediaItemAsBytes()));
35+
public void readPojoMediaItem(Blackhole bh, AuxStateSize size) throws Exception {
36+
final byte[] input = MINIMAL_CONV.mediaItemAsBytes();
37+
size.set(input.length);
38+
bh.consume(read(input));
3739
}
3840

3941
protected Object read(byte[] data) throws Exception {

src/main/java/com/fasterxml/jackson/perf/json/JsonStringReadVanilla.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@ protected Object read(String input, ObjectReader reader) throws IOException {
5858
@Benchmark
5959
@OutputTimeUnit(TimeUnit.SECONDS)
6060
@Override
61-
public void readPojoMediaItem(Blackhole bh) throws Exception {
62-
bh.consume(read(_converter.mediaItemAsString(), MEDIA_ITEM_READER));
61+
public void readPojoMediaItem(Blackhole bh, AuxStateSize size) throws Exception {
62+
final String input = _converter.mediaItemAsString();
63+
size.set(input.length());
64+
bh.consume(read(input, MEDIA_ITEM_READER));
6365
}
6466

6567
/*

src/main/java/com/fasterxml/jackson/perf/json/JsonWastefulReadVanilla.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import com.fasterxml.jackson.core.JsonFactory;
1212
import com.fasterxml.jackson.databind.*;
13+
import com.fasterxml.jackson.perf.AuxStateSize;
1314
import com.fasterxml.jackson.perf.ReadPerfBaseBasicJackson;
1415
import com.fasterxml.jackson.perf.data.InputConverter;
1516
import com.fasterxml.jackson.perf.model.MediaItem;
@@ -36,8 +37,10 @@ public JsonWastefulReadVanilla() {
3637
}
3738

3839
@Override
39-
public void readPojoMediaItem(Blackhole bh) throws Exception {
40-
bh.consume(read(MINIMAL_CONV.mediaItemAsString()));
40+
public void readPojoMediaItem(Blackhole bh, AuxStateSize size) throws Exception {
41+
final String input = MINIMAL_CONV.mediaItemAsString();
42+
size.set(input.length());
43+
bh.consume(read(input));
4144
}
4245

4346
@Benchmark

src/test/java/com/fasterxml/jackson/perf/data/InputDataTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ public class InputDataTest extends TestCase
77
public void testByteInput() throws Exception
88
{
99
for (InputData data : InputData.values()) {
10-
assertNotNull(data.bytes());
10+
final byte[] bytes = data.bytes();
11+
assertNotNull(bytes);
12+
// System.out.println("Format '"+data.name()+": "+bytes.length+" bytes");
1113
}
1214
}
1315
}

0 commit comments

Comments
 (0)