Skip to content

Commit

Permalink
Merge pull request #27 from 191220029/data-manage
Browse files Browse the repository at this point in the history
Data manage: impl merge_stats
  • Loading branch information
genedna authored Apr 24, 2024
2 parents bd0a1a0 + 58ad640 commit 54e2ce6
Show file tree
Hide file tree
Showing 17 changed files with 202 additions and 41 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ jobs:
- name: Check formatting
run: |
cargo fmt --manifest-path collector/Cargo.toml -- --check
cargo fmt --manifest-path data_manage/Cargo.toml -- --check
continue-on-error: false

build-and-test:
Expand All @@ -40,5 +41,6 @@ jobs:
- name: Build and test
run: |
cargo build --verbose --manifest-path ./collector/Cargo.toml
cargo test --verbose --manifest-path ./collector/Cargo.toml
cargo build --verbose --manifest-path collector/Cargo.toml
cargo test --verbose --manifest-path collector/Cargo.toml
cargo test --verbose --manifest-path data_manage/Cargo.toml
3 changes: 3 additions & 0 deletions collector/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ edition = "2021"
#[profile.release]
#debug = true

[lib.profile]
warnings = false

[dependencies]
anyhow = "1"
serde = { version = "1", features = ["derive"] }
Expand Down
2 changes: 1 addition & 1 deletion collector/src/benchmark/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod benchmark;
pub(crate) mod benchmark;
pub mod profile;
pub mod scenario;
3 changes: 2 additions & 1 deletion collector/src/compile_time/binary_size/plot/plotter_cmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
assert(len(args) > 2)

raw_data = args[1].split(";")
raw_data.sort(key=lambda x: x.split(',')[0].lower())
out_file = args[2]

names = [item.split(',')[0] for item in raw_data]
Expand All @@ -16,7 +17,7 @@
plt.bar(names, values, color=colors)
plt.ylabel('Change rate of binary size (%)', fontsize=7)
plt.yticks(fontsize=7)
plt.xticks(rotation=90, fontsize=7)
plt.xticks(rotation=90, fontsize=6.5)

