Skip to content

Commit

Permalink
Group/split code into modules/submodules
Browse files Browse the repository at this point in the history
Some files got too big.
This refactoring groups related files under
common parent modules.
It also moves context functions registration
from workload to the context module.
  • Loading branch information
pkolaczk committed Aug 11, 2024
1 parent ddf5e9f commit 60079eb
Show file tree
Hide file tree
Showing 25 changed files with 1,287 additions and 1,256 deletions.
1,150 changes: 0 additions & 1,150 deletions src/context.rs

This file was deleted.

2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::context::CassError;
use crate::scripting::cass_error::CassError;
use err_derive::*;
use hdrhistogram::serialization::interval_log::IntervalLogWriterError;
use hdrhistogram::serialization::V2DeflateSerializeError;
Expand Down
2 changes: 1 addition & 1 deletion src/chunks.rs → src/exec/chunks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ where

#[cfg(test)]
mod test {
use crate::chunks::{ChunksAggregated, ChunksExt};
use crate::exec::chunks::{ChunksAggregated, ChunksExt};
use futures::{stream, FutureExt, StreamExt};
use std::time::Duration;
use tokio::time::interval;
Expand Down
2 changes: 1 addition & 1 deletion src/cycle.rs → src/exec/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl BoundedCycleCounter {

#[cfg(test)]
mod test {
use crate::cycle::{CycleCounter, BATCH_SIZE};
use crate::exec::cycle::{CycleCounter, BATCH_SIZE};
use itertools::Itertools;
use std::collections::BTreeSet;

Expand Down
7 changes: 6 additions & 1 deletion src/exec.rs → src/exec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@ use tokio::signal::ctrl_c;
use tokio::time::MissedTickBehavior;
use tokio_stream::wrappers::IntervalStream;

use crate::chunks::ChunksExt;
use crate::error::{LatteError, Result};
use crate::{
BenchmarkStats, BoundedCycleCounter, Interval, Progress, Recorder, Workload, WorkloadStats,
};
use chunks::ChunksExt;

mod chunks;
pub mod cycle;
pub mod progress;
pub mod workload;

/// Returns a stream emitting `rate` events per second.
fn interval_stream(rate: f64) -> IntervalStream {
Expand Down
File renamed without changes.
74 changes: 7 additions & 67 deletions src/workload.rs → src/exec/workload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ use std::sync::Arc;
use std::time::Duration;
use std::time::Instant;

use crate::error::LatteError;
use crate::scripting::cass_error::{CassError, CassErrorKind};
use crate::scripting::context::Context;
use crate::stats::latency::LatencyDistributionRecorder;
use crate::stats::session::SessionStats;
use rand::distributions::{Distribution, WeightedIndex};
use rand::rngs::SmallRng;
use rand::{Rng, SeedableRng};
Expand All @@ -15,14 +20,10 @@ use rune::compile::meta::Kind;
use rune::compile::{CompileVisitor, MetaError, MetaRef};
use rune::runtime::{AnyObj, Args, RuntimeContext, Shared, VmError, VmResult};
use rune::termcolor::{ColorChoice, StandardStream};
use rune::{vm_try, Any, Diagnostics, Module, Source, Sources, ToValue, Unit, Value, Vm};
use rune::{vm_try, Any, Diagnostics, Source, Sources, ToValue, Unit, Value, Vm};
use serde::{Deserialize, Serialize};
use try_lock::TryLock;

use crate::error::LatteError;
use crate::latency::LatencyDistributionRecorder;
use crate::{context, CassError, CassErrorKind, Context, SessionStats};

/// Wraps a reference to Session that can be converted to a Rune `Value`
/// and passed as one of `Args` arguments to a function.
struct SessionRef<'a> {
Expand Down Expand Up @@ -115,69 +116,8 @@ impl Program {
/// - `script`: source code in Rune language
/// - `params`: parameter values that will be exposed to the script by the `params!` macro
pub fn new(source: Source, params: HashMap<String, String>) -> Result<Program, LatteError> {
let mut context_module = Module::default();
context_module.ty::<Context>().unwrap();
context_module.function_meta(context::execute).unwrap();
context_module.function_meta(context::prepare).unwrap();
context_module
.function_meta(context::execute_prepared)
.unwrap();
context_module.function_meta(context::elapsed_secs).unwrap();

let mut err_module = Module::default();
err_module.ty::<CassError>().unwrap();
err_module.function_meta(CassError::string_display).unwrap();

let mut uuid_module = Module::default();
uuid_module.ty::<context::Uuid>().unwrap();
uuid_module
.function_meta(context::Uuid::string_display)
.unwrap();

let mut latte_module = Module::with_crate("latte").unwrap();
latte_module
.macro_("param", move |ctx, ts| context::param(ctx, &params, ts))
.unwrap();

latte_module.function_meta(context::blob).unwrap();
latte_module.function_meta(context::text).unwrap();
latte_module.function_meta(context::now_timestamp).unwrap();
latte_module.function_meta(context::hash).unwrap();
latte_module.function_meta(context::hash2).unwrap();
latte_module.function_meta(context::hash_range).unwrap();
latte_module.function_meta(context::hash_select).unwrap();
latte_module.function_meta(context::uuid).unwrap();
latte_module.function_meta(context::normal).unwrap();
latte_module.function_meta(context::uniform).unwrap();

latte_module.function_meta(context::i64::to_i32).unwrap();
latte_module.function_meta(context::i64::to_i16).unwrap();
latte_module.function_meta(context::i64::to_i8).unwrap();
latte_module.function_meta(context::i64::to_f32).unwrap();
latte_module.function_meta(context::i64::clamp).unwrap();

latte_module.function_meta(context::f64::to_i8).unwrap();
latte_module.function_meta(context::f64::to_i16).unwrap();
latte_module.function_meta(context::f64::to_i32).unwrap();
latte_module.function_meta(context::f64::to_f32).unwrap();
latte_module.function_meta(context::f64::clamp).unwrap();

let mut fs_module = Module::with_crate("fs").unwrap();
fs_module.function_meta(context::read_to_string).unwrap();
fs_module.function_meta(context::read_lines).unwrap();
fs_module
.function_meta(context::read_resource_to_string)
.unwrap();
fs_module
.function_meta(context::read_resource_lines)
.unwrap();

let mut context = rune::Context::with_default_modules().unwrap();
context.install(&context_module).unwrap();
context.install(&err_module).unwrap();
context.install(&uuid_module).unwrap();
context.install(&latte_module).unwrap();
context.install(&fs_module).unwrap();
crate::scripting::install(&mut context, params);

let mut options = rune::Options::default();
options.debug_info(true);
Expand Down
29 changes: 9 additions & 20 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,35 +27,24 @@ use crate::config::{
AppConfig, Command, ConnectionConf, EditCommand, HdrCommand, Interval, ListCommand,
LoadCommand, SchemaCommand, ShowCommand,
};
use crate::context::*;
use crate::context::{CassError, CassErrorKind, Context, SessionStats};
use crate::cycle::BoundedCycleCounter;
use crate::error::{LatteError, Result};
use crate::exec::{par_execute, ExecutionOptions};
use crate::plot::plot_graph;
use crate::progress::Progress;
use crate::report::{PathAndSummary, Report, RunConfigCmp};
use crate::scripting::connect::ClusterInfo;
use crate::scripting::context::Context;
use crate::stats::{BenchmarkCmp, BenchmarkStats, Recorder};
use crate::table::{Alignment, Table};
use crate::workload::{FnRef, Program, Workload, WorkloadStats, LOAD_FN};
use exec::cycle::BoundedCycleCounter;
use exec::progress::Progress;
use exec::workload::{FnRef, Program, Workload, WorkloadStats, LOAD_FN};
use report::plot::plot_graph;
use report::table::{Alignment, Table};

mod chunks;
mod config;
mod context;
mod cycle;
mod error;
mod exec;
mod histogram;
mod latency;
mod percentiles;
mod plot;
mod progress;
mod report;
mod scripting;
mod stats;
mod table;
mod throughput;
mod timeseries;
mod workload;

const VERSION: &str = env!("CARGO_PKG_VERSION");

Expand Down Expand Up @@ -122,7 +111,7 @@ fn find_workload(workload: &Path) -> PathBuf {
/// Connects to the server and returns the session
async fn connect(conf: &ConnectionConf) -> Result<(Context, Option<ClusterInfo>)> {
eprintln!("info: Connecting to {:?}... ", conf.addresses);
let session = context::connect(conf).await?;
let session = scripting::connect::connect(conf).await?;
let cluster_info = session.cluster_info().await?;
eprintln!(
"info: Connected to {} running Cassandra version {}",
Expand Down
7 changes: 5 additions & 2 deletions src/report.rs → src/report/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::config::{RunCommand, WeightedFunction};
use crate::percentiles::Percentile;
use crate::stats::percentiles::Percentile;
use crate::stats::{BenchmarkCmp, BenchmarkStats, Mean, Sample, Significance};
use crate::table::Row;
use chrono::{DateTime, Local, TimeZone};
use console::{pad_str, style, Alignment};
use core::fmt;
Expand All @@ -15,6 +14,10 @@ use std::num::NonZeroUsize;
use std::path::{Path, PathBuf};
use std::{fs, io};
use strum::IntoEnumIterator;
use table::Row;

pub mod plot;
pub mod table;

/// A standard error is multiplied by this factor to get the error margin.
/// For a normally distributed random variable,
Expand Down
2 changes: 1 addition & 1 deletion src/plot.rs → src/report/plot.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::config::PlotCommand;
use crate::load_report_or_abort;
use crate::plot::SeriesKind::{ResponseTime, Throughput};
use crate::report::plot::SeriesKind::{ResponseTime, Throughput};
use crate::report::Report;
use crate::Result;
use itertools::Itertools;
Expand Down
2 changes: 1 addition & 1 deletion src/table.rs → src/report/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl<R: Row> Display for Table<R> {

#[cfg(test)]
mod test {
use crate::table::{Alignment, Row, Table};
use crate::report::table::{Alignment, Row, Table};

#[test]
fn render_table() {
Expand Down
Loading

0 comments on commit 60079eb

Please sign in to comment.