Skip to content

Commit

Permalink
Add profile run to ci
Browse files Browse the repository at this point in the history
  • Loading branch information
TrueDoctor committed Aug 12, 2024
1 parent da13f21 commit 42f5aa1
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 14 deletions.
85 changes: 85 additions & 0 deletions .github/workflows/profile.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
name: Rust Profiling with iai-callgrind

on:
pull_request:
branches: [ main ]

env:
CARGO_TERM_COLOR: always

jobs:
profile:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Install Rust
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable

- name: Install Valgrind
run: |
sudo apt-get update
sudo apt-get install -y valgrind
- name: Cache dependencies
uses: actions/cache@v3
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}

- name: Install iai-callgrind
run: |
cargo install [email protected]
cargo add --dev [email protected]
- name: Checkout main branch
run: |
git fetch origin main:main
git checkout main
- name: Run baseline benchmarks
run: |
cargo bench --bench compile_demo_art --features=iai -- --save-baseline=main
- name: Checkout PR branch
run: |
git checkout ${{ github.event.pull_request.head.sha }}
- name: Run PR benchmarks
run: |
cargo bench --bench compile_demo_art --features=iai -- --baseline main > benchmark_results.txt
- name: Parse benchmark results
id: parse_results
run: |
COMMENT="## Performance Benchmark Results\n\n"
while IFS= read -r line; do
if [[ $line == *"Comparison with"* ]]; then
COMMENT+="### $line\n"
elif [[ $line == *"Instructions:"* || $line == *"L1 Hits:"* || $line == *"Estimated Cycles:"* ]]; then
COMMENT+="$line\n"
fi
done < benchmark_results.txt
echo "comment<<EOF" >> $GITHUB_OUTPUT
echo -e "$COMMENT" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Comment PR
uses: actions/github-script@v6
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.name,
body: ${{steps.parse_results.outputs.comment}}
})
35 changes: 35 additions & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions node-graph/graph-craft/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ dealloc_nodes = []
wgpu = []
tokio = ["dep:tokio"]
wayland = []
criterion = []
iai = []

[dependencies]
# Local dependencies
Expand Down Expand Up @@ -57,12 +59,14 @@ glob = "0.3"
pprof = { version = "0.13", features = ["flamegraph"] }
serde_json = { workspace = true }
graph-craft = { workspace = true, features = ["serde"] }
iai-callgrind = "0.12.3"


[[bench]]
name = "compile_demo_art"
harness = false


# [[bench]]
# name = "exec_demo_art"
# harness = false
56 changes: 42 additions & 14 deletions node-graph/graph-craft/benches/compile_demo_art.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,55 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use graph_craft::{document::NodeNetwork, graphene_compiler::Compiler, proto::ProtoNetwork};
use std::path::PathBuf;

pub fn compile_to_proto(c: &mut Criterion) {
let artworks = glob::glob("../../demo-artwork/*.graphite").expect("failed to read glob pattern");
for path in artworks {
let Ok(path) = path else { continue };
let content = std::fs::read(&path).expect("failed to read file");
let network = load_network(std::str::from_utf8(&content).unwrap());
let name = path.file_stem().unwrap().to_str().unwrap();
#[cfg(feature = "criterion")]
use criterion::{black_box, criterion_group, criterion_main, Criterion};

c.bench_function(name, |b| b.iter_batched(|| network.clone(), |network| compile(black_box(network)), criterion::BatchSize::SmallInput));
}
}
#[cfg(feature = "iai")]
use iai_callgrind::{black_box, library_benchmark, library_benchmark_group, main};

fn load_network(document_string: &str) -> NodeNetwork {
let document: serde_json::Value = serde_json::from_str(&document_string).expect("Failed to parse document");
let network = serde_json::from_value::<NodeNetwork>(document["network_interface"]["network"].clone()).expect("Failed to parse document");
network
let document: serde_json::Value = serde_json::from_str(document_string).expect("Failed to parse document");
serde_json::from_value::<NodeNetwork>(document["network_interface"]["network"].clone()).expect("Failed to parse document")
}

fn compile(network: NodeNetwork) -> ProtoNetwork {
let compiler = Compiler {};
compiler.compile_single(network).unwrap()
}

fn bench_compile(path: PathBuf) {
let content = std::fs::read(&path).expect("failed to read file");
let network = load_network(std::str::from_utf8(&content).unwrap());
black_box(compile(black_box(network)));
}

#[cfg(feature = "criterion")]
fn compile_to_proto(c: &mut Criterion) {
let artworks = glob::glob("../../demo-artwork/*.graphite").expect("failed to read glob pattern");
for path in artworks {
let Ok(path) = path else { continue };
let name = path.file_stem().unwrap().to_str().unwrap();
c.bench_function(name, |b| b.iter(|| bench_compile(path.clone())));
}
}

#[cfg_attr(feature = "iai", library_benchmark)]
fn iai_compile_to_proto() {
let artworks = glob::glob("../../demo-artwork/*.graphite").expect("failed to read glob pattern");
for path in artworks {
let Ok(path) = path else { continue };
bench_compile(path);
}
}

#[cfg(feature = "criterion")]
criterion_group!(benches, compile_to_proto);

#[cfg(feature = "criterion")]
criterion_main!(benches);

#[cfg(feature = "iai")]
library_benchmark_group!(name = compile_group; benchmarks = iai_compile_to_proto);

#[cfg(feature = "iai")]
main!(library_benchmark_groups = compile_group);

0 comments on commit 42f5aa1

Please sign in to comment.