Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PoC] have a go at making goose async #8

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ license = "Apache-2.0"
ctrlc = "3.1"
http = "0.2"
log = "0.4"
macro_rules_attribute = "*"
num_cpus = "1.0"
num-format = "0.4"
rand = "0.7"
regex = "1"
reqwest = { version = "0.10", features = ["blocking", "cookies", "json"] }
structopt = "0.3"
simplelog = "0.7"
tokio = { version = "0.2.20", features = ["rt-core", "time"] }
url = "2.1"

[dev-dependencies]
Expand Down
43 changes: 26 additions & 17 deletions examples/drupal_loadtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@
//! See the License for the specific language governing permissions and
//! limitations under the License.

#[macro_use]
extern crate macro_rules_attribute;

use rand::Rng;
use regex::Regex;

use goose::dyn_async;
use goose::GooseState;
use goose::goose::{GooseTaskSet, GooseClient, GooseTask};

Expand Down Expand Up @@ -72,13 +76,14 @@ fn main() {
}

/// View the front page.
fn drupal_loadtest_front_page(client: &mut GooseClient) {
let response = client.get("/");
#[macro_rules_attribute(dyn_async!)]
async fn drupal_loadtest_front_page<'fut>(client: &'fut mut GooseClient) -> () {
let response = client.get("/").await;

// Grab some static assets from the front page.
match response {
Ok(r) => {
match r.text() {
match r.text().await {
Ok(t) => {
let re = Regex::new(r#"src="(.*?)""#).unwrap();
for url in re.captures_iter(&t) {
Expand All @@ -101,23 +106,26 @@ fn drupal_loadtest_front_page(client: &mut GooseClient) {
}

/// View a node from 1 to 10,000, created by preptest.sh.
fn drupal_loadtest_node_page(client: &mut GooseClient) {
#[macro_rules_attribute(dyn_async!)]
async fn drupal_loadtest_node_page<'fut>(client: &'fut mut GooseClient) -> () {
let nid = rand::thread_rng().gen_range(1, 10_000);
let _response = client.get(format!("/node/{}", &nid).as_str());
let _response = client.get(format!("/node/{}", &nid).as_str()).await;
}

/// View a profile from 2 to 5,001, created by preptest.sh.
fn drupal_loadtest_profile_page(client: &mut GooseClient) {
#[macro_rules_attribute(dyn_async!)]
async fn drupal_loadtest_profile_page<'fut>(client: &'fut mut GooseClient) -> () {
let uid = rand::thread_rng().gen_range(2, 5_001);
let _response = client.get(format!("/user/{}", &uid).as_str());
let _response = client.get(format!("/user/{}", &uid).as_str()).await;
}

/// Log in.
fn drupal_loadtest_login(client: &mut GooseClient) {
let response = client.get("/user");
#[macro_rules_attribute(dyn_async!)]
async fn drupal_loadtest_login<'fut>(client: &'fut mut GooseClient) -> () {
let response = client.get("/user").await;
match response {
Ok(r) => {
match r.text() {
match r.text().await {
Ok(html) => {
let re = Regex::new( r#"name="form_build_id" value=['"](.*?)['"]"#).unwrap();
let form_build_id = match re.captures(&html) {
Expand All @@ -140,7 +148,7 @@ fn drupal_loadtest_login(client: &mut GooseClient) {
("op", "Log+in"),
];
let request_builder = client.goose_post("/user");
let _response = client.goose_send(request_builder.form(&params));
let _response = client.goose_send(request_builder.form(&params)).await;
// @TODO: verify that we actually logged in.
}
Err(e) => {
Expand All @@ -155,12 +163,13 @@ fn drupal_loadtest_login(client: &mut GooseClient) {
}

/// Post a comment.
fn drupal_loadtest_post_comment(client: &mut GooseClient) {
let nid = rand::thread_rng().gen_range(1, 10_000);
let response = client.get(format!("/node/{}", &nid).as_str());
#[macro_rules_attribute(dyn_async!)]
async fn drupal_loadtest_post_comment<'fut>(client: &'fut mut GooseClient) -> () {
let nid: i32 = rand::thread_rng().gen_range(1, 10_000);
let response = client.get(format!("/node/{}", &nid).as_str()).await;
match response {
Ok(r) => {
match r.text() {
match r.text().await {
Ok(html) => {
// Extract the form_build_id from the user login form.
let re = Regex::new( r#"name="form_build_id" value=['"](.*?)['"]"#).unwrap();
Expand Down Expand Up @@ -205,10 +214,10 @@ fn drupal_loadtest_post_comment(client: &mut GooseClient) {
("op", "Save"),
];
let request_builder = client.goose_post(format!("/comment/reply/{}", &nid).as_str());
let response = client.goose_send(request_builder.form(&params));
let response = client.goose_send(request_builder.form(&params)).await;
match response {
Ok(r) => {
match r.text() {
match r.text().await {
Ok(html) => {
if !html.contains(&comment_body) {
eprintln!("no comment showed up after posting to comment/reply/{}", &nid);
Expand Down
20 changes: 14 additions & 6 deletions examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
//! See the License for the specific language governing permissions and
//! limitations under the License.


#[macro_use]
extern crate macro_rules_attribute;

use goose::dyn_async;
use goose::GooseState;
use goose::goose::{GooseTaskSet, GooseClient, GooseTask};

Expand All @@ -38,19 +43,22 @@ fn main() {
/// Demonstrates how to log in when a client starts. We flag this task as an
/// on_start task when registering it above. This means it only runs one time
/// per client, when the client thread first starts.
fn website_task_login(client: &mut GooseClient) {
#[macro_rules_attribute(dyn_async!)]
async fn website_task_login<'fut>(client: &'fut mut GooseClient) -> () {
let request_builder = client.goose_post("/login");
// https://docs.rs/reqwest/*/reqwest/blocking/struct.RequestBuilder.html#method.form
let params = [("username", "test_user"), ("password", "")];
let _response = client.goose_send(request_builder.form(&params));
let _response = client.goose_send(request_builder.form(&params)).await;
}

/// A very simple task that simply loads the front page.
fn website_task_index(client: &mut GooseClient) {
let _response = client.get("/");
#[macro_rules_attribute(dyn_async!)]
async fn website_task_index<'fut>(client: &'fut mut GooseClient) -> () {
let _response = client.get("/").await;
}

/// A very simple task that simply loads the about page.
fn website_task_about(client: &mut GooseClient) {
let _response = client.get("/about/");
#[macro_rules_attribute(dyn_async!)]
async fn website_task_about<'fut>(client: &'fut mut GooseClient) -> () {
let _response = client.get("/about/").await;
}
10 changes: 5 additions & 5 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use std::sync::mpsc;
use rand::thread_rng;
use rand::seq::SliceRandom;
use rand::Rng;
use std::{thread, time};
use std::time;

use crate::goose::{GooseTaskSet, GooseClient, GooseClientMode, GooseClientCommand};

pub fn client_main(
pub async fn client_main(
thread_number: usize,
thread_task_set: GooseTaskSet,
mut thread_client: GooseClient,
Expand Down Expand Up @@ -66,7 +66,7 @@ pub fn client_main(
thread_client.task_request_name = Some(thread_task_name.to_string());
}
// Invoke the task function.
function(&mut thread_client);
function(&mut thread_client).await;

// Prepare to sleep for a random value from min_wait to max_wait.
let wait_time: usize;
Expand Down Expand Up @@ -106,7 +106,7 @@ pub fn client_main(
if thread_continue && thread_client.max_wait > 0 {
let sleep_duration = time::Duration::from_secs(1);
debug!("client {} from {} sleeping {:?} second...", thread_number, thread_task_set.name, sleep_duration);
thread::sleep(sleep_duration);
tokio::time::delay_for(sleep_duration).await;
slept += 1;
if slept > wait_time {
in_sleep_loop = false;
Expand Down Expand Up @@ -144,4 +144,4 @@ pub fn client_main(
// Do our final sync before we exit.
thread_sender.send(thread_client.clone()).unwrap();
info!("exiting client {} from {}...", thread_number, thread_task_set.name);
}
}
Loading