1 | #![feature (test)] |
2 | #![warn (rust_2018_idioms)] |
3 | |
4 | extern crate test; |
5 | |
6 | use bytes::Bytes; |
7 | use test::Bencher; |
8 | |
9 | #[bench] |
10 | fn deref_unique(b: &mut Bencher) { |
11 | let buf = Bytes::from(vec![0; 1024]); |
12 | |
13 | b.iter(|| { |
14 | for _ in 0..1024 { |
15 | test::black_box(&buf[..]); |
16 | } |
17 | }) |
18 | } |
19 | |
20 | #[bench] |
21 | fn deref_shared(b: &mut Bencher) { |
22 | let buf = Bytes::from(vec![0; 1024]); |
23 | let _b2 = buf.clone(); |
24 | |
25 | b.iter(|| { |
26 | for _ in 0..1024 { |
27 | test::black_box(&buf[..]); |
28 | } |
29 | }) |
30 | } |
31 | |
32 | #[bench] |
33 | fn deref_static(b: &mut Bencher) { |
34 | let buf = Bytes::from_static(b"hello world" ); |
35 | |
36 | b.iter(|| { |
37 | for _ in 0..1024 { |
38 | test::black_box(&buf[..]); |
39 | } |
40 | }) |
41 | } |
42 | |
43 | #[bench] |
44 | fn clone_static(b: &mut Bencher) { |
45 | let bytes = |
46 | Bytes::from_static("hello world 1234567890 and have a good byte 0987654321" .as_bytes()); |
47 | |
48 | b.iter(|| { |
49 | for _ in 0..1024 { |
50 | test::black_box(&bytes.clone()); |
51 | } |
52 | }) |
53 | } |
54 | |
55 | #[bench] |
56 | fn clone_shared(b: &mut Bencher) { |
57 | let bytes = Bytes::from(b"hello world 1234567890 and have a good byte 0987654321" .to_vec()); |
58 | |
59 | b.iter(|| { |
60 | for _ in 0..1024 { |
61 | test::black_box(&bytes.clone()); |
62 | } |
63 | }) |
64 | } |
65 | |
66 | #[bench] |
67 | fn clone_arc_vec(b: &mut Bencher) { |
68 | use std::sync::Arc; |
69 | let bytes = Arc::new(b"hello world 1234567890 and have a good byte 0987654321" .to_vec()); |
70 | |
71 | b.iter(|| { |
72 | for _ in 0..1024 { |
73 | test::black_box(&bytes.clone()); |
74 | } |
75 | }) |
76 | } |
77 | |
78 | #[bench] |
79 | fn from_long_slice(b: &mut Bencher) { |
80 | let data = [0u8; 128]; |
81 | b.bytes = data.len() as u64; |
82 | b.iter(|| { |
83 | let buf = Bytes::copy_from_slice(&data[..]); |
84 | test::black_box(buf); |
85 | }) |
86 | } |
87 | |
88 | #[bench] |
89 | fn slice_empty(b: &mut Bencher) { |
90 | b.iter(|| { |
91 | // `clone` is to convert to ARC |
92 | let b = Bytes::from(vec![17; 1024]).clone(); |
93 | for i in 0..1000 { |
94 | test::black_box(b.slice(i % 100..i % 100)); |
95 | } |
96 | }) |
97 | } |
98 | |
99 | #[bench] |
100 | fn slice_short_from_arc(b: &mut Bencher) { |
101 | b.iter(|| { |
102 | // `clone` is to convert to ARC |
103 | let b = Bytes::from(vec![17; 1024]).clone(); |
104 | for i in 0..1000 { |
105 | test::black_box(b.slice(1..2 + i % 10)); |
106 | } |
107 | }) |
108 | } |
109 | |
110 | #[bench] |
111 | fn split_off_and_drop(b: &mut Bencher) { |
112 | b.iter(|| { |
113 | for _ in 0..1024 { |
114 | let v = vec![10; 200]; |
115 | let mut b = Bytes::from(v); |
116 | test::black_box(b.split_off(100)); |
117 | test::black_box(b); |
118 | } |
119 | }) |
120 | } |
121 | |