Skip to content

Commit

Permalink
+ benches
Browse files Browse the repository at this point in the history
  • Loading branch information
chpio committed May 23, 2017
1 parent c7afe32 commit e090776
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions benches/benches.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#![feature(test)]

extern crate test;
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate serde_bencode;

use test::Bencher;
use serde::Serialize;
use serde_bencode::ser::Serializer;
use serde_bencode::de::from_bytes;


#[bench]
fn ser_de_simple(b: &mut Bencher) {
#[derive(Serialize, Deserialize)]
struct Fake {
a: i64,
b: i64,
}

b.iter(|| {
let a = Fake {a: 2, b: 7};
let mut ser = Serializer::new();
a.serialize(&mut ser).unwrap();
let a_bytes: Vec<u8> = ser.into();
let b: Fake = from_bytes(a_bytes.as_ref()).unwrap();
b
});
}

#[bench]
fn ser_de_nested(b: &mut Bencher) {
#[derive(Serialize, Deserialize)]
struct FakeA {
a: i64,
b: i64,
}

#[derive(Serialize, Deserialize)]
struct FakeB {
a: i64,
b: FakeA,
}

b.iter(|| {
let a = FakeB {a: 2, b: FakeA {a: 7, b: 9}};
let mut ser = Serializer::new();
a.serialize(&mut ser).unwrap();
let a_bytes: Vec<u8> = ser.into();
let b: FakeB = from_bytes(a_bytes.as_ref()).unwrap();
b
});
}

0 comments on commit e090776

Please sign in to comment.