|
| 1 | +#![feature(test)] |
| 2 | + |
| 3 | +extern crate bytes; |
| 4 | +extern crate test; |
| 5 | + |
| 6 | +use test::Bencher; |
| 7 | +use 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 deref_unique(b: &mut Bencher) { |
| 34 | + let mut buf = BytesMut::with_capacity(4096); |
| 35 | + buf.put(&[0u8; 1024][..]); |
| 36 | + |
| 37 | + b.iter(|| { |
| 38 | + for _ in 0..1024 { |
| 39 | + test::black_box(&buf[..]); |
| 40 | + } |
| 41 | + }) |
| 42 | +} |
| 43 | + |
| 44 | +#[bench] |
| 45 | +fn deref_unique_unroll(b: &mut Bencher) { |
| 46 | + let mut buf = BytesMut::with_capacity(4096); |
| 47 | + buf.put(&[0u8; 1024][..]); |
| 48 | + |
| 49 | + b.iter(|| { |
| 50 | + for _ in 0..128 { |
| 51 | + test::black_box(&buf[..]); |
| 52 | + test::black_box(&buf[..]); |
| 53 | + test::black_box(&buf[..]); |
| 54 | + test::black_box(&buf[..]); |
| 55 | + test::black_box(&buf[..]); |
| 56 | + test::black_box(&buf[..]); |
| 57 | + test::black_box(&buf[..]); |
| 58 | + test::black_box(&buf[..]); |
| 59 | + } |
| 60 | + }) |
| 61 | +} |
| 62 | + |
| 63 | +#[bench] |
| 64 | +fn deref_shared(b: &mut Bencher) { |
| 65 | + let mut buf = BytesMut::with_capacity(4096); |
| 66 | + buf.put(&[0u8; 1024][..]); |
| 67 | + let _b2 = buf.split_off(1024); |
| 68 | + |
| 69 | + b.iter(|| { |
| 70 | + for _ in 0..1024 { |
| 71 | + test::black_box(&buf[..]); |
| 72 | + } |
| 73 | + }) |
| 74 | +} |
| 75 | + |
| 76 | +#[bench] |
| 77 | +fn deref_inline(b: &mut Bencher) { |
| 78 | + let mut buf = BytesMut::with_capacity(8); |
| 79 | + buf.put(&[0u8; 8][..]); |
| 80 | + |
| 81 | + b.iter(|| { |
| 82 | + for _ in 0..1024 { |
| 83 | + test::black_box(&buf[..]); |
| 84 | + } |
| 85 | + }) |
| 86 | +} |
| 87 | + |
| 88 | +#[bench] |
| 89 | +fn deref_two(b: &mut Bencher) { |
| 90 | + let mut buf1 = BytesMut::with_capacity(8); |
| 91 | + buf1.put(&[0u8; 8][..]); |
| 92 | + |
| 93 | + let mut buf2 = BytesMut::with_capacity(4096); |
| 94 | + buf2.put(&[0u8; 1024][..]); |
| 95 | + |
| 96 | + b.iter(|| { |
| 97 | + for _ in 0..512 { |
| 98 | + test::black_box(&buf1[..]); |
| 99 | + test::black_box(&buf2[..]); |
| 100 | + } |
| 101 | + }) |
| 102 | +} |
| 103 | + |
| 104 | +#[bench] |
| 105 | +fn alloc_write_split_to_mid(b: &mut Bencher) { |
| 106 | + b.iter(|| { |
| 107 | + let mut buf = BytesMut::with_capacity(128); |
| 108 | + buf.put_slice(&[0u8; 64]); |
| 109 | + test::black_box(buf.split_to(64)); |
| 110 | + }) |
| 111 | +} |
| 112 | + |
| 113 | +#[bench] |
| 114 | +fn drain_write_drain(b: &mut Bencher) { |
| 115 | + let data = [0u8; 128]; |
| 116 | + |
| 117 | + b.iter(|| { |
| 118 | + let mut buf = BytesMut::with_capacity(1024); |
| 119 | + let mut parts = Vec::with_capacity(8); |
| 120 | + |
| 121 | + for _ in 0..8 { |
| 122 | + buf.put(&data[..]); |
| 123 | + parts.push(buf.split_to(128)); |
| 124 | + } |
| 125 | + |
| 126 | + test::black_box(parts); |
| 127 | + }) |
| 128 | +} |
0 commit comments