plt.tight_layout()
plt.savefig(out_file)
14 changes: 7 additions & 7 deletions collector/src/compile_time/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ use crate::{

use self::result::CompileTimeBenchResult;

pub mod binary_size;
pub mod cargo_package_process;
pub mod cargo_single_process;
pub(crate) mod binary_size;
pub(crate) mod cargo_package_process;
pub(crate) mod cargo_single_process;
pub mod result;

pub fn bench_compile_time(
pub(crate) fn bench_compile_time(
ltc: &LocalToolchain,
perf_tool: &PerfTool,
event_filter_file: &PathBuf,
Expand Down Expand Up @@ -54,7 +54,7 @@ pub fn bench_compile_time(
Ok(result)
}

pub fn bench<'a>(
pub(crate) fn bench<'a>(
perf_tool: &PerfTool,
event_filter_file: &PathBuf,
profiles: &[Profile],
Expand Down Expand Up @@ -97,7 +97,7 @@ pub fn bench<'a>(
Ok(result_vec)
}

pub trait CompileTimeProcessor {
pub(crate) trait CompileTimeProcessor {
fn run_rustc(
&mut self,
perf_tool: &PerfTool,
Expand Down Expand Up @@ -125,7 +125,7 @@ lazy_static::lazy_static! {
};
}

pub fn discover_benchmark_suit(dir: &Path) -> anyhow::Result<Vec<Benchamrk>> {
pub(crate) fn discover_benchmark_suit(dir: &Path) -> anyhow::Result<Vec<Benchamrk>> {
let mut benchmarks = vec![];

for entry in read_dir(dir)
Expand Down
24 changes: 8 additions & 16 deletions collector/src/compile_time/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ impl CompileTimeResultSet {
statistic_vec.push((label.clone(), Statistics::from(vals.clone())));
});

statistics.0.push(CompileTimeStatistic {
statistics.push(CompileTimeStatistic {
name: result.benchmark.clone(),
profile: profile.clone(),
scenario: scenario.clone(),
Expand All @@ -142,20 +142,12 @@ impl CompileTimeResultSet {
}

#[derive(Serialize, Deserialize)]
struct CompileTimeStatistic {
name: String,
profile: Profile,
scenario: Scenario,
iterations: u32,
statistic_vec: Vec<(String, Statistics)>,
pub struct CompileTimeStatistic {
pub name: String,
pub profile: Profile,
pub scenario: Scenario,
pub iterations: u32,
pub statistic_vec: Vec<(String, Statistics)>,
}

impl CompileTimeStatistic {}

#[derive(Serialize, Deserialize)]
pub struct CompileTimeStatistics(Vec<CompileTimeStatistic>);
impl CompileTimeStatistics {
fn new() -> Self {
Self(vec![])
}
}
pub type CompileTimeStatistics = Vec<CompileTimeStatistic>;
7 changes: 7 additions & 0 deletions collector/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pub mod benchmark;
pub mod compile_time;
mod execute;
mod runtime;
pub mod statistics;
pub mod toolchain;
mod utils;
14 changes: 0 additions & 14 deletions collector/src/toolchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,20 +332,6 @@ pub enum Commands {
/// Analyze perf.data from a local directory.
AnalyzeLocal {
/// The path of dir contains perf.data.
/// The data-dir should has struct like this:
///
/// + data-dir
///
/// |----rustc_commit_1
///
/// |----benchmark_group_x
/// |----benchmark_x
/// |----x_perf.data
/// |---- ...
/// |---- ...
/// |---- ...
/// |----rustc_commit_2
/// |---- ...
#[clap(long = "data-dir", default_value = "../perf_analyze")]
data_dir: PathBuf,

Expand Down
1 change: 1 addition & 0 deletions data_manage/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
13 changes: 13 additions & 0 deletions data_manage/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "manager"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1"
collector = { path = "../collector" }
clap = { version = "3.0.9", features = ["derive"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
33 changes: 33 additions & 0 deletions data_manage/src/commannds.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use collector::benchmark::profile::Profile;
use std::path::PathBuf;

#[derive(Debug, clap::Parser)]
#[clap(about, version, author)]
pub struct Cli {
#[clap(subcommand)]
pub command: Commands,
}

#[derive(Debug, clap::Subcommand)]
#[clap(rename_all = "snake_case")]
pub enum Commands {
/// Merge Data of the same rustc version from different benchmark groups
/// and calculate their statistics.
MergeStats {
/// Path to root of Stats dir
#[clap(long = "root-dir")]
root_dir: PathBuf,

/// `debug` or `release` or someother profiles of rustc.
#[clap(long = "profile")]
profile: Profile,

/// Version of rustc
#[clap(long = "rust-ver")]
rustc: String,

/// The path of output file
#[clap(long = "out-path", default_value = "results")]
out_path: PathBuf,
},
}
22 changes: 22 additions & 0 deletions data_manage/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use clap::Parser;
use commannds::Cli;
use merge_stat::merge_compile_time_stats;

mod commannds;
mod merge_stat;

fn main() {
let cli = Cli::parse();

match cli.command {
commannds::Commands::MergeStats {
root_dir,
profile,
rustc,
out_path,
} => match merge_compile_time_stats(&root_dir, profile, rustc, out_path) {
Ok(p) => println!("Write merged stats to {}", p.to_str().unwrap()),
Err(e) => eprintln!("{}", e),
},
}
}
97 changes: 97 additions & 0 deletions data_manage/src/merge_stat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
use std::{
fs::{read_dir, File},
io::{BufReader, BufWriter},
path::PathBuf,
};

use collector::{
benchmark::profile::Profile,
compile_time::result::{CompileTimeResultSet, CompileTimeStatistics},
};

pub fn merge_compile_time_stats(
root_dir: &PathBuf,
profile: Profile,
rustc: String,
out_path: PathBuf,
) -> anyhow::Result<PathBuf> {
let mut merged_stats = CompileTimeStatistics::new();

// Iterate each benchmark group under root dir.
for bench_group in read_dir(root_dir)? {
let bench_group = bench_group?;
// Iterate each rustc stats dir under root dir.
if bench_group.metadata()?.is_dir() {
for rustc_dir in read_dir(bench_group.path())? {
let rustc_dir = rustc_dir?;
// Find wanted rustc version.
if rustc_dir.metadata()?.is_dir()
&& rustc_dir.file_name().to_str().unwrap().to_string() == rustc
{
// Find statistics file
for f in read_dir(rustc_dir.path())? {
let f = f?;
if f.file_name().to_str().unwrap().contains("results.json") {
let data: CompileTimeResultSet =
serde_json::from_reader(BufReader::new(File::open(f.path())?))?;

merged_stats.append(
&mut data
.calculate_statistics()
.into_iter()
.filter(|s| s.profile == profile)
.collect(),
);
}
}
}
}
}
}

merged_stats.sort_by(|a, b| a.name.to_lowercase().cmp(&b.name.to_lowercase()));

serde_json::to_writer(BufWriter::new(File::create(&out_path)?), &merged_stats)?;

Ok(out_path)
}

#[cfg(test)]
mod test_merge_stat {
use std::{
fs::{remove_file, File},
io::BufReader,
path::PathBuf,
};

use collector::{benchmark::profile::Profile, compile_time::result::CompileTimeStatistics};

use super::merge_compile_time_stats;

/// test for merge_stat
///
/// Step1. merge stats in `test/merge_stat/stat` for `rustc_A`.
///
/// Step2. verify length of merged stats.
///
/// Step3. clean up.
#[test]
fn test_merge_stat() {
let root_dir = PathBuf::from("test/merge_stat/stat");
let profile = Profile::Release;
let rustc = String::from("rustc_A");
let out_path = PathBuf::from("test/merge_stat/merge.json");

assert_eq!(
merge_compile_time_stats(&root_dir, profile, rustc, out_path.clone()).unwrap(),
out_path,
);

let stats: CompileTimeStatistics =
serde_json::from_reader(BufReader::new(File::open(&out_path).unwrap())).unwrap();

assert_eq!(12, stats.len());

remove_file(out_path).unwrap();
}
}

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

0 comments on commit 54e2ce6

Please sign in to comment.