From 4f6b27eec1b89bf4be22cbc4cc18c1c95121f82c Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Thu, 13 Aug 2015 22:30:02 -0700 Subject: [PATCH] Black box json benchmark numbers This prevents the optimizer from optimizing away the thing we're benchmarking. --- serde_tests/benches/bench_log.rs | 46 +++++++++++++++++++------------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/serde_tests/benches/bench_log.rs b/serde_tests/benches/bench_log.rs index 9a4ccd17..1dddcf50 100644 --- a/serde_tests/benches/bench_log.rs +++ b/serde_tests/benches/bench_log.rs @@ -1,6 +1,6 @@ use std::io::{self, Read, Write}; use num::FromPrimitive; -use test::Bencher; +use test::{Bencher, black_box}; use rustc_serialize; @@ -1093,7 +1093,7 @@ fn test_encoder() { { let mut encoder = rustc_serialize::json::Encoder::new(&mut wr); - log.encode(&mut encoder).unwrap(); + log.encode(&mut encoder).unwrap() } assert_eq!(&wr, &JSON_STR); @@ -1116,7 +1116,7 @@ fn bench_encoder(b: &mut Bencher) { wr.clear(); let mut encoder = rustc_serialize::json::Encoder::new(&mut wr); - log.encode(&mut encoder).unwrap(); + log.encode(&mut encoder).unwrap() }); } @@ -1134,7 +1134,7 @@ fn bench_serializer(b: &mut Bencher) { b.bytes = json.len() as u64; b.iter(|| { - serde_json::to_vec(&log).unwrap(); + serde_json::to_vec(&log).unwrap() }); } @@ -1162,7 +1162,8 @@ fn bench_serializer_vec(b: &mut Bencher) { let mut serializer = serde_json::Serializer::new(wr.by_ref()); log.serialize(&mut serializer).unwrap(); - let _json = serializer.into_inner(); + let json = serializer.into_inner(); + black_box(json); }); } @@ -1180,7 +1181,8 @@ fn bench_serializer_slice(b: &mut Bencher) { let mut serializer = serde_json::Serializer::new(wr.by_ref()); log.serialize(&mut serializer).unwrap(); - let _json = serializer.into_inner(); + let json = serializer.into_inner(); + black_box(json); }); } @@ -1193,7 +1195,8 @@ fn test_serializer_my_mem_writer0() { { let mut serializer = serde_json::Serializer::new(wr.by_ref()); log.serialize(&mut serializer).unwrap(); - let _json = serializer.into_inner(); + let json = serializer.into_inner(); + black_box(json); } assert_eq!(&wr.buf, &JSON_STR.as_bytes()); @@ -1212,7 +1215,8 @@ fn bench_serializer_my_mem_writer0(b: &mut Bencher) { let mut serializer = serde_json::Serializer::new(wr.by_ref()); log.serialize(&mut serializer).unwrap(); - let _json = serializer.into_inner(); + let json = serializer.into_inner(); + black_box(json); }); } @@ -1225,7 +1229,8 @@ fn test_serializer_my_mem_writer1() { { let mut serializer = serde_json::Serializer::new(wr.by_ref()); log.serialize(&mut serializer).unwrap(); - let _json = serializer.into_inner(); + let json = serializer.into_inner(); + black_box(json); } assert_eq!(&wr.buf, &JSON_STR.as_bytes()); @@ -1244,7 +1249,8 @@ fn bench_serializer_my_mem_writer1(b: &mut Bencher) { let mut serializer = serde_json::Serializer::new(wr.by_ref()); log.serialize(&mut serializer).unwrap(); - let _json = serializer.into_inner(); + let json = serializer.into_inner(); + black_box(json); }); } @@ -1254,7 +1260,7 @@ fn bench_copy(b: &mut Bencher) { b.bytes = json.len() as u64; b.iter(|| { - let _json = JSON_STR.as_bytes().to_vec(); + JSON_STR.as_bytes().to_vec() }); } @@ -1441,7 +1447,7 @@ fn bench_manual_serialize_vec_no_escape(b: &mut Bencher) { b.iter(|| { wr.clear(); - manual_serialize_no_escape(&mut wr, &log); + manual_serialize_no_escape(&mut wr, &log) }); } @@ -1467,7 +1473,7 @@ fn bench_manual_serialize_vec_escape(b: &mut Bencher) { b.iter(|| { wr.clear(); - manual_serialize_escape(&mut wr, &log); + manual_serialize_escape(&mut wr, &log) }); } @@ -1493,7 +1499,7 @@ fn bench_manual_serialize_my_mem_writer0_no_escape(b: &mut Bencher) { b.iter(|| { wr.buf.clear(); - manual_serialize_no_escape(&mut wr, &log); + manual_serialize_no_escape(&mut wr, &log) }); } @@ -1519,7 +1525,7 @@ fn bench_manual_serialize_my_mem_writer0_escape(b: &mut Bencher) { b.iter(|| { wr.buf.clear(); - manual_serialize_escape(&mut wr, &log); + manual_serialize_escape(&mut wr, &log) }); } @@ -1545,7 +1551,7 @@ fn bench_manual_serialize_my_mem_writer1_no_escape(b: &mut Bencher) { b.iter(|| { wr.buf.clear(); - manual_serialize_no_escape(&mut wr, &log); + manual_serialize_no_escape(&mut wr, &log) }); } @@ -1571,7 +1577,7 @@ fn bench_manual_serialize_my_mem_writer1_escape(b: &mut Bencher) { b.iter(|| { wr.buf.clear(); - manual_serialize_escape(&mut wr, &log); + manual_serialize_escape(&mut wr, &log) }); } @@ -1584,7 +1590,8 @@ fn bench_decoder(b: &mut Bencher) { b.iter(|| { let json = Json::from_str(JSON_STR).unwrap(); let mut decoder = rustc_serialize::json::Decoder::new(json); - let _log: Log = rustc_serialize::Decodable::decode(&mut decoder).unwrap(); + let log: Log = rustc_serialize::Decodable::decode(&mut decoder).unwrap(); + log }); } @@ -1593,6 +1600,7 @@ fn bench_deserializer(b: &mut Bencher) { b.bytes = JSON_STR.len() as u64; b.iter(|| { - let _log: Log = serde_json::from_str(JSON_STR).unwrap(); + let log: Log = serde_json::from_str(JSON_STR).unwrap(); + log }); }