Skip to content

Commit

Permalink
Merge pull request #24 from fcsonline/feature/rustfmt
Browse files Browse the repository at this point in the history
Format code with rustfmt
  • Loading branch information
fcsonline authored Mar 20, 2019
2 parents b34971a + a29e70f commit e021228
Show file tree
Hide file tree
Showing 16 changed files with 221 additions and 236 deletions.
3 changes: 3 additions & 0 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
tab_spaces = 2
max_width = 250
use_small_heuristics = "Off"
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
language: rust
cache: cargo
dist: trusty
os:
os:
- linux
- osx

rust:
- nightly
- stable

before_script:
- rustup component add rustfmt

script:
- cargo fmt --all -- --check
- cargo build
- cargo test
10 changes: 5 additions & 5 deletions src/actions/assign.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::collections::HashMap;

use yaml_rust::Yaml;
use colored::*;
use serde_json::Value;
use yaml_rust::Yaml;

use config;

use actions::{Runnable, Report};
use actions::{Report, Runnable};

#[derive(Clone)]
pub struct Assign {
Expand All @@ -16,23 +16,23 @@ pub struct Assign {
}

impl Assign {
pub fn is_that_you(item: &Yaml) -> bool{
pub fn is_that_you(item: &Yaml) -> bool {
item["assign"].as_hash().is_some()
}

pub fn new(item: &Yaml, _with_item: Option<Yaml>) -> Assign {
Assign {
name: item["name"].as_str().unwrap().to_string(),
key: item["assign"]["key"].as_str().unwrap().to_string(),
value: item["assign"]["value"].as_str().unwrap().to_string()
value: item["assign"]["value"].as_str().unwrap().to_string(),
}
}
}

impl Runnable for Assign {
fn execute(&self, context: &mut HashMap<String, Yaml>, _responses: &mut HashMap<String, Value>, _reports: &mut Vec<Report>, _config: &config::Config) {
if !_config.quiet {
println!("{:width$} {}={}", self.name.green(), self.key.cyan().bold(), self.value.magenta(), width=25);
println!("{:width$} {}={}", self.name.green(), self.key.cyan().bold(), self.value.magenta(), width = 25);
}

context.insert(self.key.to_owned(), Yaml::String(self.value.to_owned()));
Expand Down
4 changes: 2 additions & 2 deletions src/actions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ pub use self::assign::Assign;
pub use self::request::Request;
use config;

use std::fmt;
use std::collections::HashMap;
use std::fmt;

use yaml_rust::Yaml;
use serde_json::Value;
use yaml_rust::Yaml;

pub trait Runnable {
fn execute(&self, context: &mut HashMap<String, Yaml>, responses: &mut HashMap<String, Value>, reports: &mut Vec<Report>, config: &config::Config);
Expand Down
39 changes: 21 additions & 18 deletions src/actions/request.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
use std::collections::HashMap;
use std::io::Read;

use yaml_rust::Yaml;
use colored::*;
use serde_json;
use time;
use yaml_rust::Yaml;

use hyper::client::{Client, Response};
use hyper::net::HttpsConnector;
use hyper_native_tls::NativeTlsClient;
use hyper::header::{UserAgent, Headers, Cookie, SetCookie};
use hyper::header::{Cookie, Headers, SetCookie, UserAgent};
use hyper::method::Method;
use hyper::net::HttpsConnector;
use hyper_native_tls::native_tls::TlsConnector;
use hyper_native_tls::NativeTlsClient;

use interpolator;
use config;
use interpolator;

use actions::{Runnable, Report};
use actions::{Report, Runnable};

static USER_AGENT: &'static str = "drill";

Expand All @@ -33,7 +33,7 @@ pub struct Request {
}

impl Request {
pub fn is_that_you(item: &Yaml) -> bool{
pub fn is_that_you(item: &Yaml) -> bool {
item["request"].as_hash().is_some()
}

Expand Down Expand Up @@ -80,7 +80,6 @@ impl Request {
}
}


fn send_request(&self, context: &mut HashMap<String, Yaml>, responses: &mut HashMap<String, serde_json::Value>, config: &config::Config) -> (Option<Response>, f64) {
// Build a TSL connector
let mut connector_builder = TlsConnector::builder();
Expand Down Expand Up @@ -119,9 +118,7 @@ impl Request {
let interpolator = interpolator::Interpolator::new(context, responses);
interpolated_body = interpolator.resolve(body).to_owned();

request = client
.request(method, &interpolated_url)
.body(&interpolated_body);
request = client.request(method, &interpolated_url).body(&interpolated_body);
} else {
request = client.request(method, &interpolated_url);
}
Expand Down Expand Up @@ -152,7 +149,7 @@ impl Request {
println!("Error connecting '{}': {:?}", interpolated_url, e);
}
(None, duration_ms)
},
}
Ok(response) => {
let status_text = if response.status.is_server_error() {
response.status.to_string().red()
Expand All @@ -163,7 +160,7 @@ impl Request {
};

if !config.quiet {
println!("{:width$} {} {} {}", interpolated_name.green(), interpolated_url.blue().bold(), status_text, Request::format_time(duration_ms, config.nanosec).cyan(), width=25);
println!("{:width$} {} {} {}", interpolated_name.green(), interpolated_url.blue().bold(), status_text, Request::format_time(duration_ms, config.nanosec).cyan(), width = 25);
}
(Some(response), duration_ms)
}
Expand All @@ -180,9 +177,17 @@ impl Runnable for Request {
let (res, duration_ms) = self.send_request(context, responses, config);

match res {
None => reports.push(Report { name: self.name.to_owned(), duration: duration_ms, status: 520u16 }),
None => reports.push(Report {
name: self.name.to_owned(),
duration: duration_ms,
status: 520u16,
}),
Some(mut response) => {
reports.push(Report { name: self.name.to_owned(), duration: duration_ms, status: response.status.to_u16() });
reports.push(Report {
name: self.name.to_owned(),
duration: duration_ms,
status: response.status.to_u16(),
});

if let Some(&SetCookie(ref cookies)) = response.headers.get::<SetCookie>() {
if let Some(cookie) = cookies.iter().next() {
Expand All @@ -202,7 +207,5 @@ impl Runnable for Request {
}
}
}

}

}
}
20 changes: 10 additions & 10 deletions src/benchmark.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use std::thread;
use std::time;
use std::collections::HashMap;
use std::sync::Arc;
use std::thread;
use std::time;

use yaml_rust::Yaml;
use serde_json::Value;
use yaml_rust::Yaml;

use actions::{Report, Runnable};
use config;
use expandable::include;
use actions::{Runnable, Report};
use writer;

use colored::*;
Expand All @@ -20,9 +20,9 @@ fn thread_func(benchmark: Arc<Vec<Box<(Runnable + Sync + Send)>>>, config: Arc<c
let mut global_reports = Vec::new();

for iteration in 0..config.iterations {
let mut responses:HashMap<String, Value> = HashMap::new();
let mut context:HashMap<String, Yaml> = HashMap::new();
let mut reports:Vec<Report> = Vec::new();
let mut responses: HashMap<String, Value> = HashMap::new();
let mut context: HashMap<String, Yaml> = HashMap::new();
let mut reports: Vec<Report> = Vec::new();

context.insert("iteration".to_string(), Yaml::String(iteration.to_string()));
context.insert("thread".to_string(), Yaml::String(thread.to_string()));
Expand All @@ -38,8 +38,8 @@ fn thread_func(benchmark: Arc<Vec<Box<(Runnable + Sync + Send)>>>, config: Arc<c
global_reports.concat()
}

fn join<S:ToString> (l: Vec<S>, sep: &str) -> String {
l.iter().fold("".to_string(),
fn join<S: ToString>(l: Vec<S>, sep: &str) -> String {
l.iter().fold("".to_string(),
|a,b| if !a.is_empty() {a+sep} else {a} + &b.to_string()
)
}
Expand All @@ -64,7 +64,7 @@ pub fn execute(benchmark_path: &str, report_path_option: Option<&str>, no_check_

let list_arc = Arc::new(list);
let mut children = vec![];
let mut list_reports:Vec<Vec<Report>> = vec![];
let mut list_reports: Vec<Vec<Report>> = vec![];

if let Some(report_path) = report_path_option {
let reports = thread_func(list_arc.clone(), config, 0);
Expand Down
2 changes: 1 addition & 1 deletion src/checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub fn compare(list_reports: &Vec<Vec<Report>>, filepath: &str, threshold: &str)
let delta_ms = report_item.duration - recorded_duration;

if delta_ms > threshold_value {
println!("{:width$} is {}{} slower than before", report_item.name.green(), delta_ms.round().to_string().red(), "ms".red(), width=25);
println!("{:width$} is {}{} slower than before", report_item.name.green(), delta_ms.round().to_string().red(), "ms".red(), width = 25);

slow_counter += 1;
}
Expand Down
4 changes: 2 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl Config {
let rampup = read_i64_configuration(config_doc, "rampup", NRAMPUP);
let base = config_doc["base"].as_str().unwrap().to_owned();

Config{
Config {
base: base,
threads: threads,
iterations: iterations,
Expand All @@ -47,6 +47,6 @@ fn read_i64_configuration(config_doc: &Yaml, name: &str, default: i64) -> i64 {
println!("Invalid {} value!", name);

default
},
}
}
}
22 changes: 11 additions & 11 deletions src/expandable/include.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use std::process;
use yaml_rust::{YamlLoader, Yaml};
use std::path::Path;
use std::process;
use yaml_rust::{Yaml, YamlLoader};

use expandable::{multi_request, multi_csv_request, multi_iter_request, include};
use actions;
use actions::Runnable;
use expandable::{include, multi_csv_request, multi_iter_request, multi_request};

use reader;

pub fn is_that_you(item: &Yaml) -> bool{
pub fn is_that_you(item: &Yaml) -> bool {
item["include"].as_str().is_some()
}

Expand All @@ -29,12 +29,12 @@ pub fn expand_from_filepath(parent_path: &str, mut list: &mut Vec<Box<(Runnable

if let Some(accessor_id) = accessor {
items = match doc[accessor_id].as_vec() {
Some(items) => items,
None => {
println!("{} {}", "Node missing on config:", accessor_id);
println!("{}", "Exiting.");
process::exit(1)
},
Some(items) => items,
None => {
println!("{} {}", "Node missing on config:", accessor_id);
println!("{}", "Exiting.");
process::exit(1)
}
}
} else {
items = doc.as_vec().unwrap();
Expand All @@ -51,7 +51,7 @@ pub fn expand_from_filepath(parent_path: &str, mut list: &mut Vec<Box<(Runnable
include::expand(parent_path, item, &mut list);
} else if actions::Assign::is_that_you(item) {
list.push(Box::new(actions::Assign::new(item, None)));
} else if actions::Request::is_that_you(item){
} else if actions::Request::is_that_you(item) {
list.push(Box::new(actions::Request::new(item, None)));
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/expandable/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub mod include;

mod multi_request;
mod multi_csv_request;
mod multi_iter_request;
mod multi_request;
49 changes: 23 additions & 26 deletions src/expandable/multi_csv_request.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,33 @@
use yaml_rust::Yaml;
use std::path::Path;
use yaml_rust::Yaml;

use actions::{Request, Runnable};
use reader;

pub fn is_that_you(item: &Yaml) -> bool{
item["request"].as_hash().is_some() &&
(item["with_items_from_csv"].as_str().is_some() ||
item["with_items_from_csv"].as_hash().is_some())
pub fn is_that_you(item: &Yaml) -> bool {
item["request"].as_hash().is_some() && (item["with_items_from_csv"].as_str().is_some() || item["with_items_from_csv"].as_hash().is_some())
}

pub fn expand(parent_path: &str, item: &Yaml, list: &mut Vec<Box<(Runnable + Sync + Send)>>) {
let (with_items_path, quote_char) =
if let Some(with_items_path) = item["with_items_from_csv"].as_str() {
(with_items_path, '\"' as u8)
} else if let Some(_with_items_hash) = item["with_items_from_csv"].as_hash() {
let with_items_path = item["with_items_from_csv"]["file_name"].as_str().expect("Expected a file_name");
let quote_char = item["with_items_from_csv"]["quote_char"].as_str().unwrap_or("\"").bytes().nth(0).unwrap();

(with_items_path, quote_char)
} else {
panic!("WAT"); // Impossible case
};

let with_items_filepath = Path::new(parent_path).with_file_name(with_items_path);
let final_path = with_items_filepath.to_str().unwrap();

let with_items_file = reader::read_csv_file_as_yml(final_path, quote_char);

for with_item in with_items_file {
list.push(Box::new(Request::new(item, Some(with_item))));
}
let (with_items_path, quote_char) = if let Some(with_items_path) = item["with_items_from_csv"].as_str() {
(with_items_path, '\"' as u8)
} else if let Some(_with_items_hash) = item["with_items_from_csv"].as_hash() {
let with_items_path = item["with_items_from_csv"]["file_name"].as_str().expect("Expected a file_name");
let quote_char = item["with_items_from_csv"]["quote_char"].as_str().unwrap_or("\"").bytes().nth(0).unwrap();

(with_items_path, quote_char)
} else {
panic!("WAT"); // Impossible case
};

let with_items_filepath = Path::new(parent_path).with_file_name(with_items_path);
let final_path = with_items_filepath.to_str().unwrap();

let with_items_file = reader::read_csv_file_as_yml(final_path, quote_char);

for with_item in with_items_file {
list.push(Box::new(Request::new(item, Some(with_item))));
}
}

#[cfg(test)]
Expand All @@ -39,7 +36,7 @@ mod tests {
use actions::Runnable;

#[test]
fn expand_multi () {
fn expand_multi() {
let text = "---\nname: foobar\nrequest:\n url: /api/{{ item.id }}\nwith_items_from_csv: example/fixtures/users.csv";
let docs = yaml_rust::YamlLoader::load_from_str(text).unwrap();
let doc = &docs[0];
Expand Down
Loading

0 comments on commit e021228

Please sign in to comment.