Skip to content

Commit

Permalink
ends time & request count
Browse files Browse the repository at this point in the history
  • Loading branch information
cchexcode committed Sep 16, 2023
1 parent febfa81 commit 7d028bb
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 26 deletions.
20 changes: 13 additions & 7 deletions res/example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,26 @@ version: "0.0"
campaigns:
main:
phases:
- target: ""
- target: "https://solar-monitoring-dev.nexteraanalytics.com/v1/telemetry/sites"
threads: 32
ends: !requests 2000
# ends: !requests 2000
ends: !seconds 60
timeout_ms: 1000
report:
interval: !seconds 4
spec: !get
header:
x-api-key: [""]
x-api-key:
- !env "API_KEY"
query:
page: [0]
per_page: [1000]
from: [1262304000]
to: [1262307600]
page:
- !static 0
per_page:
- !static 1000
from:
- !static 1262304000
to:
- !static 1262307600
behaviours:
ok:
- match: ^(200)$
Expand Down
12 changes: 10 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,18 @@ pub enum Interval {
#[serde(rename_all = "snake_case")]
pub enum Spec {
Get {
header: HashMap<String, Vec<String>>,
query: HashMap<String, Vec<String>>,
header: HashMap<String, Vec<ValueParser>>,
query: HashMap<String, Vec<ValueParser>>,
},
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ValueParser {
Static(String),
Env(String),
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct Behaviours {
Expand Down Expand Up @@ -84,6 +91,7 @@ pub enum Mark {
#[serde(rename_all = "snake_case")]
pub enum End {
Requests(usize),
Seconds(u64),
}

#[cfg(test)]
Expand Down
76 changes: 59 additions & 17 deletions src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use {
ErrorBehaviour,
Mark,
Spec,
ValueParser,
},
anyhow::Result,
fancy_regex::Regex,
Expand Down Expand Up @@ -50,8 +51,8 @@ impl Engine {
for phase in &campaign.phases {
let phase_start = std::time::Instant::now();
let (tasks_tx, tasks_rx) =
flume::unbounded::<(Method, String, HeaderMap, Vec<(String, String)>, Duration)>();
let (status_tx, status_rx) = flume::unbounded::<(usize, ThreadEvent)>();
flume::bounded::<(Method, String, HeaderMap, Vec<(String, String)>, Duration)>(phase.threads * 2);
let (status_tx, status_rx) = flume::bounded::<(usize, ThreadEvent)>(phase.threads * 2);

let mut threads = Vec::<JoinHandle<_>>::with_capacity(phase.threads);
let mut thread_stats = BTreeMap::<usize, ThreadStats>::new();
Expand Down Expand Up @@ -97,23 +98,48 @@ impl Engine {

match &phase.spec {
| Spec::Get { header, query } => {
let header_map = HeaderMap::from_iter(
header
.iter()
.map(|v| {
(
v.0.parse().unwrap(),
v.1.into_iter()
.map(|v| {
match v {
| ValueParser::Static(v) => v.to_owned(),
| ValueParser::Env(v) => std::env::var(v).unwrap(),
}
})
.join(",")
.parse()
.unwrap(),
)
})
.collect::<Vec<(HeaderName, HeaderValue)>>(),
);
let query_map = query
.iter()
.map(|v| {
(
v.0.clone(),
v.1.into_iter()
.map(|v| {
match v {
| ValueParser::Static(v) => v.to_owned(),
| ValueParser::Env(v) => std::env::var(v).unwrap(),
}
})
.join(","),
)
})
.collect::<Vec<_>>();

let target = phase.target.clone();
let timeout_ms = phase.timeout_ms;

match phase.ends {
| End::Requests(v) => {
let header_map = HeaderMap::from_iter(
header
.iter()
.map(|v| (v.0.parse().unwrap(), v.1.into_iter().join(",").parse().unwrap()))
.collect::<Vec<(HeaderName, HeaderValue)>>(),
);
let query_map = query
.iter()
.map(|v| (v.0.clone(), v.1.into_iter().join(",")))
.collect::<Vec<_>>();

let target = phase.target.clone();
let timeout_ms = phase.timeout_ms;

// producer thread
spawn(move || {
for _ in 0..v {
tasks_tx
Expand All @@ -128,6 +154,22 @@ impl Engine {
}
});
},
| End::Seconds(v) => {
let now = std::time::Instant::now();
spawn(move || {
while now.elapsed().as_secs() < v {
tasks_tx
.send((
Method::GET,
target.clone(),
header_map.clone(),
query_map.clone(),
Duration::from_millis(timeout_ms),
))
.unwrap();
}
});
},
}
},
};
Expand Down

0 comments on commit 7d028bb

Please sign in to comment.