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]
deref_unique(b: &mut Bencher)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]
deref_shared(b: &mut Bencher)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]
deref_static(b: &mut Bencher)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]
clone_static(b: &mut Bencher)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]
clone_shared(b: &mut Bencher)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]
clone_arc_vec(b: &mut Bencher)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]
from_long_slice(b: &mut Bencher)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]
slice_empty(b: &mut Bencher)89 fn slice_empty(b: &mut Bencher) {
90     b.iter(|| {
91         let b = Bytes::from(vec![17; 1024]).clone();
92         for i in 0..1000 {
93             test::black_box(b.slice(i % 100..i % 100));
94         }
95     })
96 }
97 
98 #[bench]
slice_short_from_arc(b: &mut Bencher)99 fn slice_short_from_arc(b: &mut Bencher) {
100     b.iter(|| {
101         // `clone` is to convert to ARC
102         let b = Bytes::from(vec![17; 1024]).clone();
103         for i in 0..1000 {
104             test::black_box(b.slice(1..2 + i % 10));
105         }
106     })
107 }
108 
109 #[bench]
split_off_and_drop(b: &mut Bencher)110 fn split_off_and_drop(b: &mut Bencher) {
111     b.iter(|| {
112         for _ in 0..1024 {
113             let v = vec![10; 200];
114             let mut b = Bytes::from(v);
115             test::black_box(b.split_off(100));
116             test::black_box(b);
117         }
118     })
119 }
120