1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
use std::{
    collections::HashMap,
    fs::{self, File},
    io::{BufReader, BufWriter},
    ops::Sub,
    path::{Path, PathBuf},
    process::Command,
    time::Duration,
};

use chrono::{DateTime, Duration as ChronoDuration, NaiveDateTime, Utc};
use getset::{Getters, MutGetters, Setters};
use serde::{Deserialize, Serialize};
use tempfile::NamedTempFile;
use url::Url;

use crate::{
    benchmark::{Benchmark, BenchmarkBuilder},
    error::WrkError,
    result::{Variance, WrkResult},
    Gnuplot, LuaScript, Result,
};

const DATE_FORMAT: &str = "%Y-%m-%d-%H:%M:%S-%z";

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum HistoryPeriod {
    Last,
    Hour,
    Day,
    Week,
    Month,
    Forever,
}

impl Default for HistoryPeriod {
    fn default() -> Self {
        HistoryPeriod::Last
    }
}

impl HistoryPeriod {
    pub fn last_valid_datapoint(&self) -> DateTime<Utc> {
        let now = Utc::now();
        match self {
            Self::Last => now,
            Self::Hour => now.sub(ChronoDuration::hours(1)),
            Self::Day => now.sub(ChronoDuration::days(1)),
            Self::Week => now.sub(ChronoDuration::weeks(1)),
            Self::Month => now.sub(ChronoDuration::weeks(4)),
            Self::Forever => DateTime::from_utc(NaiveDateTime::from_timestamp(1, 0), Utc),
        }
    }
}

pub type Benchmarks = Vec<WrkResult>;
pub type Headers = HashMap<String, String>;

/// Wrapper around Wrk enabling to run benchmarks, record historical data and plot graphs.
#[derive(Debug, Clone, Serialize, Deserialize, Getters, Setters, MutGetters, Builder)]
pub struct Wrk {
    /// Url of the service to benchmark against. Use the full URL of the request.
    /// IE: http://localhost:1234/some/uri.
    #[getset(get = "pub", set = "pub", get_mut = "pub")]
    url: String,
    /// Set of benchmarks for the current instance.
    #[builder(default)]
    #[getset(get = "pub", set = "pub", get_mut = "pub")]
    benchmarks: Benchmarks,
    /// Historical benchmarks data, indexed by dates.
    #[builder(default)]
    #[getset(get = "pub", set = "pub", get_mut = "pub")]
    benchmarks_history: Benchmarks,
    /// Directory on disk where to store and read the historical benchmark data.
    #[builder(default = "Path::new(\".\").join(\".wrk-api-bench\")")]
    #[getset(get = "pub", set = "pub", get_mut = "pub")]
    history_dir: PathBuf,
    /// User defined LUA script to run through wrk.
    /// **NOTE: This script MUST not override the wrk function `done()` as it already
    /// overriden by this crate to allow wrk to spit out a parsable JSON output.
    #[builder(default)]
    #[getset(get = "pub", set = "pub", get_mut = "pub")]
    user_script: Option<PathBuf>,
    /// Header to add to the wrk request.
    #[builder(default)]
    #[getset(get = "pub", set = "pub", get_mut = "pub")]
    headers: Headers,
    /// Method for the wrk request.
    #[builder(default = "String::from(\"GET\")")]
    #[getset(get = "pub", set = "pub", get_mut = "pub")]
    method: String,
    /// Body for the wrk request.
    #[builder(default)]
    #[getset(get = "pub", set = "pub", get_mut = "pub")]
    body: String,
    /// Max percentage of errors vs total request to conside a benchmark healthy.
    #[builder(default = "2")]
    #[getset(get = "pub", set = "pub", get_mut = "pub")]
    max_error_percentage: u8,
    /// Current benchmark date and time.
    #[serde(skip)]
    #[builder(default)]
    #[getset(get = "pub", set = "pub", get_mut = "pub")]
    benchmark_date: Option<DateTime<Utc>>,
}

