@@ -13,18 +13,23 @@ use stun_types::message::{
13
13
TransactionId , BINDING ,
14
14
} ;
15
15
16
- fn builder_with_attribute < ' a > ( attr : impl Into < RawAttribute < ' a > > ) -> MessageBuilder < ' a > {
16
+ fn builder_with_attribute ( attr : & dyn AttributeWrite ) -> MessageBuilder {
17
17
let mut msg = Message :: builder_request ( BINDING ) ;
18
18
msg. add_attribute ( attr) . unwrap ( ) ;
19
19
msg
20
20
}
21
21
22
- fn build_with_attribute < ' a > ( attr : impl Into < RawAttribute < ' a > > ) {
22
+ fn build_with_attribute ( attr : & dyn AttributeWrite ) {
23
23
let mut msg = Message :: builder_request ( BINDING ) ;
24
24
msg. add_attribute ( attr) . unwrap ( ) ;
25
25
let _data = msg. build ( ) ;
26
26
}
27
27
28
+ fn write_into_with_attribute ( attr : & dyn AttributeWrite , dest : & mut [ u8 ] ) {
29
+ let msg = builder_with_attribute ( attr) ;
30
+ msg. write_into ( dest) . unwrap ( ) ;
31
+ }
32
+
28
33
fn bench_message_write ( c : & mut Criterion ) {
29
34
let software = Software :: new ( "stun-types" ) . unwrap ( ) ;
30
35
let addr = "192.168.10.200:9876" . parse ( ) . unwrap ( ) ;
@@ -122,6 +127,91 @@ fn bench_message_write(c: &mut Criterion) {
122
127
} ,
123
128
) ;
124
129
group. finish ( ) ;
130
+
131
+ let mut group = c. benchmark_group ( "Message/WriteInto" ) ;
132
+ let mut scratch = vec ! [ 0 ; 1 << 8 ] ;
133
+
134
+ group. throughput ( criterion:: Throughput :: Bytes (
135
+ builder_with_attribute ( & software) . build ( ) . len ( ) as u64 ,
136
+ ) ) ;
137
+ group. bench_with_input (
138
+ BenchmarkId :: from_parameter ( "Software" ) ,
139
+ & software,
140
+ |b, software| b. iter ( || write_into_with_attribute ( software, & mut scratch) ) ,
141
+ ) ;
142
+ group. bench_with_input (
143
+ BenchmarkId :: from_parameter ( "Attributes/9" ) ,
144
+ & (
145
+ & software,
146
+ & xor_mapped_address,
147
+ & nonce,
148
+ & alt_server,
149
+ & alt_domain,
150
+ & priority,
151
+ & controlled,
152
+ & controlling,
153
+ & use_candidate,
154
+ ) ,
155
+ |b, attrs| {
156
+ b. iter ( || {
157
+ let mut msg = builder_with_attribute ( attrs. 0 ) ;
158
+ msg. add_attribute ( attrs. 1 ) . unwrap ( ) ;
159
+ msg. add_attribute ( attrs. 2 ) . unwrap ( ) ;
160
+ msg. add_attribute ( attrs. 3 ) . unwrap ( ) ;
161
+ msg. add_attribute ( attrs. 4 ) . unwrap ( ) ;
162
+ msg. add_attribute ( attrs. 5 ) . unwrap ( ) ;
163
+ msg. add_attribute ( attrs. 6 ) . unwrap ( ) ;
164
+ msg. add_attribute ( attrs. 7 ) . unwrap ( ) ;
165
+ msg. add_attribute ( attrs. 8 ) . unwrap ( ) ;
166
+ msg. write_into ( & mut scratch) . unwrap ( ) ;
167
+ } )
168
+ } ,
169
+ ) ;
170
+ group. bench_with_input (
171
+ BenchmarkId :: from_parameter ( "XorMappedAddress" ) ,
172
+ & xor_mapped_address,
173
+ |b, xor_mapped_address| {
174
+ b. iter ( || write_into_with_attribute ( xor_mapped_address, & mut scratch) ) ;
175
+ } ,
176
+ ) ;
177
+ group. bench_with_input (
178
+ BenchmarkId :: from_parameter ( "XorMappedAddress+Fingerprint" ) ,
179
+ & xor_mapped_address,
180
+ |b, xor_mapped_address| {
181
+ b. iter ( || {
182
+ let mut msg = builder_with_attribute ( xor_mapped_address) ;
183
+ msg. add_fingerprint ( ) . unwrap ( ) ;
184
+ msg. write_into ( & mut scratch) . unwrap ( ) ;
185
+ } )
186
+ } ,
187
+ ) ;
188
+ group. bench_with_input (
189
+ BenchmarkId :: from_parameter ( "XorMappedAddress+ShortTermIntegritySha1+Fingerprint" ) ,
190
+ & ( & xor_mapped_address, & short_term_integrity) ,
191
+ |b, & ( xor_mapped_address, short_term_integrity) | {
192
+ b. iter ( || {
193
+ let mut msg = builder_with_attribute ( xor_mapped_address) ;
194
+ msg. add_message_integrity ( short_term_integrity, IntegrityAlgorithm :: Sha1 )
195
+ . unwrap ( ) ;
196
+ msg. add_fingerprint ( ) . unwrap ( ) ;
197
+ msg. write_into ( & mut scratch) . unwrap ( ) ;
198
+ } )
199
+ } ,
200
+ ) ;
201
+ group. bench_with_input (
202
+ BenchmarkId :: from_parameter ( "XorMappedAddress+ShortTermIntegritySha256+Fingerprint" ) ,
203
+ & ( & xor_mapped_address, & short_term_integrity) ,
204
+ |b, & ( xor_mapped_address, short_term_integrity) | {
205
+ b. iter ( || {
206
+ let mut msg = builder_with_attribute ( xor_mapped_address) ;
207
+ msg. add_message_integrity ( short_term_integrity, IntegrityAlgorithm :: Sha256 )
208
+ . unwrap ( ) ;
209
+ msg. add_fingerprint ( ) . unwrap ( ) ;
210
+ msg. write_into ( & mut scratch) . unwrap ( ) ;
211
+ } )
212
+ } ,
213
+ ) ;
214
+ group. finish ( ) ;
125
215
}
126
216
127
217
criterion_group ! ( message_parse, bench_message_write) ;
0 commit comments