Add code to benchmark parsers

next
Sayan Nandan 4 years ago
parent e84c58d4ad
commit cfcc73bd86
No known key found for this signature in database
GPG Key ID: C31EFD7DDA12AEE0

87
Cargo.lock generated

@ -1,8 +1,79 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "cfg-if"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "devtimer"
version = "4.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "getrandom"
version = "0.1.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.72 (registry+https://github.com/rust-lang/crates.io-index)",
"wasi 0.9.0+wasi-snapshot-preview1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "libc"
version = "0.2.72"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "libcore"
version = "0.1.0"
dependencies = [
"devtimer 4.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "ppv-lite86"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "rand"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"getrandom 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.72 (registry+https://github.com/rust-lang/crates.io-index)",
"rand_chacha 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"rand_hc 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "rand_chacha"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"ppv-lite86 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
"rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "rand_core"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"getrandom 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "rand_hc"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "terrabase-cli"
@ -12,3 +83,19 @@ version = "0.1.0"
name = "terrabase-server"
version = "0.1.0"
[[package]]
name = "wasi"
version = "0.9.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata]
"checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
"checksum devtimer 4.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6035b7b9244bf9637cd7ef80b5e1c54404bef92cccd34738c85c45f04ae8b244"
"checksum getrandom 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)" = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb"
"checksum libc 0.2.72 (registry+https://github.com/rust-lang/crates.io-index)" = "a9f8082297d534141b30c8d39e9b1773713ab50fdbe4ff30f750d063b3bfd701"
"checksum ppv-lite86 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "237a5ed80e274dbc66f86bd59c1e25edc039660be53194b5fe0a482e0f2612ea"
"checksum rand 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)" = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03"
"checksum rand_chacha 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402"
"checksum rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19"
"checksum rand_hc 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c"
"checksum wasi 0.9.0+wasi-snapshot-preview1 (registry+https://github.com/rust-lang/crates.io-index)" = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"

@ -7,3 +7,8 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
[dev-dependencies]
devtimer = "4.0.0"
rand = "0.7.3"

@ -229,7 +229,7 @@ fn test_result_macros() {
/// This returns a `TPQueryMethod` which can be used to execute the action or
/// it returns a `TPQueryError` in the case an error occurs while parsing the packet
pub fn parse_query_packet(
packet: String,
packet: &String,
self_version: &Version,
) -> Result<TPQueryMethod, TPQueryError> {
let rlines: Vec<&str> = packet.lines().collect();
@ -319,7 +319,7 @@ pub type TPResult = String;
/// If there was no error in parsing the packet, then a `TPResult` is returned.
/// Otherwise a `TPResultError` is returned
pub fn parse_result_packet(
packet: String,
packet: &String,
self_version: &Version,
) -> Result<TPResult, TPResultError> {
use TPResultError::*;
@ -347,7 +347,7 @@ pub fn parse_result_packet(
return Err(StandardError(TPError::InvalidMetaframe));
}
let respcode = match metaframe[4].parse::<u8>() {
let respcode = match metaframe[3].parse::<u8>() {
Ok(v) => v,
Err(_) => return Err(UnrecognizedError(metaframe[4].to_owned())),
};
@ -378,7 +378,7 @@ pub fn parse_result_packet(
fn test_query_packet_parsing() {
let qpacket = query_packet!(Version(0, 1, 0), TPQueryType::GET, "sayan");
let query_should_be = TPQueryMethod::GET("sayan".to_owned());
let parsed_qpacket = parse_query_packet(qpacket, &Version(0, 1, 0)).unwrap();
let parsed_qpacket = parse_query_packet(&qpacket, &Version(0, 1, 0)).unwrap();
assert_eq!(query_should_be, parsed_qpacket);
}
@ -388,6 +388,36 @@ fn test_result_packet_parsing() {
let v = Version(0, 1, 0);
let rpacket = result_packet!(v, 0, 18);
let result_should_be = 18.to_string();
let parsed_rpacket = parse_result_packet(rpacket, &v).unwrap();
let parsed_rpacket = parse_result_packet(&rpacket, &v).unwrap();
assert_eq!(result_should_be, parsed_rpacket);
}
#[cfg(test)]
#[test]
fn benchmark_packet_parsing() {
let version = Version(0, 1, 0);
use devtimer;
use rand::{distributions::Alphanumeric, thread_rng, Rng};
// First generate about 5000 random keys and 5000 random values
let rankeys: Vec<String> = (0..5000)
.map(|_| thread_rng().sample_iter(&Alphanumeric).take(30).collect())
.collect();
let ranvalues: Vec<String> = (0..5000)
.map(|_| thread_rng().sample_iter(&Alphanumeric).take(30).collect())
.collect();
let queries: Vec<String> = (0..5000)
.map(|n| query_packet!(version, TPQueryType::GET, rankeys[n]))
.collect();
let results: Vec<String> = (0..5000)
.map(|n| result_packet!(version, 0, ranvalues[n]))
.collect();
let qpacket_bench = devtimer::run_benchmark(5000, |n| {
parse_query_packet(&queries[n], &version).unwrap();
});
let rpacket_bench = devtimer::run_benchmark(5000, |n| {
parse_result_packet(&results[n], &version).unwrap();
});
qpacket_bench.print_stats();
rpacket_bench.print_stats();
}

Loading…
Cancel
Save