|
1 | 1 | package com.arangodb;
|
2 | 2 |
|
| 3 | +import com.arangodb.entity.MultiDocumentEntity; |
| 4 | +import com.arangodb.internal.ArangoCollectionImpl; |
| 5 | +import com.arangodb.internal.ArangoDatabaseImpl; |
| 6 | +import com.arangodb.internal.ArangoExecutor; |
| 7 | +import com.arangodb.internal.InternalResponse; |
3 | 8 | import com.arangodb.internal.serde.InternalSerde;
|
4 | 9 | import com.arangodb.internal.serde.InternalSerdeProvider;
|
5 | 10 | import com.arangodb.jackson.dataformat.velocypack.VPackMapper;
|
|
25 | 30 | import org.openjdk.jmh.runner.options.OptionsBuilder;
|
26 | 31 |
|
27 | 32 | import java.io.IOException;
|
| 33 | +import java.net.URISyntaxException; |
28 | 34 | import java.nio.file.Files;
|
29 | 35 | import java.nio.file.Path;
|
30 | 36 | import java.nio.file.Paths;
|
|
39 | 45 | @OutputTimeUnit(TimeUnit.MILLISECONDS)
|
40 | 46 | @Fork(1)
|
41 | 47 | public class SerdeBench {
|
| 48 | + public static class MyCol extends ArangoCollectionImpl { |
| 49 | + static ArangoDB jsonAdb = new ArangoDB.Builder() |
| 50 | + .host("127.0.0.1", 8529) |
| 51 | + .protocol(Protocol.HTTP_JSON) |
| 52 | + .build(); |
| 53 | + |
| 54 | + static ArangoDB vpackAdb = new ArangoDB.Builder() |
| 55 | + .host("127.0.0.1", 8529) |
| 56 | + .protocol(Protocol.HTTP_VPACK) |
| 57 | + .build(); |
| 58 | + |
| 59 | + private MyCol(ArangoDB adb) { |
| 60 | + super((ArangoDatabaseImpl) adb.db(), "foo"); |
| 61 | + } |
| 62 | + |
| 63 | + public static MyCol ofJson() { |
| 64 | + return new MyCol(jsonAdb); |
| 65 | + } |
| 66 | + |
| 67 | + public static MyCol ofVpack() { |
| 68 | + return new MyCol(vpackAdb); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public <T> ArangoExecutor.ResponseDeserializer<MultiDocumentEntity<T>> getDocumentsResponseDeserializer(Class<T> type) { |
| 73 | + return super.getDocumentsResponseDeserializer(type); |
| 74 | + } |
| 75 | + } |
| 76 | + |
42 | 77 | @State(Scope.Benchmark)
|
43 | 78 | public static class Data {
|
44 | 79 | public final byte[] vpack;
|
45 | 80 | public final byte[] json;
|
46 | 81 | public final RawBytes rawJsonBytes;
|
47 | 82 | public final RawBytes rawVPackBytes;
|
48 | 83 | public final RawJson rawJson;
|
| 84 | + public final MyCol jsonCol = MyCol.ofJson(); |
| 85 | + public final MyCol vpackCol = MyCol.ofVpack(); |
| 86 | + public final InternalResponse jsonResp = new InternalResponse(); |
| 87 | + public final InternalResponse vpackResp = new InternalResponse(); |
49 | 88 |
|
50 | 89 | public Data() {
|
51 | 90 | ObjectMapper jsonMapper = new ObjectMapper();
|
52 | 91 | VPackMapper vpackMapper = new VPackMapper();
|
53 | 92 |
|
54 | 93 | try {
|
55 |
| - String str = new String(Files.readAllBytes( |
56 |
| - Paths.get(SerdeBench.class.getResource("/api-docs.json").toURI()))); |
57 |
| - JsonNode jn = jsonMapper.readTree(str); |
58 |
| - |
| 94 | + JsonNode jn = readFile("/api-docs.json", jsonMapper); |
59 | 95 | json = jsonMapper.writeValueAsBytes(jn);
|
60 | 96 | vpack = vpackMapper.writeValueAsBytes(jn);
|
61 | 97 | rawJsonBytes = RawBytes.of(json);
|
62 | 98 | rawVPackBytes = RawBytes.of(vpack);
|
63 | 99 | rawJson = RawJson.of(jsonMapper.writeValueAsString(jsonMapper.readTree(json)));
|
64 | 100 |
|
| 101 | + JsonNode docs = readFile("/multi-docs.json", jsonMapper); |
| 102 | + jsonResp.setResponseCode(200); |
| 103 | + jsonResp.setBody(jsonMapper.writeValueAsBytes(docs)); |
| 104 | + vpackResp.setResponseCode(200); |
| 105 | + vpackResp.setBody(vpackMapper.writeValueAsBytes(docs)); |
65 | 106 | } catch (Exception e) {
|
66 | 107 | throw new RuntimeException(e);
|
67 | 108 | }
|
68 | 109 | }
|
| 110 | + |
| 111 | + private JsonNode readFile(String filename, ObjectMapper mapper) throws IOException, URISyntaxException { |
| 112 | + String str = new String(Files.readAllBytes( |
| 113 | + Paths.get(SerdeBench.class.getResource(filename).toURI()))); |
| 114 | + return mapper.readTree(str); |
| 115 | + } |
69 | 116 | }
|
70 | 117 |
|
71 | 118 | public static void main(String[] args) throws RunnerException, IOException {
|
@@ -122,4 +169,18 @@ public void extractBytesJson(Data data, Blackhole bh) {
|
122 | 169 | );
|
123 | 170 | }
|
124 | 171 |
|
| 172 | + @Benchmark |
| 173 | + public void deserializeDocsJson(Data data, Blackhole bh) { |
| 174 | + bh.consume( |
| 175 | + data.jsonCol.getDocumentsResponseDeserializer(RawBytes.class).deserialize(data.jsonResp) |
| 176 | + ); |
| 177 | + } |
| 178 | + |
| 179 | + @Benchmark |
| 180 | + public void deserializeDocsVPack(Data data, Blackhole bh) { |
| 181 | + bh.consume( |
| 182 | + data.vpackCol.getDocumentsResponseDeserializer(RawBytes.class).deserialize(data.vpackResp) |
| 183 | + ); |
| 184 | + } |
| 185 | + |
125 | 186 | }
|
0 commit comments