Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 54 additions & 27 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,48 +14,75 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
rust: [stable]
rust: [stable, 1.80.0]
fail-fast: false
max-parallel: 2

steps:
- name: Checkout code
uses: actions/checkout@v3
with:
fetch-depth: 1

- name: Set up Rust
uses: actions-rs/toolchain@v1
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
with:
toolchain: ${{ matrix.rust }}
override: true
components: rustfmt, clippy

- name: Build project
uses: actions-rs/cargo@v1
- name: Rust Cache
uses: actions/cache@v3
with:
command: build
args: --release
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: ${{ runner.os }}-rust-${{ matrix.rust }}-${{ hashFiles('**/Cargo.lock') }}
restore-keys: |
${{ runner.os }}-rust-${{ matrix.rust }}-
${{ runner.os }}-rust-

- name: Check formatting
run: cargo fmt --all -- --check
continue-on-error: true

- name: Run clippy
run: cargo clippy -- -D warnings
continue-on-error: true

- name: Build project
run: cargo build --release
timeout-minutes: 20
if: always()

- name: Run tests
uses: actions-rs/cargo@v1
with:
command: test
args: --release
run: cargo test --release
timeout-minutes: 30
if: success()

- name: Package binary (Unix-like)
if: runner.os != 'Windows'
run: |
mkdir -p dist
cp target/release/treegen dist/
tar -czvf treegen-${{ runner.os }}.tar.gz dist/treegen
- name: Create dist directory
run: mkdir -p dist
shell: bash

- name: Package binary (Windows)
if: runner.os == 'Windows'
- name: Package binary
shell: bash
run: |
mkdir dist
Copy-Item target\release\treegen.exe dist\
Compress-Archive -Path dist\treegen.exe -DestinationPath treegen-${{ runner.os }}.zip
if [ "$RUNNER_OS" == "Windows" ]; then
cp target/release/treegen.exe LICENSE README.md dist/
7z a "treegen-${RUNNER_OS}-${GITHUB_SHA}.zip" ./dist/*
else
cp target/release/treegen LICENSE README.md dist/
tar -czvf "treegen-${RUNNER_OS}-${GITHUB_SHA}.tar.gz" dist/
fi

- name: Upload artifact
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
if: success()
with:
name: treegen-${{ runner.os }}
name: treegen-${{ runner.os }}-${{ matrix.rust }}-${{ github.sha }}
path: |
treegen-${{ runner.os }}.tar.gz
treegen-${{ runner.os }}.zip
treegen-*.tar.gz
treegen-*.zip
retention-days: 5
69 changes: 69 additions & 0 deletions Cargo.lock

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

7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ description = "A CLI tool to generate file trees from Markdown/YAML/JSON definit
license = "MIT"
repository = "https://github.com/AnNingUI/treegen"

[[bin]]
name = "bench"
path = "src/bench.rs"

[dependencies]
anyhow = "1.0.98"
clap = { version = "4.5.39", features = ["derive"] }
json5 = "0.4.1"
num_cpus = "1.17.0"
rayon = "1.11.0"
regex = "1.11.1"
serde = { version = "1.0.219", features = ["derive"] }
serde_json = "1.0.140"
serde_yaml = "0.9.34"
toml = "0.5" # 添加 toml crate 依赖
toml = "0.5"
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2025 AnNingUI

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
71 changes: 71 additions & 0 deletions src/bench.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use anyhow::Result;
use std::path::PathBuf;
use std::time::Instant;

use treegen::{create_fs_parallel, CreateFsOptions, Node};

/// 构造一个包含 N 个文件的 Node 树,每个文件大小为 file_size 字节
fn generate_tree(num_files: usize, file_size: usize) -> Node {
let mut root = Node::new_dir("bench".to_string());
let content = "A".repeat(file_size);

for i in 0..num_files {
let name = format!("file_{i}.txt");
root.children
.push(Node::new_file(name, Some(content.clone())));
}
root
}

fn run_bench(num_files: usize, file_size: usize, jobs: usize) -> Result<()> {
let root = generate_tree(num_files, file_size);
let out_dir = PathBuf::from(format!("bench_out_{num_files}_{file_size}_{jobs}"));

let opts = CreateFsOptions {
dry_run: false,
verbose: false,
force: true,
mode: 0o644,
jobs,
batch_log: 0,
};

let start = Instant::now();
create_fs_parallel(&out_dir, &root, &opts)?;
let elapsed = start.elapsed();

let total_bytes = num_files * file_size;
let mb = total_bytes as f64 / (1024.0 * 1024.0);
let secs = elapsed.as_secs_f64();
let throughput = mb / secs;

println!(
"Files: {:>6}, Size: {:>6} KB, Threads: {:>2} | Time: {:>6.2} s | {:.2} MB/s",
num_files,
file_size / 1024,
jobs,
secs,
throughput
);

Ok(())
}

fn main() -> Result<()> {
// 测试不同规模
let configs = vec![
(1000, 1024), // 1k 个文件,每个 1KB
(10_000, 1024), // 1w 个文件,每个 1KB
(1000, 1024 * 1024), // 1k 个文件,每个 1MB
];

let threads = vec![1, 4, 8];

for &(num_files, file_size) in &configs {
for &j in &threads {
run_bench(num_files, file_size, j)?;
}
}

Ok(())
}
Loading
Loading