1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#![cfg_attr(feature = "unstable", feature(test))]

mod buffer;
//pub mod kitty_pool;
pub mod pool;
mod range;

extern crate getset;
extern crate rand;
extern crate tokio;
extern crate tokio_async_await;

#[cfg(all(feature = "unstable", test))]
mod bench {
    extern crate test;
    use super::*;
    use kitty_pool::{BorrowBufferResponse, KittyPool};
    use rand::{distributions::Alphanumeric, thread_rng, Rng};
    use std::io::Read;
    use std::io::Write;
    use std::sync::Arc;
    use std::thread;
    use test::Bencher;
    use tokio::prelude::future::ok;
    use tokio::prelude::Future;

    const POOL_BLOCKS: usize = 64;
    const POOL_SIZE: usize = 1024 * 1024 * 64;
    const REQUEST_SIZE: usize = 1024 * 1024 * 16;
    const REQUESTS: usize = 10;

    fn test_fn(pool: Arc<KittyPool>) {
        let rand_string: String = thread_rng()
            .sample_iter(&Alphanumeric)
            .take(REQUEST_SIZE)
            .collect();

        let write_buffer = rand_string.into_bytes();
        let mut read_buffer = vec![0; REQUEST_SIZE];

        let result = pool
            .borrow_ranges(REQUEST_SIZE)
            .and_then(move |mut result| {
                let write_result = result.write(write_buffer.as_slice());
                assert!(write_result.is_ok());
                let read_result = result.read(read_buffer.as_mut_slice());
                assert!(read_result.is_ok());
                assert_eq!(write_result.unwrap(), read_result.unwrap());
                ok(())
            })
            .poll();
        assert!(result.is_ok());
    }

    #[bench]
    fn bench_thread_single_context(b: &mut Bencher) {
        let mut kitty_pool = Arc::from(KittyPool::new(POOL_SIZE, POOL_BLOCKS));

        b.iter(|| {
            let kitty_clone = kitty_pool.clone();
            test_fn(kitty_clone);
        })
    }

    #[bench]
    fn bench_thread_multiple_contexts(b: &mut Bencher) {
        let mut kitty_pool = Arc::from(KittyPool::new(POOL_SIZE, POOL_BLOCKS));

        b.iter(|| {
            let mut handles = Vec::new();
            for _ in 0..REQUESTS {
                let kitty_clone = kitty_pool.clone();
                handles.push(thread::spawn(move || test_fn(kitty_clone)));
            }

            for handle in handles.into_iter() {
                assert!(handle.join().is_ok());
            }
        })
    }
}