Skip to content

Commit

Permalink
Create a simple profiler for Rust code
Browse files Browse the repository at this point in the history
  • Loading branch information
nukeop committed Nov 13, 2023
1 parent 37d35f8 commit 468cb3f
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 15 deletions.
85 changes: 85 additions & 0 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"type": "git",
"url": "git+https://github.com/nukeop/nuclear.git"
},
"author": "nukeop <nukeop@gumblert.tech>",
"author": "nukeop <12746779+nukeop@users.noreply.github.com>",
"license": "AGPL-3.0",
"bugs": {
"url": "https://github.com/nukeop/nuclear/issues"
Expand Down Expand Up @@ -58,4 +58,4 @@
"ts-node": "^10.7.0",
"typescript": "^4.2.4"
}
}
}
4 changes: 2 additions & 2 deletions packages/i18n/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"type": "git",
"url": "git+https://github.com/nukeop/nuclear.git"
},
"author": "nukeop <nukeop@gumblert.tech>",
"author": "nukeop <12746779+nukeop@users.noreply.github.com>",
"license": "AGPL-3.0",
"bugs": {
"url": "https://github.com/nukeop/nuclear/issues"
Expand All @@ -25,4 +25,4 @@
"ts-node": "^10.7.0",
"typescript": "^4.2.4"
}
}
}
2 changes: 1 addition & 1 deletion packages/main/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"type": "git",
"url": "git+https://github.com/nukeop/nuclear.git"
},
"author": "nukeop <nukeop@gumblert.tech>",
"author": "nukeop <12746779+nukeop@users.noreply.github.com>",
"license": "AGPL-3.0",
"bugs": {
"url": "https://github.com/nukeop/nuclear/issues"
Expand Down
8 changes: 4 additions & 4 deletions packages/scanner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ exclude = ["index.node"]
[lib]
crate-type = ["cdylib"]

[features]
profiling = []

[dependencies]
derive_builder = "0.12.0"
id3 = "1.7.0"
Expand All @@ -21,10 +24,7 @@ features = ["webp-encoder"]

[dependencies.uuid]
version = "1.3.4"
features = [
"v4",
"fast-rng"
]
features = ["v4", "fast-rng"]

[dependencies.neon]
version = "0.10"
Expand Down
1 change: 1 addition & 0 deletions packages/scanner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"types": "index.d.ts",
"scripts": {
"build": "cargo-cp-artifact -nc index.node -- cargo build --message-format=json-render-diagnostics",
"build-profiling": "cargo-cp-artifact -nc index.node -- cargo build --message-format=json-render-diagnostics --features profiling",
"build-debug": "npm run build --",
"build-release": "npm run build -- --release",
"install": "npm run build-release",
Expand Down
1 change: 1 addition & 0 deletions packages/scanner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod error;
mod js;
mod local_track;
mod metadata;
mod profiling;
mod scanner;
mod thumbnails;
use error::ScannerError;
Expand Down
43 changes: 43 additions & 0 deletions packages/scanner/src/profiling.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#[cfg(feature = "profiling")]
pub struct Profiler {
label: String,
start: std::time::Instant,
}

#[cfg(feature = "profiling")]
impl Profiler {
pub fn start(label: &str) -> Self {
Profiler {
label: label.to_owned(),
start: std::time::Instant::now(),
}
}

pub fn end(self) {
let duration = self.start.elapsed();
println!("{} - Elapsed time: {:?}", self.label, duration);
}
}

#[cfg(feature = "profiling")]
impl Drop for Profiler {
fn drop(&mut self) {
let duration = self.start.elapsed();
println!("{} - Elapsed time: {:?}", self.label, duration);
}
}

// Provide no-op implementations when profiling feature is not enabled
#[cfg(not(feature = "profiling"))]
pub struct Profiler;

#[cfg(not(feature = "profiling"))]
impl Profiler {
pub fn start(_label: &str) -> Self {
Profiler {}
}

pub fn end(self) {
// Do nothing
}
}
21 changes: 17 additions & 4 deletions packages/scanner/src/thumbnails.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use metaflac::Tag as FlacTag;
use std::io::{self, Cursor};
use std::path::{Path, PathBuf};

use crate::profiling::Profiler;

pub trait ThumbnailGenerator {
fn generate_thumbnail(filename: &str, thumbnails_dir: &str) -> Option<String>;
fn read_image_data(filename: &str) -> Option<Vec<u8>>;
Expand Down Expand Up @@ -59,15 +61,26 @@ fn generate_thumbnail_common<T: ThumbnailGenerator>(
}

fn resize_and_save_thumbnail(data: &[u8], path: &Path) -> ImageResult<()> {
let _profiler = Profiler::start("resize_and_save_thumbnail");
let img = get_resized_image(data)?;
img.save_with_format(path, ImageFormat::WebP)
}

fn get_resized_image(data: &[u8]) -> Result<ImageBuffer<Rgba<u8>, Vec<u8>>, ImageError> {
ImageReader::new(Cursor::new(data))
.with_guessed_format()?
.decode()
.map(|img| resize(&img, 192, 192, FilterType::Lanczos3))
let img = ImageReader::new(Cursor::new(data));

let guess_format_profiler = Profiler::start("guess_format");
let format = img.with_guessed_format()?;
guess_format_profiler.end();

let decode_start = Profiler::start("decode");
let decoded = format.decode()?;
decode_start.end();

let resize_start = Profiler::start("resize");
let resized = resize(&decoded, 256, 256, FilterType::CatmullRom);
resize_start.end();
return Ok(resized);
}

fn create_and_get_thumbnail_path(filename: &str, thumbnails_dir: &str) -> Option<String> {
Expand Down
4 changes: 2 additions & 2 deletions packages/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@nuclear/ui",
"version": "0.6.30",
"description": "Nuclear - UI components",
"author": "nukeop <nukeop@gumblert.tech>",
"author": "nukeop <12746779+nukeop@users.noreply.github.com>",
"homepage": "https://github.com/nukeop/nuclear/tree/master/packages/ui#readme",
"license": "AGPL-3.0",
"files": [
Expand Down Expand Up @@ -93,4 +93,4 @@
"styled-components": "^4.4.1",
"yup": "^0.32.9"
}
}
}

0 comments on commit 468cb3f

Please sign in to comment.