Skip to content

Commit

Permalink
bench(feat): Allow specifying cobc optimisation level, build only.
Browse files Browse the repository at this point in the history
  • Loading branch information
c272 committed May 9, 2024
1 parent 256dfaf commit 273e5df
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
11 changes: 11 additions & 0 deletions crates/bench/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ pub struct Cli {
#[arg(short = 'O', long, value_parser = clap::builder::PossibleValuesParser::new(&["none", "speed", "speed_and_size"]))]
pub cobalt_opt_level: Option<String>,

/// The optimisation level to use when building with `cobc`.
/// By default, turns on all optimisations (-O3).
#[arg(short = 'C', long, default_value_t = 3)]
pub cobc_opt_level: u8,

/// Whether to disable generation of hardware security instructions by
/// Cobalt when generating benchmarking binaries.
#[arg(long, short = 'h', action)]
Expand All @@ -28,6 +33,10 @@ pub struct Cli {
#[arg(long, short = 'g', action)]
pub run_comparative: bool,

/// Whether to run only a build test and not execute benchmarks.
#[arg(long, short = 'g', action)]
pub build_only: bool,

/// Whether to ignore benchmarks from the default benchmark directory.
/// Ignored if a benchmark directory is directly specified.
#[arg(long, short = 'i', action)]
Expand Down Expand Up @@ -205,8 +214,10 @@ impl TryInto<Cfg> for Cli {
Ok(Cfg {
compiler: cobalt_bin,
cobalt_opt_level: opt_level,
cobc_opt_level: self.cobc_opt_level,
disable_hw_security: self.disable_hw_security,
run_comparative: self.run_comparative,
build_only: self.build_only,
output_dir,
output_log,
benchmarks: all_benchmarks,
Expand Down
7 changes: 4 additions & 3 deletions crates/bench/src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,10 @@ pub(crate) struct BenchmarkResult {
pub compile_time_avg: Duration,

/// The time taken to execute the benchmark for the specified
/// iterations.
pub execute_time_total: Duration,
/// iterations. Not present if no execution was performed.
pub execute_time_total: Option<Duration>,

/// The time taken to execute the benchmark once, on average.
pub execute_time_avg: Duration,
/// Not present if no execution was performed.
pub execute_time_avg: Option<Duration>,
}
24 changes: 20 additions & 4 deletions crates/bench/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,19 @@ pub(crate) struct Cfg {
/// The optimisation level to run Cobalt at when compiling.
pub cobalt_opt_level: String,

/// The optimisation level to run `cobc` at when compiling.
pub cobc_opt_level: u8,

/// Whether to disable generating hardware security instructions
/// when compiling sources with Cobalt.
pub disable_hw_security: bool,

/// Whether to run comparative tests against GnuCobol's `cobc`.
pub run_comparative: bool,

/// Whether to only build and not execute benchmarks.
pub build_only: bool,

/// The output directory for benchmark artifacts.
pub output_dir: PathBuf,

Expand Down Expand Up @@ -109,7 +115,12 @@ fn run_cobalt(cfg: &Cfg, benchmark: &Benchmark) -> Result<BenchmarkResult> {
);

// Run the target program.
let (execute_time_total, execute_time_avg) = run_bench_bin(cfg, benchmark)?;
let (execute_time_total, execute_time_avg) = if !cfg.build_only {
let (x, y) = run_bench_bin(cfg, benchmark)?;
(Some(x), Some(y))
} else {
(None, None)
};

Ok(BenchmarkResult {
compile_time_total: elapsed,
Expand All @@ -125,7 +136,7 @@ fn run_cobc(cfg: &Cfg, benchmark: &Benchmark) -> Result<BenchmarkResult> {
let mut bench_bin_path = cfg.output_dir.clone();
bench_bin_path.push(BENCH_BIN_NAME);
let mut cobc = Command::new("cobc");
cobc.args(["-x", "-O3", "-free"])
cobc.args(["-x", &format!("-O{}", cfg.cobc_opt_level), "-free"])
.args(["-o", bench_bin_path.to_str().unwrap()])
.arg(&benchmark.source_file);

Expand All @@ -150,8 +161,13 @@ fn run_cobc(cfg: &Cfg, benchmark: &Benchmark) -> Result<BenchmarkResult> {
);

// Run the target program.
let (execute_time_total, execute_time_avg) = run_bench_bin(cfg, benchmark)?;

let (execute_time_total, execute_time_avg) = if !cfg.build_only {
let (x, y) = run_bench_bin(cfg, benchmark)?;
(Some(x), Some(y))
} else {
(None, None)
};

Ok(BenchmarkResult {
compile_time_total: elapsed,
compile_time_avg: elapsed / 100,
Expand Down
4 changes: 2 additions & 2 deletions examples/algorithms/questionmarks.cobol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ DATA DIVISION.
WORKING-STORAGE SECTION.
01 INPUT-LEN PIC 9(5) COMP.
01 INPUT-STR PIC X(1000).
01 CUR-IDX PIC 9(5) COMP VALUE 0.
01 CUR-IDX PIC 9(5) COMP VALUE 1.
01 CUR-CHAR PIC X(1).
01 OUT-NUM PIC S9(1) COMP.
01 LAST-NUM PIC S9(1) COMP VALUE -1.
Expand All @@ -17,7 +17,7 @@ PROCEDURE DIVISION.
DISPLAY "Please enter a string to analyse.".
ACCEPT INPUT-STR.
MOVE FUNCTION LENGTH(INPUT-STR) TO INPUT-LEN.
PERFORM ANALYSIS-LOOP UNTIL CUR-IDX >= INPUT-LEN.
PERFORM ANALYSIS-LOOP UNTIL CUR-IDX > INPUT-LEN.
IF CONFORMANT = 1 THEN
DISPLAY "The string is conformant with the specification."
ELSE
Expand Down

0 comments on commit 273e5df

Please sign in to comment.