impl Wrk {
    fn wrk_args(&self, benchmark: &Benchmark, url: &Url, lua_script: &Path) -> Result<Vec<String>> {
        Ok(vec![
            "-t".to_string(),
            benchmark.threads().to_string(),
            "-c".to_string(),
            benchmark.connections().to_string(),
            "-d".to_string(),
            format!("{}s", benchmark.duration().as_secs()),
            "-s".to_string(),
            lua_script.to_string_lossy().to_string(),
            url.to_string(),
        ])
    }

    fn wrk_result(&self, wrk_json: &str) -> WrkResult {
        match serde_json::from_str::<WrkResult>(wrk_json) {
            Ok(mut run) => {
                let error_percentage = run.errors() / 100.0 * run.requests();
                if error_percentage < *self.max_error_percentage() as f64 {
                    *run.success_mut() = true;
                } else {
                    error!(
                        "Errors percentage is {}%, which is more than {}%",
                        error_percentage, self.max_error_percentage
                    );
                }
                run
            }
            Err(e) => {
                error!("Wrk JSON result deserialize failed: {}", e);
                WrkResult::fail(e.to_string())
            }
        }
    }

    pub fn bench(&mut self, benchmarks: &Vec<Benchmark>) -> Result<()> {
        if !self.history_dir().exists() {
            fs::create_dir(self.history_dir()).unwrap_or_else(|e| {
                error!(
                    "Unable to create storage dir {}: {}. Statistics calculation could be impaired",
                    self.history_dir().display(),
                    e
                );
            });
        }
        let date = Utc::now();
        *self.benchmark_date_mut() = Some(date);
        let url = Url::parse(self.url())?;
        let mut script_file = NamedTempFile::new()?;
        LuaScript::render(
            &mut script_file,
            self.user_script().as_ref(),
            url.path(),
            self.method(),
            self.headers(),
            self.body(),
        )?;
        for benchmark in benchmarks {
            let mut run = match Command::new("wrk")
                .args(self.wrk_args(benchmark, &url, script_file.path())?)
                .output()
            {
                Ok(wrk) => {
                    let output = String::from_utf8_lossy(&wrk.stdout);
                    let error = String::from_utf8_lossy(&wrk.stderr);
                    if wrk.status.success() {
                        debug!("Wrk execution succeded:\n{}", output);
                        let wrk_json = output
                            .split("JSON")
                            .nth(1)
                            .ok_or_else(|| WrkError::Lua("Wrk returned empty JSON".to_string()))?;
                        self.wrk_result(wrk_json)
                    } else {
                        error!("Wrk execution failed.\nOutput: {}\nError: {}", output, error);
                        WrkResult::fail(error.to_string())
                    }
                }
                Err(e) => {
                    error!("Wrk execution failed: {}", e);
                    WrkResult::fail(e.to_string())
                }
            };
            *run.date_mut() = date;
            *run.benchmark_mut() = benchmark.clone();
            println!("Current run: {:?}", run);
            self.benchmarks_mut().push(run);
        }
        self.dump(date)?;
        Ok(())
    }

    pub fn bench_exponential(&mut self, duration: Option<Duration>) -> Result<()> {
        self.bench(&BenchmarkBuilder::exponential(duration))?;
        Ok(())
    }

    fn dump(&self, date: DateTime<Utc>) -> Result<()> {
        let filename = format!("result.{}.json", date.format(DATE_FORMAT));
        let file = File::create(self.history_dir().join(&filename))?;
        let writer = BufWriter::new(file);
        println!("Writing current benchmark to {}", filename);
        serde_json::to_writer(writer, &self.benchmarks())?;
        Ok(())
    }

