Skip to content

Commit af63d78

Browse files
committed
Add CurrencyWrite test
1 parent bc4c6d1 commit af63d78

File tree

5 files changed

+105
-0
lines changed

5 files changed

+105
-0
lines changed

results-currency-2.15.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ Benchmark Mode Cnt Score Error Uni
88
JsonStdReadVanilla.readCurrencyPojoDefault thrpt 45 61012.389 ± 529.749 ops/s
99
JsonStdReadVanilla.readCurrencyPojoFast thrpt 45 70684.408 ± 621.934 ops/s
1010

11+
java -Xmx256m -jar target/perf.jar ".*Json.*StdWriteVanilla.writeCurrencyPojo.*" -wi 3 -w 1 -i 5 -r 1 -f 5
12+
13+
Benchmark Mode Cnt Score Error Units
14+
JsonStdWriteVanilla.writeCurrencyPojoDefault thrpt 25 54065.042 ± 457.264 ops/s
15+
JsonStdWriteVanilla.writeCurrencyPojoFast thrpt 25 68396.099 ± 430.756 ops/s
16+
17+
1118
### Java 17 / JSON
1219

1320
java -Xmx256m -jar target/perf.jar ".*Json.*StdReadVanilla.readCurrencyPojo.*" -wi 3 -w 1 -i 5 -r 1 -f 9

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88
import org.openjdk.jmh.annotations.OutputTimeUnit;
99
import org.openjdk.jmh.infra.Blackhole;
1010

11+
import com.fasterxml.jackson.core.StreamWriteFeature;
1112
import com.fasterxml.jackson.databind.JsonNode;
1213
import com.fasterxml.jackson.databind.ObjectMapper;
1314
import com.fasterxml.jackson.databind.ObjectWriter;
15+
import com.fasterxml.jackson.perf.model.Currency;
16+
import com.fasterxml.jackson.perf.model.CurrencySampleProvider;
1417

1518
public abstract class WritePerfBaseFullJackson<T>
1619
extends WritePerfBasicJackson<T>
@@ -22,6 +25,12 @@ public abstract class WritePerfBaseFullJackson<T>
2225

2326
protected final ObjectWriter NODE_WRITER;
2427

28+
protected final ObjectWriter CURRENCY_WRITER_STD;
29+
30+
protected final ObjectWriter CURRENCY_WRITER_FAST;
31+
32+
protected final Currency currencyValue;
33+
2534
// Note on these two variables: looks like there is some (de?)optimization
2635
// that changes results if we use conversion operations too early.
2736
// To avoid that, we will lazily do conversions
@@ -35,6 +44,32 @@ protected WritePerfBaseFullJackson(ObjectMapper mapper) {
3544
MAPPER = mapper;
3645
UNTYPED_WRITER = mapper.writerFor(Object.class);
3746
NODE_WRITER = mapper.writerFor(JsonNode.class);
47+
CURRENCY_WRITER_STD = mapper.writerFor(Currency.class)
48+
.without(StreamWriteFeature.USE_FAST_DOUBLE_WRITER);
49+
CURRENCY_WRITER_FAST = CURRENCY_WRITER_STD.with(StreamWriteFeature.USE_FAST_DOUBLE_WRITER);
50+
51+
// Alas, we need to read Currency POJO in here, no getting around the fact
52+
currencyValue = CurrencySampleProvider.getSample();
53+
}
54+
55+
/*
56+
/**********************************************************************
57+
/* POJO writing tests
58+
/**********************************************************************
59+
*/
60+
61+
@Benchmark
62+
@OutputTimeUnit(TimeUnit.SECONDS)
63+
@Override
64+
public void writeCurrencyPojoDefault(Blackhole bh) throws Exception {
65+
bh.consume(write(currencyValue, CURRENCY_WRITER_STD));
66+
}
67+
68+
@Benchmark
69+
@OutputTimeUnit(TimeUnit.SECONDS)
70+
@Override
71+
public void writeCurrencyPojoFast(Blackhole bh) throws Exception {
72+
bh.consume(write(currencyValue, CURRENCY_WRITER_FAST));
3873
}
3974

4075
/*

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@ public interface WritePerfTestFull extends WritePerfTestBasic
77
public void writeUntypedMediaItem(Blackhole bh) throws Exception;
88

99
public void writeNodeMediaItem(Blackhole bh) throws Exception;
10+
11+
public void writeCurrencyPojoDefault(Blackhole bh) throws Exception;
12+
public void writeCurrencyPojoFast(Blackhole bh) throws Exception;
1013
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.fasterxml.jackson.databind.JsonNode;
1313
import com.fasterxml.jackson.databind.ObjectMapper;
1414
import com.fasterxml.jackson.perf.WritePerfTestFull;
15+
import com.fasterxml.jackson.perf.model.CurrencySampleProvider;
1516
import com.fasterxml.jackson.perf.model.MediaItem;
1617
import com.fasterxml.jackson.perf.model.MediaItems;
1718
import com.fasterxml.jackson.perf.util.NopOutputStream;
@@ -42,6 +43,27 @@ public JsonWastefulWriteVanilla() {
4243
_untyped = MAPPER.convertValue(_value, Map.class);
4344
}
4445

46+
/*
47+
/**********************************************************************
48+
/* POJO writing tests
49+
/**********************************************************************
50+
*/
51+
52+
@Benchmark
53+
@OutputTimeUnit(TimeUnit.SECONDS)
54+
@Override
55+
public void writeCurrencyPojoDefault(Blackhole bh) throws Exception {
56+
bh.consume(write(CurrencySampleProvider.getSample()));
57+
}
58+
59+
@Benchmark
60+
@OutputTimeUnit(TimeUnit.SECONDS)
61+
@Override
62+
public void writeCurrencyPojoFast(Blackhole bh) throws Exception {
63+
// NOTE! Does not use Fast writes...
64+
bh.consume(write(CurrencySampleProvider.getSample()));
65+
}
66+
4567
@Benchmark
4668
@OutputTimeUnit(TimeUnit.SECONDS)
4769
@Override
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.fasterxml.jackson.perf.model;
2+
3+
import com.fasterxml.jackson.databind.json.JsonMapper;
4+
import com.fasterxml.jackson.perf.data.InputData;
5+
6+
public class CurrencySampleProvider
7+
{
8+
private final static CurrencySampleProvider INSTANCE = new CurrencySampleProvider();
9+
10+
private final RuntimeException _fail;
11+
12+
private final Currency _sample;
13+
14+
public static Currency getSample() {
15+
return INSTANCE._getSample();
16+
}
17+
18+
CurrencySampleProvider() {
19+
RuntimeException fail = null;
20+
Currency sample = null;
21+
try {
22+
byte[] json = InputData.CURRENCY_WS.bytes();
23+
sample = new JsonMapper().readerFor(Currency.class)
24+
.readValue(json);
25+
} catch (Exception e) {
26+
fail = new RuntimeException(e);
27+
}
28+
_fail = fail;
29+
_sample = sample;
30+
}
31+
32+
Currency _getSample() {
33+
if (_fail != null) {
34+
throw _fail;
35+
}
36+
return _sample;
37+
}
38+
}

0 commit comments

Comments
 (0)