Skip to content

Commit

Permalink
The sn bench client support simulation of exception
Browse files Browse the repository at this point in the history
  • Loading branch information
jing-git committed May 10, 2023
1 parent 0083875 commit 9a636b8
Show file tree
Hide file tree
Showing 6 changed files with 240 additions and 46 deletions.
12 changes: 8 additions & 4 deletions src/tools/bdt-tool/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,40 +171,44 @@ async fn main() {

match subcommand {
"sn_bench_ping" => {
loger_init(log_level, "sn_bench_ping");

let subcommand = cmd_params.subcommand_matches("sn_bench_ping").unwrap();
let device_load = subcommand.value_of("load").unwrap_or("");
let device_num = u64::from_str(subcommand.value_of("device").unwrap_or("1000")).unwrap();
let interval_ms = u64::from_str(subcommand.value_of("interval").unwrap_or("1000")).unwrap();
let timeout_sec = u64::from_str(subcommand.value_of("timeout").unwrap_or("3")).unwrap();
let bench_time = u64::from_str(subcommand.value_of("time").unwrap_or("60")).unwrap();
let exception = bool::from_str(subcommand.value_of("exception").unwrap_or("false")).unwrap();

let ping_exception = SnBenchPingException::default();
let result = sn_bench_ping(
device_num, device_load,
sns, endpoints, bench_time,
interval_ms,
timeout_sec,
ping_exception).await.unwrap();
exception).await.unwrap();

result.show();

return;
},
"sn_bench_call" => {
loger_init(log_level, "sn_bench_call");

let subcommand = cmd_params.subcommand_matches("sn_bench_call").unwrap();
let device_load = subcommand.value_of("load").unwrap_or("");
let device_num = u64::from_str(subcommand.value_of("device").unwrap_or("1000")).unwrap();
let interval_ms = u64::from_str(subcommand.value_of("interval").unwrap_or("1000")).unwrap();
let timeout_sec = u64::from_str(subcommand.value_of("timeout").unwrap_or("3")).unwrap();
let bench_time = u64::from_str(subcommand.value_of("time").unwrap_or("60")).unwrap();
let exception = bool::from_str(subcommand.value_of("exception").unwrap_or("false")).unwrap();

let ping_exception = SnBenchPingException::default();
let result = sn_bench_call(
device_num, device_load,
sns, endpoints, bench_time,
interval_ms,
timeout_sec,
ping_exception).await.unwrap();
exception).await.unwrap();

result.show();

Expand Down
191 changes: 189 additions & 2 deletions src/tools/bdt-tool/src/sn_bench/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,208 @@ use cyfs_bdt::*;

use super::*;

async fn stack_sn_online(stack: StackGuard, snsl: Option<Vec<Device>>, timeout_sec: u64) {
loop {
if let Some(sns) = snsl.as_ref() {
stack.reset_sn_list(sns.clone());
}

let mut online = false;

match future::timeout(
Duration::from_secs(timeout_sec),
stack.sn_client().ping().wait_online(),
).await {
Ok(res) => {
match res {
Ok(res) => {
match res {
SnStatus::Online => {
online = true;
},
_ => {
}
}
},
Err(_) => {
}
}
},
Err(_) => {
}
}
if online {
break ;
}

task::sleep(Duration::from_millis(500)).await;
}
}

pub async fn device_online_once(sns: Option<Vec<Device>>, endpoints: Vec<Endpoint>) -> Device {
let de = device_stack_new(1, sns.clone(), endpoints.clone(), 1, 0).await;
let stack = {
let stacks = de.0.stacks.lock().unwrap();
let stack = stacks.get(0).unwrap();
stack.clone()
};

let device = {
let devices = de.0.devices.read().unwrap();
let d = devices.get(0).unwrap();
d.clone()
};

stack_sn_online(stack.clone(), sns.clone(), 3).await;
let endpoint: &Endpoint = endpoints.get(0).unwrap();
let ep = new_endpoint(&endpoint, 2);
let mut eps = vec![];
eps.push(ep);
let _ = stack.reset_endpoints(&eps).await;

device
}

//port:0,1,2
async fn remote_offline(
sns: Option<Vec<Device>>, endpoints: Vec<Endpoint>,
call_interval_ms: u64,
result: SnBenchResult,
bench_end_time: u64) {
let de = device_stack_new(1, sns.clone(), endpoints.clone(), 0, 0).await;
let local_stack = {
let stacks = de.0.stacks.lock().unwrap();
let stack = stacks.get(0).unwrap();
stack.clone()
};

stack_sn_online(local_stack.clone(), sns.clone(), 3).await;
let remote = device_online_once(sns.clone(), endpoints).await;

let pinger = cyfs_bdt::debug::Pinger::open(local_stack.clone().to_weak()).unwrap();
let mut end_time = bucky_time_now() + 20;
if end_time > bench_end_time {
end_time = bench_end_time;
}
loop {
if bucky_time_now() >= end_time {
break ;
}

match pinger.ping(remote.clone(), Duration::from_secs(3), "debug".as_ref()).await {
Ok(_) => {
},
Err(e) => {
result.add_error(ExceptionType::RemoteOffline, e.code())
}
}

task::sleep(Duration::from_millis(call_interval_ms)).await;
}
}

async fn exception_remote_offline(
sns: Option<Vec<Device>>, endpoints: Vec<Endpoint>,
call_interval_ms: u64,
result: SnBenchResult,
bench_time_sec: u64) {
let bench_end_time = bucky_time_now() + bench_time_sec;
loop {
if bucky_time_now() >= bench_end_time {
break ;
}

remote_offline(sns.clone(), endpoints.clone(), call_interval_ms, result.clone(), bench_end_time).await;
}
}

//port:3,4
async fn exception_remote_not_exist(
sns: Option<Vec<Device>>, endpoints: Vec<Endpoint>,
call_interval_ms: u64,
result: SnBenchResult,
bench_time_sec: u64) {
let de = device_stack_new(2, sns.clone(), endpoints, 3, 0).await;
let local_stack = {
let stacks = de.0.stacks.lock().unwrap();
let stack = stacks.get(0).unwrap();
stack.clone()
};
let remote = {
let devices = de.0.devices.read().unwrap();
let d = devices.get(1).unwrap();
d.clone()
};

stack_sn_online(local_stack.clone(), sns.clone(), 3).await;

let pinger = cyfs_bdt::debug::Pinger::open(local_stack.clone().to_weak()).unwrap();
let bench_end_time = bucky_time_now() + bench_time_sec;
loop {
if bucky_time_now() >= bench_end_time {
break ;
}

match pinger.ping(remote.clone(), Duration::from_secs(3), "debug".as_ref()).await {
Ok(_) => {
},
Err(e) => {
result.add_error(ExceptionType::RemoteNotExists, e.code())
}
}

task::sleep(Duration::from_millis(call_interval_ms)).await;
}
}

pub async fn sn_bench_call(
device_num: u64, device_dir: &str,
sns: Option<Vec<Device>>, endpoints: Vec<Endpoint>,
bench_time_sec: u64,
call_interval_ms: u64,
timeout_sec: u64,
_: SnBenchPingException) -> BuckyResult<SnBenchResult> {
exception: bool) -> BuckyResult<SnBenchResult> {
let call_bench_result = SnBenchResult::default();

//
if exception {
println!("start exception task");

let mut tasks = vec![];

let start = bucky_time_now();
//remote offline
let snsl = sns.clone();
let ep = endpoints.clone();
let result: SnBenchResult = call_bench_result.clone();
tasks.push(task::spawn(async move {
exception_remote_offline(snsl, ep, call_interval_ms, result, bench_time_sec).await;
}));

//remote not exist
let snsl = sns.clone();
let ep = endpoints.clone();
let result: SnBenchResult = call_bench_result.clone();
tasks.push(task::spawn(async move {
exception_remote_not_exist(snsl, ep, call_interval_ms, result, bench_time_sec).await;
}));

for t in tasks {
let _ = t.await;
}
let end = bucky_time_now();

call_bench_result.stat(start, end);

return Ok(call_bench_result)
}

println!("init devices");
let device_emulator = {
if device_dir.len() != 0 { //load the dir's desc files
unimplemented!()
} else { //create device
let de = device_stack_new(device_num, sns.clone(), endpoints, 3600*24*30).await;
let de = device_stack_new(device_num, sns.clone(), endpoints, 0, 3600*24*30).await;
device_save(de.clone()).await;
de
}
Expand Down
35 changes: 4 additions & 31 deletions src/tools/bdt-tool/src/sn_bench/exception.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,5 @@

pub struct SnBenchPingException {
wrong_public_key: bool,
wrong_private_key: bool,
pub enum ExceptionType {
Common,
RemoteOffline,
RemoteNotExists,
}

impl SnBenchPingException {
pub fn default() -> Self {
SnBenchPingException {
wrong_public_key: false,
wrong_private_key: false,
}
}
}

pub struct SnBenchCallException {
wrong_public_key: bool,
wrong_private_key: bool,
remote_offline: bool,
remote_not_exist: bool,
}

impl SnBenchCallException {
pub fn default() -> Self {
SnBenchCallException {
wrong_public_key: false,
wrong_private_key: false,
remote_offline: false,
remote_not_exist: false,
}
}
}
4 changes: 2 additions & 2 deletions src/tools/bdt-tool/src/sn_bench/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub struct DeviceEmulatorImpl {
#[derive(Clone)]
pub struct DeviceEmulator(Arc<DeviceEmulatorImpl>);

pub async fn device_stack_new(device_num: u64, sns: Option<Vec<Device>>, endpoints: Vec<Endpoint>, ping_interval: u64) -> DeviceEmulator {
pub async fn device_stack_new(device_num: u64, sns: Option<Vec<Device>>, endpoints: Vec<Endpoint>, port_offset: u16, ping_interval: u64) -> DeviceEmulator {
let endpoint = endpoints.get(0).unwrap();

let stacks = Arc::new(Mutex::new(vec![]));
Expand Down Expand Up @@ -107,7 +107,7 @@ pub async fn device_stack_new(device_num: u64, sns: Option<Vec<Device>>, endpoin

let mut endpoints = vec![];

let ep = new_endpoint(&ept, idx as u16);
let ep = new_endpoint(&ept, idx as u16 + port_offset);

endpoints.push(ep);

Expand Down
4 changes: 2 additions & 2 deletions src/tools/bdt-tool/src/sn_bench/ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ pub async fn sn_bench_ping(
bench_time_sec: u64,
ping_interval_ms: u64,
timeout_sec: u64,
_: SnBenchPingException) -> BuckyResult<SnBenchResult> {
_: bool) -> BuckyResult<SnBenchResult> {
let ping_bench_result = SnBenchResult::default();

println!("init devices");
let device_emulator = {
if device_dir.len() != 0 { //load the dir's desc files
unimplemented!()
} else { //create device
let de = device_stack_new(device_num, sns.clone(), endpoints, 0).await;
let de = device_stack_new(device_num, sns.clone(), endpoints, 0, 0).await;
device_save(de.clone()).await;
de
}
Expand Down
Loading

0 comments on commit 9a636b8

Please sign in to comment.