Skip to content

Commit

Permalink
finish thread-safe sqlite impl, save contract deployments to DB. unce…
Browse files Browse the repository at this point in the history
…rtain about binding DB to Spammer, prob changing
  • Loading branch information
zeroXbrock committed Sep 13, 2024
1 parent 8298db9 commit ccaec20
Show file tree
Hide file tree
Showing 7 changed files with 295 additions and 179 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
cargotest.toml
*.db
120 changes: 90 additions & 30 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ lazy_static = "1.5.0"
rand = "0.8.5"
serde = "1.0.209"
serde_json = "1.0.128"
sqlite = "0.36.1"
tokio = { version = "1.40.0", features = ["rt-multi-thread"] }
toml = "0.8.19"
foundry-common = { git = "https://github.com/foundry-rs/foundry" }
alloy-json-abi = "0.7.7"
r2d2_sqlite = "0.25.0"
rusqlite = "0.32.1"
r2d2 = "0.8.10"
93 changes: 13 additions & 80 deletions src/bin/cli.rs
Original file line number Diff line number Diff line change
@@ -1,92 +1,25 @@
use clap::{Parser, Subcommand};
mod cli_lib;

use cli_lib::{ContenderCli, ContenderSubcommand};
use contender_core::{
db::{database::DbOps, sqlite::SqliteDb},
generator::{
testfile::{SetupGenerator, SpamGenerator, TestConfig},
RandSeed,
},
spammer::Spammer,
};
use std::sync::LazyLock;

#[derive(Parser, Debug)]
struct ContenderCli {
#[command(subcommand)]
command: ContenderSubcommand,
}

#[derive(Debug, Subcommand)]
enum ContenderSubcommand {
#[command(
name = "spam",
long_about = "Spam the RPC with tx requests as designated in the given testfile."
)]
Spam {
/// The path to the test file to use for spamming.
testfile: String,

/// The HTTP JSON-RPC URL to spam with requests.
rpc_url: String,

/// The number of txs to send per second.
#[arg(short, long, default_value = "10", long_help = "Number of txs to send per second", visible_aliases = &["tps"])]
intensity: Option<usize>,

/// The duration of the spamming run in seconds.
#[arg(
short,
long,
default_value = "60",
long_help = "Duration of the spamming run in seconds"
)]
duration: Option<usize>,

/// The seed to use for generating spam transactions. If not provided, one is generated.
#[arg(
short,
long,
long_help = "The seed to use for generating spam transactions"
)]
seed: Option<String>,
},

#[command(
name = "setup",
long_about = "Run the setup step(s) in the given testfile."
)]
Setup {
/// The path to the test file to use for setup.
testfile: String,

/// The HTTP JSON-RPC URL to use for setup.
rpc_url: String,
},

#[command(
name = "report",
long_about = "Export performance reports for data analysis."
)]
Report {
/// The run ID to export reports for. If not provided, the latest run is used.
#[arg(
short,
long,
long_help = "The run ID to export reports for. If not provided, the latest run is used."
)]
id: Option<String>,

/// The path to save the report to.
/// If not provided, the report is saved to the current directory.
#[arg(
short,
long,
long_help = "Filename of the saved report. May be a fully-qualified path. If not provided, the report is saved to the current directory."
)]
out_file: Option<String>,
},
}
// TODO: is this the best solution? feels like there's something better out there lmao
static DB: LazyLock<SqliteDb> = std::sync::LazyLock::new(|| {
SqliteDb::from_file("contender.db").expect("failed to open contender.db")
});

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let args = ContenderCli::parse();
let args = ContenderCli::parse_args();
let _ = DB.create_tables(); // ignore error; tables already exist
match args.command {
ContenderSubcommand::Spam {
testfile,
Expand All @@ -98,12 +31,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let testfile = TestConfig::from_file(&testfile)?;
let rand_seed = seed.map(|s| RandSeed::from_str(&s)).unwrap_or_default();
let gen = SpamGenerator::new(testfile, &rand_seed);
let spammer = Spammer::new(gen, rpc_url);
let spammer = Spammer::new(gen, &*DB, rpc_url);
spammer.spam_rpc(intensity.unwrap_or_default(), duration.unwrap_or_default())?;
}
ContenderSubcommand::Setup { testfile, rpc_url } => {
let gen: SetupGenerator = TestConfig::from_file(&testfile)?.into();
let spammer = Spammer::new(gen, rpc_url);
let spammer = Spammer::new(gen, &*DB, rpc_url);
spammer.spam_rpc(10, 1)?;
}
ContenderSubcommand::Report { id, out_file } => {
Expand Down
Loading

0 comments on commit ccaec20

Please sign in to comment.