diff options
Diffstat (limited to 'benches')
-rw-r--r-- | benches/coded_input_stream.rs | 31 | ||||
-rw-r--r-- | benches/coded_output_stream.rs | 15 | ||||
-rw-r--r-- | benches/write.rs | 23 |
3 files changed, 69 insertions, 0 deletions
diff --git a/benches/coded_input_stream.rs b/benches/coded_input_stream.rs index d1795e5..10831da 100644 --- a/benches/coded_input_stream.rs +++ b/benches/coded_input_stream.rs @@ -102,3 +102,34 @@ fn read_varint_1(b: &mut Bencher) { assert_eq!(1000, count); }) } + +fn xorshift(mut x: u64) -> u64 { + x ^= x << 13; + x ^= x >> 7; + x ^= x << 17; + x +} + +#[bench] +fn read_varint_large(b: &mut Bencher) { + let mut v = Vec::new(); + { + let mut v = protobuf::CodedOutputStream::vec(&mut v); + let mut rng = 1; + for _ in 0..1000 { + // one or two byte varints + v.write_raw_varint64(rng).unwrap(); + rng = xorshift(rng); + } + v.flush().expect("flush"); + } + b.iter(|| { + let mut is = CodedInputStream::from_bytes(test::black_box(&v)); + let mut count = 0; + while !is.eof().expect("eof") { + test::black_box(is.read_raw_varint64().expect("read")); + count += 1; + } + assert_eq!(1000, count); + }) +} diff --git a/benches/coded_output_stream.rs b/benches/coded_output_stream.rs index 9edf95c..8aed4d8 100644 --- a/benches/coded_output_stream.rs +++ b/benches/coded_output_stream.rs @@ -48,3 +48,18 @@ fn bench_buffer_bytes(b: &mut Bencher) { v }); } + +#[bench] +fn bench_write_raw_varint_32(b: &mut Bencher) { + let mut v = Vec::with_capacity(10_000); + b.iter(|| { + v.clear(); + { + let mut os = CodedOutputStream::new(&mut v); + for i in 0..1000 { + os.write_raw_varint32(i * 139 % 1000).unwrap(); + } + } + v.len() + }) +} diff --git a/benches/write.rs b/benches/write.rs new file mode 100644 index 0000000..3b49831 --- /dev/null +++ b/benches/write.rs @@ -0,0 +1,23 @@ +// `cargo test --benches` and `#[feature(test)]` work only in nightly +#![cfg(rustc_nightly)] +#![feature(test)] + +extern crate test; + +use protobuf::well_known_types::struct_::value; +use protobuf::well_known_types::struct_::Struct; +use protobuf::well_known_types::struct_::Value; +use protobuf::Message; +use test::Bencher; + +#[bench] +fn write_to_bytes(b: &mut Bencher) { + let mut value = Value::new(); + value.kind = Some(value::Kind::NumberValue(10.0)); + let mut value2 = Value::new(); + value2.kind = Some(value::Kind::BoolValue(true)); + let mut s = Struct::new(); + s.fields.insert("foo".to_owned(), value); + s.fields.insert("bar".to_owned(), value2); + b.iter(|| s.write_to_bytes()); +} |