Skip to content

Commit

Permalink
n-tests option for rustfinity runner (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodesdev authored Sep 30, 2024
1 parent 5be9d14 commit f673e45
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 8 deletions.
6 changes: 6 additions & 0 deletions crates/rustfinity-runner/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@ docker run -i \
rustfinity-runner \
/bin/bash -c "/app/rustfinity-runner run --code 'cHViIGZuIGhlbGxvX3dvcmxkKCkgewogICAgcHJpbnRsbiEoIkdvb2Qgam9iLCB5b3UgZGVjb2RlZCBpdCA6RCIpCn0K' --challenge 'printing-hello-world'"
```

### Arguments

- `--code`: The base64 encoded code to run.
- `--challenge`: The challenge name. This is used to identify the challenge in the logs.
- `--n-tests` or `n`: The number of tests to run (takes the minimum amount of time in `ms` and prints it).
3 changes: 2 additions & 1 deletion crates/rustfinity-runner/example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

cargo -q run run \
--code cHViIGZuIGhlbGxvX3dvcmxkKCkgewogICAgcHJpbnRsbiEoImhlbGxvIHdvcmxkIikKfQo= \
--challenge printing-hello-world
--challenge printing-hello-world \
-n 10
6 changes: 5 additions & 1 deletion crates/rustfinity-runner/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ pub enum Commands {

#[clap(long)]
/// Challenge slug
challenge: String,
challenge: Option<String>,

#[clap(long = "n-tests", short)]
/// number of tests to take the minimum time of
n_tests: Option<usize>,
},
}
38 changes: 34 additions & 4 deletions crates/rustfinity-runner/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,42 @@ use std::time::Instant;

use crate::regex::extract_unittest_path;

pub async fn run_code(code_base64: &str, challenge: &str) -> anyhow::Result<String> {
pub struct RunCodeParams {
code_base64: String,
challenge: String,
n_tests: usize,
}

impl RunCodeParams {
pub fn new(code_base64: String, challenge: Option<String>, n_tests: Option<usize>) -> Self {
Self {
code_base64,
challenge: challenge.unwrap_or("playground".to_string()),
n_tests: n_tests.unwrap_or(1),
}
}
}

pub async fn run_code(params: &RunCodeParams) -> anyhow::Result<String> {
let RunCodeParams {
code_base64,
challenge,
n_tests,
} = params;

let mut output = String::new();

let tests_output = run_tests(&code_base64, &challenge).await?;
output.push_str(&tests_output);

let test_binary_path = extract_unittest_path(&output);

if challenge.as_str() == "playground" {
return Ok(output);
}

if let Some(test_binary_path) = test_binary_path {
let time_output = benchmark_time_min(&challenge, &test_binary_path).await?;
let time_output = benchmark_time_min(&challenge, &test_binary_path, n_tests).await?;
let memory_output = memory_benchmark(&challenge, &test_binary_path).await?;

output.push_str("\n");
Expand Down Expand Up @@ -50,10 +76,14 @@ async fn benchmark_time(challenge: &str, test_binary_path: &str) -> anyhow::Resu
}

/// Runs the tests 10 times and gets the minimum time
async fn benchmark_time_min(challenge: &str, test_binary_path: &str) -> anyhow::Result<String> {
async fn benchmark_time_min(
challenge: &str,
test_binary_path: &str,
n_tests: &usize,
) -> anyhow::Result<String> {
let mut nums = Vec::with_capacity(10);

for _ in 0..10 {
for _ in 0..*n_tests {
let time = benchmark_time(&challenge, &test_binary_path).await?;
nums.push(time);
}
Expand Down
7 changes: 5 additions & 2 deletions crates/rustfinity-runner/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clap::Parser;
use command::run_code;
use command::{run_code, RunCodeParams};
use dotenvy::dotenv;

mod cli;
Expand All @@ -18,8 +18,11 @@ async fn main() {
Commands::Run {
code: code_base64,
challenge,
n_tests,
} => {
match run_code(&code_base64, &challenge).await {
let params = RunCodeParams::new(code_base64, challenge, n_tests);

match run_code(&params).await {
Ok(output) => println!("{}", output),
Err(e) => eprintln!("{}", e),
};
Expand Down

0 comments on commit f673e45

Please sign in to comment.