From 67692b21b5c66c391f5f85c918149d4a98e321c4 Mon Sep 17 00:00:00 2001 From: Corey Richardson Date: Tue, 12 Nov 2013 12:38:54 -0500 Subject: [PATCH] io: benchmarks for creation of the various Buffered objects --- src/libstd/io/buffered.rs | 42 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/libstd/io/buffered.rs b/src/libstd/io/buffered.rs index 4932f392090..68fda813a1f 100644 --- a/src/libstd/io/buffered.rs +++ b/src/libstd/io/buffered.rs @@ -351,6 +351,27 @@ mod test { use super::*; use io; use super::super::mem::{MemReader, MemWriter}; + use Harness = extra::test::BenchHarness; + + /// A type, free to create, primarily intended for benchmarking creation of wrappers that, just + /// for construction, don't need a Reader/Writer that does anything useful. Is equivalent to + /// `/dev/null` in semantics. + #[deriving(Clone,Eq,Ord)] + pub struct NullStream; + + impl Reader for NullStream { + fn read(&mut self, _: &mut [u8]) -> Option { + None + } + + fn eof(&mut self) -> bool { + true + } + } + + impl Writer for NullStream { + fn write(&mut self, _: &[u8]) { } + } #[test] fn test_buffered_reader() { @@ -470,4 +491,25 @@ mod test { writer.flush(); assert_eq!(*writer.inner_ref().inner_ref(), ~[0, 1, 0, '\n' as u8, 1]); } + + #[bench] + fn bench_buffered_reader(bh: &mut Harness) { + do bh.iter { + BufferedReader::new(NullStream); + } + } + + #[bench] + fn bench_buffered_writer(bh: &mut Harness) { + do bh.iter { + BufferedWriter::new(NullStream); + } + } + + #[bench] + fn bench_buffered_stream(bh: &mut Harness) { + do bh.iter { + BufferedStream::new(NullStream); + } + } }