2015-11-25 20:05:11 +00:00
|
|
|
use test::Bencher;
|
|
|
|
|
|
|
|
use std::str::FromStr;
|
|
|
|
use std::net::ToSocketAddrs;
|
|
|
|
|
|
|
|
use super::udpmessage::{Options, Message, encode, decode};
|
2015-12-22 13:43:02 +00:00
|
|
|
use super::crypto::{Crypto, CryptoMethod};
|
2015-11-25 20:05:11 +00:00
|
|
|
use super::ethernet::{Frame, SwitchTable};
|
|
|
|
use super::types::{Address, Table, Protocol};
|
|
|
|
use super::ip::Packet;
|
|
|
|
|
2015-12-22 13:43:02 +00:00
|
|
|
#[bench]
|
|
|
|
fn crypto_salsa20(b: &mut Bencher) {
|
|
|
|
let mut crypto = Crypto::from_shared_key(CryptoMethod::ChaCha20, "test");
|
|
|
|
let mut payload = [0; 1500];
|
|
|
|
let header = [0; 8];
|
|
|
|
let mut nonce_bytes = [0; 12];
|
|
|
|
b.iter(|| {
|
|
|
|
let len = crypto.encrypt(&mut payload, 1400, &mut nonce_bytes, &header);
|
|
|
|
assert!(crypto.decrypt(&mut payload[..len], &nonce_bytes, &header).is_ok())
|
|
|
|
});
|
|
|
|
b.bytes = 1400;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn crypto_aes256(b: &mut Bencher) {
|
|
|
|
if !Crypto::aes256_available() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let mut crypto = Crypto::from_shared_key(CryptoMethod::AES256, "test");
|
|
|
|
let mut payload = [0; 1500];
|
|
|
|
let header = [0; 8];
|
|
|
|
let mut nonce_bytes = [0; 12];
|
|
|
|
b.iter(|| {
|
|
|
|
let len = crypto.encrypt(&mut payload, 1400, &mut nonce_bytes, &header);
|
|
|
|
assert!(crypto.decrypt(&mut payload[..len], &nonce_bytes, &header).is_ok());
|
|
|
|
});
|
|
|
|
b.bytes = 1400;
|
|
|
|
}
|
2015-11-25 20:05:11 +00:00
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn message_encode(b: &mut Bencher) {
|
|
|
|
let mut options = Options::default();
|
|
|
|
let mut crypto = Crypto::None;
|
2015-12-13 21:10:47 +00:00
|
|
|
let mut payload = [0; 1600];
|
|
|
|
let mut msg = Message::Data(&mut payload, 64, 1464);
|
|
|
|
let mut buf = [0; 1600];
|
2015-11-25 20:05:11 +00:00
|
|
|
b.iter(|| {
|
2015-12-13 21:10:47 +00:00
|
|
|
encode(&mut options, &mut msg, &mut buf[..], &mut crypto);
|
2015-11-25 20:05:11 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn message_decode(b: &mut Bencher) {
|
|
|
|
let mut options = Options::default();
|
|
|
|
let mut crypto = Crypto::None;
|
2015-12-13 21:10:47 +00:00
|
|
|
let mut payload = [0; 1600];
|
|
|
|
let mut msg = Message::Data(&mut payload, 64, 1464);
|
|
|
|
let mut buf = [0; 1600];
|
|
|
|
let mut res = encode(&mut options, &mut msg, &mut buf[..], &mut crypto);
|
2015-11-25 20:05:11 +00:00
|
|
|
b.iter(|| {
|
2015-12-13 21:10:47 +00:00
|
|
|
decode(&mut res, &mut crypto).unwrap();
|
2015-11-25 20:05:11 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn switch_learn(b: &mut Bencher) {
|
2015-11-26 09:45:25 +00:00
|
|
|
let mut table = SwitchTable::new(10);
|
2015-11-25 20:05:11 +00:00
|
|
|
let addr = Address::from_str("12:34:56:78:90:ab").unwrap();
|
|
|
|
let peer = "1.2.3.4:5678".to_socket_addrs().unwrap().next().unwrap();
|
|
|
|
b.iter(|| {
|
|
|
|
table.learn(addr.clone(), None, peer);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn switch_lookup(b: &mut Bencher) {
|
2015-11-26 09:45:25 +00:00
|
|
|
let mut table = SwitchTable::new(10);
|
2015-11-25 20:05:11 +00:00
|
|
|
let addr = Address::from_str("12:34:56:78:90:ab").unwrap();
|
|
|
|
let peer = "1.2.3.4:5678".to_socket_addrs().unwrap().next().unwrap();
|
|
|
|
table.learn(addr.clone(), None, peer);
|
|
|
|
b.iter(|| {
|
|
|
|
table.lookup(&addr);
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn ethernet_parse(b: &mut Bencher) {
|
2015-11-25 20:55:30 +00:00
|
|
|
let mut data = [0; 1500];
|
|
|
|
data[5] = 45;
|
2015-11-25 20:05:11 +00:00
|
|
|
b.iter(|| {
|
|
|
|
Frame::parse(&data).unwrap()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn ipv4_parse(b: &mut Bencher) {
|
|
|
|
let mut data = [0; 1500];
|
|
|
|
data[0] = 4*16;
|
|
|
|
b.iter(|| {
|
|
|
|
Packet::parse(&data).unwrap()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn ipv6_parse(b: &mut Bencher) {
|
|
|
|
let mut data = [0; 1500];
|
|
|
|
data[0] = 6*16;
|
|
|
|
b.iter(|| {
|
|
|
|
Packet::parse(&data).unwrap()
|
|
|
|
})
|
|
|
|
}
|