Extensive tests and benchmarks

This commit is contained in:
Dennis Schwerdel 2016-02-08 20:37:06 +01:00
parent c07d772b90
commit 60de5d069f
3 changed files with 84 additions and 3 deletions

View File

@ -5,13 +5,19 @@
use test::Bencher; use test::Bencher;
use std::str::FromStr; use std::str::FromStr;
use std::net::ToSocketAddrs; use std::net::{UdpSocket, ToSocketAddrs, Ipv4Addr, SocketAddr, SocketAddrV4};
use std::os::unix::io::AsRawFd;
use super::cloud::GenericCloud;
use super::device::Device;
use super::udpmessage::{Options, Message, encode, decode}; use super::udpmessage::{Options, Message, encode, decode};
use super::crypto::{Crypto, CryptoMethod}; use super::crypto::{Crypto, CryptoMethod};
use super::ethernet::{Frame, SwitchTable}; use super::ethernet::{Frame, SwitchTable};
use super::types::{Address, Table, Protocol}; use super::types::{Address, Table, Protocol};
use super::ip::Packet; use super::ip::Packet;
use super::util::now as util_now;
use epoll;
#[bench] #[bench]
fn crypto_salsa20(b: &mut Bencher) { fn crypto_salsa20(b: &mut Bencher) {
@ -117,3 +123,62 @@ fn ipv6_parse(b: &mut Bencher) {
Packet::parse(&data).unwrap() Packet::parse(&data).unwrap()
}) })
} }
#[bench]
fn now(b: &mut Bencher) {
b.iter(|| {
util_now()
})
}
#[bench]
fn epoll_wait(b: &mut Bencher) {
let socket = UdpSocket::bind("0.0.0.0:0").unwrap();
let epoll_handle = epoll::create1(0).unwrap();
let fd = socket.as_raw_fd();
let mut event = epoll::EpollEvent{events: epoll::util::event_type::EPOLLOUT, data: 0};
epoll::ctl(epoll_handle, epoll::util::ctl_op::ADD, fd, &mut event).unwrap();
let mut events = [epoll::EpollEvent{events: 0, data: 0}; 1];
b.iter(|| {
epoll::wait(epoll_handle, &mut events, 1000).unwrap()
});
}
#[bench]
fn handle_interface_data(b: &mut Bencher) {
let mut node = GenericCloud::<Frame>::new(
Device::dummy("vpncloud0", "/dev/null").unwrap(), "127.0.0.1:0", None,
Box::new(SwitchTable::new(300)), 1800, true, true, vec![], Crypto::None
);
let mut data = [0; 1500];
data[105] = 45;
b.iter(|| {
node.handle_interface_data(&mut data, 100, 1400).unwrap()
});
b.bytes = 1400;
}
#[bench]
fn handle_net_message(b: &mut Bencher) {
let mut node = GenericCloud::<Frame>::new(
Device::dummy("vpncloud0", "/dev/null").unwrap(), "127.0.0.1:0", None,
Box::new(SwitchTable::new(300)), 1800, true, true, vec![], Crypto::None
);
let addr = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 1));
let mut data = [0; 1500];
data[105] = 45;
b.iter(|| {
node.handle_net_message(addr.clone(), Options::default(), Message::Data(&mut data, 0, 1400)).unwrap()
});
b.bytes = 1400;
}
#[bench]
fn udp_send(b: &mut Bencher) {
let sock = UdpSocket::bind("127.0.0.1:0").unwrap();
let data = [0; 1400];
b.iter(|| {
sock.send_to(&data, "127.0.0.1:1").unwrap()
});
b.bytes = 1400;
}

View File

@ -7,6 +7,7 @@ use std::collections::HashMap;
use std::hash::Hasher; use std::hash::Hasher;
use std::net::UdpSocket; use std::net::UdpSocket;
use std::io::Read; use std::io::Read;
use std::io::Result as IoResult;
use std::fmt; use std::fmt;
use std::os::unix::io::AsRawFd; use std::os::unix::io::AsRawFd;
use std::marker::PhantomData; use std::marker::PhantomData;
@ -171,6 +172,16 @@ impl<P: Protocol> GenericCloud<P> {
} }
} }
#[allow(dead_code)]
pub fn address(&self) -> IoResult<SocketAddr> {
self.socket.local_addr()
}
#[allow(dead_code)]
pub fn peer_count(&self) -> usize {
self.peers.len()
}
pub fn connect<Addr: ToSocketAddrs+fmt::Display>(&mut self, addr: Addr, reconnect: bool) -> Result<(), Error> { pub fn connect<Addr: ToSocketAddrs+fmt::Display>(&mut self, addr: Addr, reconnect: bool) -> Result<(), Error> {
if let Ok(mut addrs) = addr.to_socket_addrs() { if let Ok(mut addrs) = addr.to_socket_addrs() {
while let Some(a) = addrs.next() { while let Some(a) = addrs.next() {
@ -225,7 +236,7 @@ impl<P: Protocol> GenericCloud<P> {
Ok(()) Ok(())
} }
fn handle_interface_data(&mut self, payload: &mut [u8], start: usize, end: usize) -> Result<(), Error> { pub fn handle_interface_data(&mut self, payload: &mut [u8], start: usize, end: usize) -> Result<(), Error> {
let (src, dst) = try!(P::parse(&payload[start..end])); let (src, dst) = try!(P::parse(&payload[start..end]));
debug!("Read data from interface: src: {}, dst: {}, {} bytes", src, dst, end-start); debug!("Read data from interface: src: {}, dst: {}, {} bytes", src, dst, end-start);
match self.table.lookup(&dst) { match self.table.lookup(&dst) {
@ -250,7 +261,7 @@ impl<P: Protocol> GenericCloud<P> {
Ok(()) Ok(())
} }
fn handle_net_message(&mut self, peer: SocketAddr, options: Options, msg: Message) -> Result<(), Error> { pub fn handle_net_message(&mut self, peer: SocketAddr, options: Options, msg: Message) -> Result<(), Error> {
if self.options.network_id != options.network_id { if self.options.network_id != options.network_id {
info!("Ignoring message from {} with wrong token {:?}", peer, options.network_id); info!("Ignoring message from {} with wrong token {:?}", peer, options.network_id);
return Err(Error::WrongNetwork(options.network_id)); return Err(Error::WrongNetwork(options.network_id));

View File

@ -42,6 +42,11 @@ impl Device {
} }
} }
#[allow(dead_code)]
pub fn dummy(ifname: &str, path: &str) -> IoResult<Self> {
Ok(Device{fd: try!(fs::OpenOptions::new().create(true).read(true).write(true).open(path)), ifname: ifname.to_string()})
}
#[inline(always)] #[inline(always)]
pub fn ifname(&self) -> &str { pub fn ifname(&self) -> &str {
&self.ifname &self.ifname