Skip to content

Commit

Permalink
chore(memory-profiling): add option for macos (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
pbillaut authored Sep 24, 2024
1 parent 46c179b commit ac42aed
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
25 changes: 23 additions & 2 deletions Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ command = "cargo"
args = ["build", "--profile", "${BUILD_PROFILE_PERF}"]

[tasks.heaptrack]
description = "Run heaptrack"
description = "Profile memory usage with heaptrack (Linux)"
condition = { platforms = ["linux"] }
dependencies = ["build-perf"]
script = '''
SIZE="${1:-10K}"
Expand Down Expand Up @@ -41,5 +42,25 @@ fi
OUTPUT_FILE="${CARGO_MAKE_WORKING_DIRECTORY}/heaptrack.${CARGO_MAKE_CRATE_NAME}.${SIZE}.${TAG}"
BINARY="${CARGO_MAKE_CRATE_TARGET_DIRECTORY}/${BUILD_PROFILE_PERF}/${CARGO_MAKE_CRATE_NAME}"
heaptrack --output "${OUTPUT_FILE}" "${BINARY}" "${SAMPLE_FILE}"
heaptrack --output "${OUTPUT_FILE}" "${BINARY}" --silent "${SAMPLE_FILE}"
'''

[tasks.instruments]
description = "Profile memory usage with instruments (macOS)"
condition = { platforms = ["mac"] }
script = '''
SIZE="${1:-10K}"
SAMPLE_FILE="${BENCHES_DATA_DIR}/activities_${SIZE}.csv"
if [ ! -f "$SAMPLE_FILE" ]; then
echo "ERROR: File '${SAMPLE_FILE}' does not exist."
echo
echo "Available sample files in '${DATA_DIR}':"
for file in ${BENCHES_DATA_DIR}/activities_*.csv; do
echo "• $(basename "$file")"
done
echo
exit 1
fi
cargo instruments --profile perf -t Allocations -- --silent "${SAMPLE_FILE}"
'''
31 changes: 29 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,28 @@

A simple toy payment processor.

## Profiling

### Memory

#### Linux

To profile memory usage on Linux with [heaptrack][tool:heaptrack], run

```shell
cargo make heaptrack [1K|10K]
```

#### macOS

To profile memory usage on macOS with [Instruments][tool:instruments],
install [cargo-instruments][tool:cargo-instruments] and
run

```shell
cargo make instruments [1K|10K]
```

## A Note on Parsing

In parsing a CSV file containing different types of records, such as transactions and dispute events, using separate
Expand Down Expand Up @@ -49,9 +71,14 @@ As above, since no specific performance target has been set, there is no strong
that rely on native floating-point types. The advantages provided by [`rust_decimal`][crate:rust_decimal], particularly
in terms of precision and error avoidance, outweigh the potential performance gains from using floats.

[type:decimal]: https://docs.rs/rust_decimal/latest/rust_decimal/struct.Decimal.html

[crate:csv]: https://docs.rs/csv/latest

[crate:rust_decimal]: https://docs.rs/rust_decimal/latest

[tool:cargo-instruments]: https://crates.io/crates/cargo-instruments

[tool:heaptrack]: https://github.com/KDE/heaptrack

[tool:instruments]: https://help.apple.com/instruments/mac/current

[type:decimal]: https://docs.rs/rust_decimal/latest/rust_decimal/struct.Decimal.html
11 changes: 10 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use anyhow::Context;
use clap::{Parser, ValueHint};
use payment_processor::processor::Processor;
use payment_processor::processors::csv::CsvProcessor;
use std::io::Write;
use std::{fs::File, io, path::PathBuf};
use tracing_subscriber::EnvFilter;

Expand All @@ -13,6 +14,14 @@ struct Cli {
/// Supported file formats: CSV
#[arg(value_hint = ValueHint::FilePath)]
path: PathBuf,

/// Whether to suppress printing the results to stdout.
#[clap(long, action)]
silent: bool,
}

fn output(silent: bool) -> Box<dyn Write> {
if silent { Box::new(io::sink()) } else { Box::new(io::stdout()) }
}

fn main() -> Result<(), anyhow::Error> {
Expand All @@ -24,6 +33,6 @@ fn main() -> Result<(), anyhow::Error> {
let cli = Cli::parse();
let file = File::open(&cli.path).context("unable to open file input file")?;

let mut processor = CsvProcessor::try_new(file, io::stdout())?;
let mut processor = CsvProcessor::try_new(file, output(cli.silent))?;
processor.process().context("processing input file failed")
}

0 comments on commit ac42aed

Please sign in to comment.