1 // `cargo test --benches` and `#[feature(test)]` work only in nightly 2 #![cfg(rustc_nightly)] 3 #![feature(test)] 4 5 extern crate protobuf; 6 extern crate test; 7 8 use self::test::Bencher; 9 use protobuf::CodedOutputStream; 10 11 #[inline] buffer_write_byte(os: &mut CodedOutputStream)12fn buffer_write_byte(os: &mut CodedOutputStream) { 13 for i in 0..10 { 14 os.write_raw_byte(test::black_box(i as u8)).unwrap(); 15 } 16 os.flush().unwrap(); 17 } 18 19 #[inline] buffer_write_bytes(os: &mut CodedOutputStream)20fn buffer_write_bytes(os: &mut CodedOutputStream) { 21 for _ in 0..10 { 22 os.write_raw_bytes(test::black_box(b"1234567890")).unwrap(); 23 } 24 os.flush().unwrap(); 25 } 26 27 #[bench] bench_buffer(b: &mut Bencher)28fn bench_buffer(b: &mut Bencher) { 29 b.iter(|| { 30 let mut v = Vec::new(); 31 { 32 let mut os = CodedOutputStream::new(&mut v); 33 buffer_write_byte(&mut os); 34 } 35 v 36 }); 37 } 38 39 #[bench] bench_buffer_bytes(b: &mut Bencher)40fn bench_buffer_bytes(b: &mut Bencher) { 41 b.iter(|| { 42 let mut v = Vec::new(); 43 { 44 let mut os = CodedOutputStream::new(&mut v); 45 buffer_write_bytes(&mut os); 46 } 47 v 48 }); 49 } 50