tdb_cli/
lib.rs

1extern crate byteorder;
2extern crate serde;
3extern crate serde_json;
4extern crate tdb_core;
5#[macro_use] extern crate log;
6
7pub mod error;
8pub mod client;
9
10use std::env;
11use crate::client::TectonicClient;
12use crate::error::TectonicError;
13use std::time::SystemTime;
14use tdb_core::dtf::update::Update;
15
16fn key_or_default(key: &str, default: &str) -> String {
17   match env::var(key) {
18        Ok(val) => val,
19        Err(_) => default.into(),
20    }
21}
22
23fn get_tectonic_conf_from_env() -> (String, String) {
24    let tectonic_hostname: String = key_or_default("TDB_HOSTNAME", "localhost");
25    let tectonic_port: String     = key_or_default("TDB_PORT", "9001");
26
27    (tectonic_hostname, tectonic_port)
28}
29
30/// Creates a new connection to TectonicDB, using configuration values from environment
31/// or defaults to localhost:9001 if none are set.
32///
33/// "TDB_HOSTNAME", "localhost");
34/// "TDB_PORT", "9001");
35///
36pub fn client_from_env() -> TectonicClient {
37    let (tectonic_hostname, tectonic_port) = get_tectonic_conf_from_env();
38    match TectonicClient::new(&tectonic_hostname, &tectonic_port) {
39        Ok(cli) => cli,
40        Err(TectonicError::ConnectionError) => {
41            panic!("DB cannot be connected!");
42        },
43        _ => unreachable!(),
44    }
45}
46
47pub fn benchmark(mut cli: TectonicClient, times: usize) {
48
49    let mut t = SystemTime::now();
50
51    let mut acc = vec![];
52    let create = cli.cmd("CREATE benchmark\n");
53    println!("{:?}", create);
54    for i in 0..times {
55        if i % 10_000 == 0 {
56            dbg!(i);
57        }
58        let ts = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_nanos() as u64 / 1000;
59
60        let res = cli.insert(
61            Some("benchmark"),
62            &Update { ts, seq: 0, is_bid: true, is_trade: false, price: 0.001939,  size: 22.85 },
63            true,
64        );
65        res.unwrap();
66        acc.push(t.elapsed().unwrap().subsec_nanos() as usize);
67        // info!("res: {:?}, latency: {:?}", res, t.elapsed());
68        t = SystemTime::now();
69    }
70
71    ::std::thread::sleep(std::time::Duration::new(1, 0));
72    cli.shutdown();
73
74    let avg_ns = acc.iter().fold(0, |s, i| s + i) as f32 / acc.len() as f32;
75    println!("AVG ns/insert: {}", avg_ns);
76    println!("AVG inserts/s: {}", 1. / (avg_ns / 1_000_000_000.));
77}