From 9bca4f2dbd3f3c425fb65714193c149d631d90f2 Mon Sep 17 00:00:00 2001 From: bee Date: Tue, 26 Nov 2024 09:24:03 +0000 Subject: [PATCH 1/6] benchmark --- benches/benchmark_portscan.rs | 46 +++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/benches/benchmark_portscan.rs b/benches/benchmark_portscan.rs index c50c661d6..ebb17b7aa 100644 --- a/benches/benchmark_portscan.rs +++ b/benches/benchmark_portscan.rs @@ -1,3 +1,4 @@ +use async_std::task::block_on; use criterion::{black_box, criterion_group, criterion_main, Criterion}; use rustscan::input::{PortRange, ScanOrder}; use rustscan::port_strategy::PortStrategy; @@ -5,51 +6,54 @@ use rustscan::scanner::Scanner; use std::net::IpAddr; use std::time::Duration; -fn portscan_tcp() { +fn portscan_tcp(scanner: &Scanner) { + let _scan_result = block_on(scanner.run()); +} + +fn portscan_udp(scanner: &Scanner) { + let _scan_result = block_on(scanner.run()); +} + +fn criterion_benchmark(c: &mut Criterion) { let addrs = vec!["127.0.0.1".parse::().unwrap()]; let range = PortRange { start: 1, - end: 60_000, + end: 1_000, }; - let strategy = PortStrategy::pick(&Some(range), None, ScanOrder::Serial); - let _scanner = Scanner::new( + let strategy_tcp = PortStrategy::pick(&Some(range.clone()), None, ScanOrder::Serial); + let strategy_udp = PortStrategy::pick(&Some(range.clone()), None, ScanOrder::Serial); + + let scanner_tcp = Scanner::new( &addrs, 10, Duration::from_millis(100), 1, false, - strategy, + strategy_tcp, true, vec![], false, ); - // Perform the actual scan or logic here if needed -} -fn portscan_udp() { - let addrs = vec!["127.0.0.1".parse::().unwrap()]; - let range = PortRange { - start: 1, - end: 60_000, - }; - let strategy = PortStrategy::pick(&Some(range), None, ScanOrder::Serial); - let _scanner = Scanner::new( + c.bench_function("portscan tcp", |b| { + b.iter(|| portscan_tcp(black_box(&scanner_tcp))) + }); + + let scanner_udp = Scanner::new( &addrs, 10, Duration::from_millis(100), 1, false, - strategy, + strategy_udp, true, vec![], true, ); - // Perform the actual scan or logic here if needed -} -fn criterion_benchmark(c: &mut Criterion) { - c.bench_function("portscan tcp", |b| b.iter(|| portscan_tcp())); - c.bench_function("portscan udp", |b| b.iter(|| portscan_udp())); + c.bench_function("portscan udp", |b| { + b.iter(|| portscan_udp(black_box(&scanner_udp))) + }); } criterion_group!(benches, criterion_benchmark); From 77e9262130e4e1aa00e9080dcce20d43371a1042 Mon Sep 17 00:00:00 2001 From: "Autumn (Bee)" Date: Tue, 26 Nov 2024 09:56:23 +0000 Subject: [PATCH 2/6] Change duration of each individual port scan to 10ms --- benches/benchmark_portscan.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/benches/benchmark_portscan.rs b/benches/benchmark_portscan.rs index ebb17b7aa..caf118928 100644 --- a/benches/benchmark_portscan.rs +++ b/benches/benchmark_portscan.rs @@ -26,7 +26,7 @@ fn criterion_benchmark(c: &mut Criterion) { let scanner_tcp = Scanner::new( &addrs, 10, - Duration::from_millis(100), + Duration::from_millis(10), 1, false, strategy_tcp, From cfb494f6289ab4b15c062fcd9f50cf13e09a0014 Mon Sep 17 00:00:00 2001 From: PsypherPunk Date: Tue, 26 Nov 2024 09:57:58 +0000 Subject: [PATCH 3/6] test: extend UDP benchmark measurement time --- benches/benchmark_portscan.rs | 5 ++++- src/scripts/mod.rs | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/benches/benchmark_portscan.rs b/benches/benchmark_portscan.rs index caf118928..0f6485ddd 100644 --- a/benches/benchmark_portscan.rs +++ b/benches/benchmark_portscan.rs @@ -51,7 +51,10 @@ fn criterion_benchmark(c: &mut Criterion) { true, ); - c.bench_function("portscan udp", |b| { + let mut udp_group = c.benchmark_group("portscan udp"); + udp_group.measurement_time(Duration::from_secs(20)); + + udp_group.bench_function("portscan udp", |b| { b.iter(|| portscan_udp(black_box(&scanner_udp))) }); } diff --git a/src/scripts/mod.rs b/src/scripts/mod.rs index 8bc188d01..410fd21a1 100644 --- a/src/scripts/mod.rs +++ b/src/scripts/mod.rs @@ -281,7 +281,7 @@ fn execute_script(script: &str) -> Result { }; match Command::new(cmd) - .args(&[arg, script]) + .args([arg, script]) .stdin(Stdio::piped()) .stderr(Stdio::piped()) .output() From 45e789e8b0b81be393abec851e3d217b8bfa5771 Mon Sep 17 00:00:00 2001 From: bee Date: Tue, 26 Nov 2024 11:07:23 +0000 Subject: [PATCH 4/6] benching helper funcs --- benches/benchmark_portscan.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/benches/benchmark_portscan.rs b/benches/benchmark_portscan.rs index 0f6485ddd..c063db3db 100644 --- a/benches/benchmark_portscan.rs +++ b/benches/benchmark_portscan.rs @@ -14,6 +14,18 @@ fn portscan_udp(scanner: &Scanner) { let _scan_result = block_on(scanner.run()); } +fn bench_address() { + vec!["127.0.0.1".parse::().unwrap()]; +} + +fn bench_port_strategy() { + let range = PortRange { + start: 1, + end: 1_000, + }; + PortStrategy::pick(&Some(range.clone()), None, ScanOrder::Serial); +} + fn criterion_benchmark(c: &mut Criterion) { let addrs = vec!["127.0.0.1".parse::().unwrap()]; let range = PortRange { @@ -57,6 +69,12 @@ fn criterion_benchmark(c: &mut Criterion) { udp_group.bench_function("portscan udp", |b| { b.iter(|| portscan_udp(black_box(&scanner_udp))) }); + + // Benching helper functions + + c.bench_function("parse address", |b| b.iter(|| bench_address)); + + c.bench_function("parse address", |b| b.iter(|| bench_port_strategy)); } criterion_group!(benches, criterion_benchmark); From fc0b9a6c7fd6b345df8d7afc20d25f3287f9ea8a Mon Sep 17 00:00:00 2001 From: bee Date: Tue, 26 Nov 2024 11:13:35 +0000 Subject: [PATCH 5/6] Broken benchmarking code --- benches/benchmark_portscan.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/benches/benchmark_portscan.rs b/benches/benchmark_portscan.rs index c063db3db..873367c5c 100644 --- a/benches/benchmark_portscan.rs +++ b/benches/benchmark_portscan.rs @@ -72,9 +72,9 @@ fn criterion_benchmark(c: &mut Criterion) { // Benching helper functions - c.bench_function("parse address", |b| b.iter(|| bench_address)); + parse_address.bench_function("parse address", |b| b.iter(|| bench_address)); - c.bench_function("parse address", |b| b.iter(|| bench_port_strategy)); + let mut port_strategy = c.benchmark_group("port strategy"); } criterion_group!(benches, criterion_benchmark); From cfde69ded36c052971f7746d515f703393de0db2 Mon Sep 17 00:00:00 2001 From: bee Date: Tue, 26 Nov 2024 12:49:05 +0000 Subject: [PATCH 6/6] Add complete benchmarking for most functions --- benches/benchmark_portscan.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/benches/benchmark_portscan.rs b/benches/benchmark_portscan.rs index 873367c5c..ef607e2fe 100644 --- a/benches/benchmark_portscan.rs +++ b/benches/benchmark_portscan.rs @@ -15,7 +15,7 @@ fn portscan_udp(scanner: &Scanner) { } fn bench_address() { - vec!["127.0.0.1".parse::().unwrap()]; + let _addrs = vec!["127.0.0.1".parse::().unwrap()]; } fn bench_port_strategy() { @@ -23,7 +23,7 @@ fn bench_port_strategy() { start: 1, end: 1_000, }; - PortStrategy::pick(&Some(range.clone()), None, ScanOrder::Serial); + let _strategy = PortStrategy::pick(&Some(range.clone()), None, ScanOrder::Serial); } fn criterion_benchmark(c: &mut Criterion) { @@ -54,7 +54,7 @@ fn criterion_benchmark(c: &mut Criterion) { let scanner_udp = Scanner::new( &addrs, 10, - Duration::from_millis(100), + Duration::from_millis(10), 1, false, strategy_udp, @@ -65,16 +65,15 @@ fn criterion_benchmark(c: &mut Criterion) { let mut udp_group = c.benchmark_group("portscan udp"); udp_group.measurement_time(Duration::from_secs(20)); - udp_group.bench_function("portscan udp", |b| { b.iter(|| portscan_udp(black_box(&scanner_udp))) }); + udp_group.finish(); // Benching helper functions + c.bench_function("parse address", |b| b.iter(|| bench_address())); - parse_address.bench_function("parse address", |b| b.iter(|| bench_address)); - - let mut port_strategy = c.benchmark_group("port strategy"); + c.bench_function("port strategy", |b| b.iter(|| bench_port_strategy())); } criterion_group!(benches, criterion_benchmark);