]> git.proxmox.com Git - cargo.git/blob - vendor/bytes/benches/bytes.rs
New upstream version 0.35.0
[cargo.git] / vendor / bytes / benches / bytes.rs
1 #![feature(test)]
2
3 extern crate bytes;
4 extern crate test;
5
6 use test::Bencher;
7 use bytes::{Bytes, BytesMut, BufMut};
8
9 #[bench]
10 fn alloc_small(b: &mut Bencher) {
11 b.iter(|| {
12 for _ in 0..1024 {
13 test::black_box(BytesMut::with_capacity(12));
14 }
15 })
16 }
17
18 #[bench]
19 fn alloc_mid(b: &mut Bencher) {
20 b.iter(|| {
21 test::black_box(BytesMut::with_capacity(128));
22 })
23 }
24
25 #[bench]
26 fn alloc_big(b: &mut Bencher) {
27 b.iter(|| {
28 test::black_box(BytesMut::with_capacity(4096));
29 })
30 }
31
32 #[bench]
33 fn split_off_and_drop(b: &mut Bencher) {
34 b.iter(|| {
35 for _ in 0..1024 {
36 let v = vec![10; 200];
37 let mut b = Bytes::from(v);
38 test::black_box(b.split_off(100));
39 test::black_box(b);
40 }
41 })
42 }
43
44 #[bench]
45 fn deref_unique(b: &mut Bencher) {
46 let mut buf = BytesMut::with_capacity(4096);
47 buf.put(&[0u8; 1024][..]);
48
49 b.iter(|| {
50 for _ in 0..1024 {
51 test::black_box(&buf[..]);
52 }
53 })
54 }
55
56 #[bench]
57 fn deref_unique_unroll(b: &mut Bencher) {
58 let mut buf = BytesMut::with_capacity(4096);
59 buf.put(&[0u8; 1024][..]);
60
61 b.iter(|| {
62 for _ in 0..128 {
63 test::black_box(&buf[..]);
64 test::black_box(&buf[..]);
65 test::black_box(&buf[..]);
66 test::black_box(&buf[..]);
67 test::black_box(&buf[..]);
68 test::black_box(&buf[..]);
69 test::black_box(&buf[..]);
70 test::black_box(&buf[..]);
71 }
72 })
73 }
74
75 #[bench]
76 fn deref_shared(b: &mut Bencher) {
77 let mut buf = BytesMut::with_capacity(4096);
78 buf.put(&[0u8; 1024][..]);
79 let _b2 = buf.split_off(1024);
80
81 b.iter(|| {
82 for _ in 0..1024 {
83 test::black_box(&buf[..]);
84 }
85 })
86 }
87
88 #[bench]
89 fn deref_inline(b: &mut Bencher) {
90 let mut buf = BytesMut::with_capacity(8);
91 buf.put(&[0u8; 8][..]);
92
93 b.iter(|| {
94 for _ in 0..1024 {
95 test::black_box(&buf[..]);
96 }
97 })
98 }
99
100 #[bench]
101 fn deref_two(b: &mut Bencher) {
102 let mut buf1 = BytesMut::with_capacity(8);
103 buf1.put(&[0u8; 8][..]);
104
105 let mut buf2 = BytesMut::with_capacity(4096);
106 buf2.put(&[0u8; 1024][..]);
107
108 b.iter(|| {
109 for _ in 0..512 {
110 test::black_box(&buf1[..]);
111 test::black_box(&buf2[..]);
112 }
113 })
114 }
115
116 #[bench]
117 fn clone_inline(b: &mut Bencher) {
118 let bytes = Bytes::from_static(b"hello world");
119
120 b.iter(|| {
121 for _ in 0..1024 {
122 test::black_box(&bytes.clone());
123 }
124 })
125 }
126
127 #[bench]
128 fn clone_static(b: &mut Bencher) {
129 let bytes = Bytes::from_static("hello world 1234567890 and have a good byte 0987654321".as_bytes());
130
131 b.iter(|| {
132 for _ in 0..1024 {
133 test::black_box(&bytes.clone());
134 }
135 })
136 }
137
138 #[bench]
139 fn clone_arc(b: &mut Bencher) {
140 let bytes = Bytes::from("hello world 1234567890 and have a good byte 0987654321".as_bytes());
141
142 b.iter(|| {
143 for _ in 0..1024 {
144 test::black_box(&bytes.clone());
145 }
146 })
147 }
148
149 #[bench]
150 fn alloc_write_split_to_mid(b: &mut Bencher) {
151 b.iter(|| {
152 let mut buf = BytesMut::with_capacity(128);
153 buf.put_slice(&[0u8; 64]);
154 test::black_box(buf.split_to(64));
155 })
156 }
157
158 #[bench]
159 fn drain_write_drain(b: &mut Bencher) {
160 let data = [0u8; 128];
161
162 b.iter(|| {
163 let mut buf = BytesMut::with_capacity(1024);
164 let mut parts = Vec::with_capacity(8);
165
166 for _ in 0..8 {
167 buf.put(&data[..]);
168 parts.push(buf.split_to(128));
169 }
170
171 test::black_box(parts);
172 })
173 }
174
175 #[bench]
176 fn fmt_write(b: &mut Bencher) {
177 use std::fmt::Write;
178 let mut buf = BytesMut::with_capacity(128);
179 let s = "foo bar baz quux lorem ipsum dolor et";
180
181 b.bytes = s.len() as u64;
182 b.iter(|| {
183 let _ = write!(buf, "{}", s);
184 test::black_box(&buf);
185 unsafe { buf.set_len(0); }
186 })
187 }
188
189 #[bench]
190 fn from_long_slice(b: &mut Bencher) {
191 let data = [0u8; 128];
192 b.bytes = data.len() as u64;
193 b.iter(|| {
194 let buf = BytesMut::from(&data[..]);
195 test::black_box(buf);
196 })
197 }
198
199 #[bench]
200 fn slice_empty(b: &mut Bencher) {
201 b.iter(|| {
202 let b = Bytes::from(vec![17; 1024]).clone();
203 for i in 0..1000 {
204 test::black_box(b.slice(i % 100, i % 100));
205 }
206 })
207 }
208
209 #[bench]
210 fn slice_short_from_arc(b: &mut Bencher) {
211 b.iter(|| {
212 // `clone` is to convert to ARC
213 let b = Bytes::from(vec![17; 1024]).clone();
214 for i in 0..1000 {
215 test::black_box(b.slice(1, 2 + i % 10));
216 }
217 })
218 }
219
220 // Keep in sync with bytes.rs
221 #[cfg(target_pointer_width = "64")]
222 const INLINE_CAP: usize = 4 * 8 - 1;
223 #[cfg(target_pointer_width = "32")]
224 const INLINE_CAP: usize = 4 * 4 - 1;
225
226 #[bench]
227 fn slice_avg_le_inline_from_arc(b: &mut Bencher) {
228 b.iter(|| {
229 // `clone` is to convert to ARC
230 let b = Bytes::from(vec![17; 1024]).clone();
231 for i in 0..1000 {
232 // [1, INLINE_CAP]
233 let len = 1 + i % (INLINE_CAP - 1);
234 test::black_box(b.slice(i % 10, i % 10 + len));
235 }
236 })
237 }
238
239 #[bench]
240 fn slice_large_le_inline_from_arc(b: &mut Bencher) {
241 b.iter(|| {
242 // `clone` is to convert to ARC
243 let b = Bytes::from(vec![17; 1024]).clone();
244 for i in 0..1000 {
245 // [INLINE_CAP - 10, INLINE_CAP]
246 let len = INLINE_CAP - 9 + i % 10;
247 test::black_box(b.slice(i % 10, i % 10 + len));
248 }
249 })
250 }