Updates tests and spec

This commit is contained in:
Dennis Schwerdel 2015-11-20 11:33:32 +01:00
parent c038209026
commit a448a26e7f
3 changed files with 10 additions and 128 deletions

View File

@ -1,118 +0,0 @@
#![feature(test)]
extern crate test;
extern crate time;
use std::net::{SocketAddr, ToSocketAddrs, SocketAddrV4, Ipv4Addr};
use std::collections::HashMap;
use time::{Duration, SteadyTime};
use test::Bencher;
struct PeerListHashMap {
timeout: Duration,
peers: HashMap<SocketAddr, SteadyTime>
}
impl PeerListHashMap {
fn new(timeout: Duration) -> Self {
PeerListHashMap{peers: HashMap::new(), timeout: timeout}
}
fn add(&mut self, addr: &SocketAddr) {
if self.peers.insert(*addr, SteadyTime::now()+self.timeout).is_none() {
}
}
}
struct PeerListVec {
timeout: Duration,
peers: Vec<(SocketAddr, SteadyTime)>
}
impl PeerListVec {
fn new(timeout: Duration) -> Self {
PeerListVec{peers: Vec::new(), timeout: timeout}
}
fn add(&mut self, addr: &SocketAddr) {
for &(ref peer, ref timeout) in &self.peers {
if peer == addr {
return;
}
}
self.peers.push((*addr, SteadyTime::now()+self.timeout));
}
}
fn bench_hashmap_add_n(b: &mut Bencher, n: u16) {
let mut peers = PeerListHashMap::new(Duration::seconds(60));
for i in 0..n {
peers.add(&SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(0, 0, 0, 1), i)));
}
let addr = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(0, 0, 0, 0), 0));
b.iter(|| {
peers.add(&addr)
})
}
#[bench]
fn bench_hashmap_add_0(b: &mut Bencher) {
bench_hashmap_add_n(b, 0);
}
#[bench]
fn bench_hashmap_add_1(b: &mut Bencher) {
bench_hashmap_add_n(b, 1);
}
#[bench]
fn bench_hashmap_add_10(b: &mut Bencher) {
bench_hashmap_add_n(b, 10);
}
#[bench]
fn bench_hashmap_add_100(b: &mut Bencher) {
bench_hashmap_add_n(b, 100);
}
#[bench]
fn bench_hashmap_add_1000(b: &mut Bencher) {
bench_hashmap_add_n(b, 1000);
}
fn bench_vec_add_n(b: &mut Bencher, n: u16) {
let mut peers = PeerListVec::new(Duration::seconds(60));
for i in 0..n {
peers.add(&SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(0, 0, 0, 1), i)));
}
let addr = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(0, 0, 0, 0), 0));
b.iter(|| {
peers.add(&addr)
})
}
#[bench]
fn bench_vec_add_0(b: &mut Bencher) {
bench_vec_add_n(b, 0);
}
#[bench]
fn bench_vec_add_1(b: &mut Bencher) {
bench_vec_add_n(b, 1);
}
#[bench]
fn bench_vec_add_10(b: &mut Bencher) {
bench_vec_add_n(b, 10);
}
#[bench]
fn bench_vec_add_100(b: &mut Bencher) {
bench_vec_add_n(b, 100);
}
#[bench]
fn bench_vec_add_1000(b: &mut Bencher) {
bench_vec_add_n(b, 1000);
}

View File

@ -49,10 +49,10 @@ peers. For nodes behind a firewall or a NAT, this can function as hole-punching.
The protocol of `ethcloud` is kept as simple as possible to allow other The protocol of `ethcloud` is kept as simple as possible to allow other
implementations and to maximize the performance. implementations and to maximize the performance.
The first 8 bytes of each packet are the token that is used to distinguish The first 7 bytes of each packet are the token that is used to distinguish
different networks and sort out stray packets that do not belong. different networks and sort out stray packets that do not belong.
After that, the 9th byte is a switch that determines the structure of the rest After that, the 8th byte is a switch that determines the structure of the rest
of the packet: of the packet:
* **Frame packet** (value `0`): * **Frame packet** (value `0`):

