1
+ /*
2
+ * Copyright 2021 the original author or authors.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * https://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ package io .r2dbc .postgresql ;
18
+
19
+ import io .netty .buffer .ByteBufAllocator ;
20
+ import io .netty .buffer .UnpooledByteBufAllocator ;
21
+ import io .r2dbc .postgresql .codec .CodecFinderCacheImpl ;
22
+ import io .r2dbc .postgresql .codec .CodecFinderDefaultImpl ;
23
+ import io .r2dbc .postgresql .codec .Codecs ;
24
+ import io .r2dbc .postgresql .codec .DefaultCodecs ;
25
+ import io .r2dbc .postgresql .util .ByteBufUtils ;
26
+ import org .junit .platform .commons .annotation .Testable ;
27
+ import org .openjdk .jmh .annotations .Benchmark ;
28
+ import org .openjdk .jmh .annotations .BenchmarkMode ;
29
+ import org .openjdk .jmh .annotations .Mode ;
30
+ import org .openjdk .jmh .annotations .OutputTimeUnit ;
31
+ import org .openjdk .jmh .annotations .Param ;
32
+ import org .openjdk .jmh .annotations .Scope ;
33
+ import org .openjdk .jmh .annotations .State ;
34
+ import org .openjdk .jmh .infra .Blackhole ;
35
+
36
+ import java .time .LocalDateTime ;
37
+ import java .util .concurrent .TimeUnit ;
38
+
39
+ import static io .r2dbc .postgresql .message .Format .FORMAT_BINARY ;
40
+ import static io .r2dbc .postgresql .message .Format .FORMAT_TEXT ;
41
+ import static io .r2dbc .postgresql .type .PostgresqlObjectId .FLOAT4 ;
42
+ import static io .r2dbc .postgresql .type .PostgresqlObjectId .FLOAT8 ;
43
+ import static io .r2dbc .postgresql .type .PostgresqlObjectId .FLOAT8_ARRAY ;
44
+ import static io .r2dbc .postgresql .type .PostgresqlObjectId .INT2 ;
45
+ import static io .r2dbc .postgresql .type .PostgresqlObjectId .INT2_ARRAY ;
46
+ import static io .r2dbc .postgresql .type .PostgresqlObjectId .INT4 ;
47
+ import static io .r2dbc .postgresql .type .PostgresqlObjectId .INT4_ARRAY ;
48
+ import static io .r2dbc .postgresql .type .PostgresqlObjectId .TIMESTAMP ;
49
+ import static io .r2dbc .postgresql .type .PostgresqlObjectId .VARCHAR ;
50
+ import static io .r2dbc .postgresql .util .TestByteBufAllocator .TEST ;
51
+
52
+ /**
53
+ * Benchmarks for codec encoding and decoding using cached enabled or disabled registries.
54
+ */
55
+ @ BenchmarkMode (Mode .Throughput )
56
+ @ OutputTimeUnit (TimeUnit .SECONDS )
57
+ @ Testable
58
+ public class CodecRegistryBenchmarks extends BenchmarkSettings {
59
+
60
+ @ State (Scope .Benchmark )
61
+ public static class CodecRegistryHolder {
62
+
63
+ @ Param ({"10" , "100" , "1000" })
64
+ public int iterations ;
65
+
66
+ final ByteBufAllocator byteBufAllocator = new UnpooledByteBufAllocator (false , true );
67
+
68
+ DefaultCodecs cacheEnabledRegistry = new DefaultCodecs (byteBufAllocator , false , new CodecFinderCacheImpl ());
69
+
70
+ DefaultCodecs cacheDisabledRegistry = new DefaultCodecs (byteBufAllocator , false , new CodecFinderDefaultImpl ());
71
+
72
+ }
73
+
74
+ private void decode (Codecs codecs , int iterations , Blackhole voodoo ) {
75
+ for (int i = 0 ; i < iterations ; i ++) {
76
+ voodoo .consume (codecs .decode (
77
+ TEST .buffer (4 ).writeInt (200 ), INT4 .getObjectId (), FORMAT_BINARY , Integer .class ));
78
+ voodoo .consume (codecs .decode (
79
+ ByteBufUtils .encode (TEST , "100" ), INT2 .getObjectId (), FORMAT_TEXT , Short .class ));
80
+ voodoo .consume (codecs .decode (
81
+ ByteBufUtils .encode (TEST , "-125.369" ), FLOAT8 .getObjectId (), FORMAT_TEXT , Double .class ));
82
+ voodoo .consume (codecs .decode (
83
+ TEST .buffer (4 ).writeFloat (-65.369f ), FLOAT4 .getObjectId (), FORMAT_BINARY , Float .class ));
84
+ voodoo .consume (
85
+ codecs .decode (
86
+ ByteBufUtils .encode (TEST , "test" ),
87
+ VARCHAR .getObjectId (),
88
+ FORMAT_TEXT ,
89
+ String .class ));
90
+ voodoo .consume (
91
+ codecs .decode (
92
+ ByteBufUtils .encode (TEST , "2018-11-04 15:35:00.847108" ),
93
+ TIMESTAMP .getObjectId (),
94
+ FORMAT_TEXT ,
95
+ LocalDateTime .class ));
96
+ voodoo .consume (codecs .decode (ByteBufUtils .encode (TEST , "{100,200}" ), INT2_ARRAY .getObjectId (), FORMAT_TEXT , Object .class ));
97
+ voodoo .consume (codecs .decode (ByteBufUtils .encode (TEST , "{100,200}" ), INT4_ARRAY .getObjectId (), FORMAT_TEXT , Object .class ));
98
+ voodoo .consume (codecs .decode (ByteBufUtils .encode (TEST , "{100.5,200.8}" ), FLOAT8_ARRAY .getObjectId (), FORMAT_TEXT , Object .class ));
99
+ }
100
+ }
101
+
102
+ @ Benchmark
103
+ public void decodeWithCacheEnabledRegistry (CodecRegistryHolder holder , Blackhole voodoo ) {
104
+ decode (holder .cacheEnabledRegistry , holder .iterations , voodoo );
105
+ }
106
+
107
+ @ Benchmark
108
+ public void decodeWithCacheDisabledRegistry (CodecRegistryHolder holder , Blackhole voodoo ) {
109
+ decode (holder .cacheDisabledRegistry , holder .iterations , voodoo );
110
+ }
111
+
112
+ private void encode (Codecs codecs , int iterations , Blackhole voodoo ) {
113
+ for (int i = 0 ; i < iterations ; i ++) {
114
+ voodoo .consume (codecs .encode ((short ) 12 ));
115
+ voodoo .consume (codecs .encode (35698 ));
116
+ voodoo .consume (codecs .encode (-256.3698 ));
117
+ voodoo .consume (codecs .encode (85.7458f ));
118
+ voodoo .consume (codecs .encode ("A text value" ));
119
+ voodoo .consume (codecs .encode (LocalDateTime .now ()));
120
+ voodoo .consume (codecs .encode (new Long []{100L , 200L }));
121
+ voodoo .consume (codecs .encode (new Double []{100.5 , 200.25 }));
122
+ voodoo .consume (codecs .encodeNull (Integer .class ));
123
+ voodoo .consume (codecs .encodeNull (String .class ));
124
+ voodoo .consume (codecs .encodeNull (Double [].class ));
125
+ }
126
+ }
127
+
128
+ @ Benchmark
129
+ public void encodeWithCacheEnabledRegistry (CodecRegistryHolder holder , Blackhole voodoo ) {
130
+ encode (holder .cacheEnabledRegistry , holder .iterations , voodoo );
131
+ }
132
+
133
+ @ Benchmark
134
+ public void encodeWithCacheDisabledRegistry (CodecRegistryHolder holder , Blackhole voodoo ) {
135
+ encode (holder .cacheDisabledRegistry , holder .iterations , voodoo );
136
+ }
137
+
138
+ }
0 commit comments