    fn load(&mut self, period: HistoryPeriod, best: bool) -> Result<()> {
        if !self.history_dir().exists() {
            fs::create_dir(self.history_dir())?;
        }
        let mut paths: Vec<_> = fs::read_dir(self.history_dir())?.map(|r| r.unwrap()).collect();
        paths.sort_by_key(|dir| {
            let metadata = fs::metadata(dir.path()).unwrap();
            metadata.modified().unwrap()
        });
        let mut history = Benchmarks::new();
        if period == HistoryPeriod::Last {
            let file = File::open(paths.pop().unwrap().path())?;
            let mut reader = BufReader::new(file);
            history = serde_json::from_reader(&mut reader)?;
            let benchmark = history.pop().unwrap();
            if let Some(benchmark_date) = self.benchmark_date() {
                if benchmark_date == benchmark.date() && !paths.is_empty() {
                    let file = File::open(paths.pop().unwrap().path())?;
                    let mut reader = BufReader::new(file);
                    history = serde_json::from_reader(&mut reader)?;
                    if best {
                        let best = self.best_benchmark(&history)?;
                        history = vec![best];
                    }
                } else {
                    return Err(WrkError::History(
                        "Unable to load history with a single measurement".to_string(),
                    ));
                }
            }
        } else {
            for path in paths {
                if let Some(date_str) = path.file_name().to_string_lossy().split('.').nth(1) {
                    let date = DateTime::parse_from_str(date_str, DATE_FORMAT)?;
                    if date >= period.last_valid_datapoint() {
                        let file = File::open(path.path())?;
                        let mut reader = BufReader::new(file);
                        let mut benchmarks: Vec<_> = serde_json::from_reader(&mut reader)?;
                        benchmarks.retain(|x| !self.benchmarks_history().contains(x));
                        if best {
                            let best = self.best_benchmark(&benchmarks)?;
                            history.push(best);
                        } else {
                            history.append(&mut benchmarks);
                        }
                    }
                }
            }
        }
        *self.benchmarks_history_mut() = history;
        Ok(())
    }

    fn best_benchmark(&self, benchmarks: &Benchmarks) -> Result<WrkResult> {
        let best = benchmarks.iter().filter(|v| *v.success()).max_by(|a, b| {
            (*a.requests_sec() as i64)
                .cmp(&(*b.requests_sec() as i64))
                .then((*a.successes() as i64).cmp(&(*b.successes() as i64)))
                .then((*a.requests() as i64).cmp(&(*b.requests() as i64)))
                .then((*a.requests() as i64).cmp(&(*b.requests() as i64)))
                .then((*a.transfer_mb() as i64).cmp(&(*b.transfer_mb() as i64)))
        });
        best.cloned().ok_or_else(|| {
            WrkError::Stats(format!(
                "Unable to calculate best in a set of {} elements",
                benchmarks.len()
            ))
        })
    }

    fn best(&self) -> Result<WrkResult> {
        self.best_benchmark(self.benchmarks())
    }

    fn historical_best(&self) -> Result<WrkResult> {
        self.best_benchmark(self.benchmarks_history())
    }

    fn calculate_variance(&self, new: &f64, old: &f64) -> f64 {
        (new - old) / old * 100.0
    }

    pub fn all_benchmarks(&self) -> Benchmarks {
        let mut history = self.benchmarks_history().clone();
        history.append(&mut self.benchmarks().clone());
        history
    }

    pub fn variance(&mut self, period: HistoryPeriod) -> Result<Variance> {
        self.load(period, false)?;
        let new = self.best()?;
        let old = self.historical_best()?;
        Ok(Variance::new(new, old))
    }

    pub fn plot(&self, title: &str, output: &Path, benchmarks: &Benchmarks) -> Result<()> {
        Gnuplot::new(title, output).plot(benchmarks)
    }
}

mod tests {
    use std::{thread, time::Duration};

    use super::*;
    use crate::benchmark::BenchmarkBuilder;

    #[test]
    fn benchmark() {
        let mut wrk = WrkBuilder::default()
            .url("http://localhost:13734/some".to_string())
            .build()
            .unwrap();
        // wrk.bench_exponential(Some(Duration::from_secs(30))).unwrap();
        // println!("{}", wrk.variance(HistoryPeriod::Last).unwrap());
        // wrk.bench(&vec![BenchmarkBuilder::default()
        //     .duration(Duration::from_secs(5))
        //     .build()
        //     .unwrap()])
        //     .unwrap();
        wrk.load(HistoryPeriod::Day, false).unwrap();
        // wrk.plot("Wrk Weeeeeee", Path::new("./some.png"), &wrk.all_benchmarks())
        // .unwrap();
    }
}