View File

@ -143,8 +143,8 @@ fn encode_message_packet() {
let msg = Message::Frame(ethernet::Frame{src: &src, dst: &dst, vlan: 0, payload: &payload}); let msg = Message::Frame(ethernet::Frame{src: &src, dst: &dst, vlan: 0, payload: &payload});
let mut buf = [0; 1024]; let mut buf = [0; 1024];
let size = encode(token, &msg, &mut buf[..]); let size = encode(token, &msg, &mut buf[..]);
assert_eq!(size, 26); assert_eq!(size, 25);
assert_eq!(&buf[..9], &[0,0,0,0,0,0,0,134,0]); assert_eq!(&buf[..8], &[0,0,0,0,0,0,134,0]);
let (token2, msg2) = decode(&buf[..size]).unwrap(); let (token2, msg2) = decode(&buf[..size]).unwrap();
assert_eq!(token, token2); assert_eq!(token, token2);
assert_eq!(msg, msg2); assert_eq!(msg, msg2);
@ -157,8 +157,8 @@ fn encode_message_peers() {
let msg = Message::Peers(vec![SocketAddr::from_str("1.2.3.4:123").unwrap(), SocketAddr::from_str("5.6.7.8:12345").unwrap()]); let msg = Message::Peers(vec![SocketAddr::from_str("1.2.3.4:123").unwrap(), SocketAddr::from_str("5.6.7.8:12345").unwrap()]);
let mut buf = [0; 1024]; let mut buf = [0; 1024];
let size = encode(token, &msg, &mut buf[..]); let size = encode(token, &msg, &mut buf[..]);
assert_eq!(size, 23); assert_eq!(size, 22);
assert_eq!(&buf[..size], &[0,0,0,0,0,0,0,134,1,2,1,2,3,4,0,123,5,6,7,8,48,57,0]); assert_eq!(&buf[..size], &[0,0,0,0,0,0,134,1,2,1,2,3,4,0,123,5,6,7,8,48,57,0]);
let (token2, msg2) = decode(&buf[..size]).unwrap(); let (token2, msg2) = decode(&buf[..size]).unwrap();
assert_eq!(token, token2); assert_eq!(token, token2);
assert_eq!(msg, msg2); assert_eq!(msg, msg2);
@ -170,8 +170,8 @@ fn encode_message_getpeers() {
let msg = Message::GetPeers; let msg = Message::GetPeers;
let mut buf = [0; 1024]; let mut buf = [0; 1024];
let size = encode(token, &msg, &mut buf[..]); let size = encode(token, &msg, &mut buf[..]);
assert_eq!(size, 9); assert_eq!(size, 8);
assert_eq!(&buf[..size], &[0,0,0,0,0,0,0,134,2]); assert_eq!(&buf[..size], &[0,0,0,0,0,0,134,2]);
let (token2, msg2) = decode(&buf[..size]).unwrap(); let (token2, msg2) = decode(&buf[..size]).unwrap();
assert_eq!(token, token2); assert_eq!(token, token2);
assert_eq!(msg, msg2); assert_eq!(msg, msg2);
@ -183,8 +183,8 @@ fn encode_message_close() {
let msg = Message::Close; let msg = Message::Close;
let mut buf = [0; 1024]; let mut buf = [0; 1024];
let size = encode(token, &msg, &mut buf[..]); let size = encode(token, &msg, &mut buf[..]);
assert_eq!(size, 9); assert_eq!(size, 8);
assert_eq!(&buf[..size], &[0,0,0,0,0,0,0,134,3]); assert_eq!(&buf[..size], &[0,0,0,0,0,0,134,3]);
let (token2, msg2) = decode(&buf[..size]).unwrap(); let (token2, msg2) = decode(&buf[..size]).unwrap();
assert_eq!(token, token2); assert_eq!(token, token2);
assert_eq!(msg, msg2); assert_eq!(msg, msg2);