{"id":26168758,"url":"https://github.com/nikkolasg/timed-rs","last_synced_at":"2026-04-21T00:32:36.210Z","repository":{"id":281248555,"uuid":"944692490","full_name":"nikkolasg/timed-rs","owner":"nikkolasg","description":"Simple macro to time any function","archived":false,"fork":false,"pushed_at":"2025-03-07T20:02:11.000Z","size":0,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-07T20:33:09.607Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nikkolasg.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-03-07T19:52:47.000Z","updated_at":"2025-03-07T20:02:15.000Z","dependencies_parsed_at":"2025-03-07T20:33:12.147Z","dependency_job_id":"98b16ebc-2ace-4d77-80ef-d9953a9e993d","html_url":"https://github.com/nikkolasg/timed-rs","commit_stats":null,"previous_names":["nikkolasg/timed-rs"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nikkolasg%2Ftimed-rs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nikkolasg%2Ftimed-rs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nikkolasg%2Ftimed-rs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nikkolasg%2Ftimed-rs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nikkolasg","download_url":"https://codeload.github.com/nikkolasg/timed-rs/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243092009,"owners_count":20235106,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-03-11T18:35:18.905Z","updated_at":"2025-12-25T00:39:42.253Z","avatar_url":"https://github.com/nikkolasg.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Timed\n\nA Rust library for timing function execution with configurable output. Measure and report function execution time with minimal code changes.\n\n## Features\n\n- **Simple API**: Time function execution with a single attribute macro: `#[timed::timed_instrument]`\n- **Configurable Log Levels**: Control verbosity with `#[timed::timed_instrument(level = \"debug\")]`\n- **Custom Naming**: Provide a custom name for output: `#[timed::timed_instrument(name = \"my_operation\")]`\n- **Multiple Output Methods**:\n  - Disable timing: `set_output(Output::Off)`\n  - Tracing logs: `set_output(Output::Tracing)`\n  - CSV file export: `set_output(Output::CSV(\"timing_results.csv\".to_string()))`\n- **Runtime Configuration**: Change output method dynamically without recompiling (useful for tests)\n- **Environment Variable Control**: Configure via `TIMED_OUTPUT=off|tracing|filename.csv`\n- **Thread-Safe**: Safe to use in multi-threaded applications\n\n## Installation\n\nAdd the dependencies to your `Cargo.toml` file:\n\n```toml\n[dependencies]\ntimed = { git = \"https://github.com/username/timed.git\" }\ntimed-core = { git = \"https://github.com/username/timed.git\" }\n```\n\nFor using tracing output, also add:\n\n```toml\n[dependencies]\ntracing = \"0.1\"\ntracing-subscriber = \"0.3\"\n```\n\n## Quick Start\n\n```rust\nuse timed_core::{set_output, Output};\n\n// Configure output method (once, at startup)\nset_output(Output::Tracing);\n\n// Add timing to any function\n#[timed::timed_instrument]\nfn my_function() {\n    // Function code - timing is now automatic\n}\n\n// Run your function\nmy_function();\n```\n\n## Detailed Usage\n\n### Setting Output Methods\n\n```rust\nuse timed_core::{set_output, Output};\n\n// Disable all timing output\nset_output(Output::Off);\n\n// Output to tracing logs (requires tracing setup)\nset_output(Output::Tracing);\n\n// Output to CSV file\nset_output(Output::CSV(\"function_timing.csv\".to_string()));\n```\n\n### Using the Macro\n\n```rust\n// Default timing (INFO level, uses function name)\n#[timed::timed_instrument]\nfn regular_function() {\n    // Function code\n}\n\n// Custom log level\n#[timed::timed_instrument(level = \"debug\")]\nfn debug_level_function() {\n    // Function code\n}\n\n// Custom name for output\n#[timed::timed_instrument(name = \"important_calculation\")]\nfn some_complex_task() {\n    // Function code\n}\n\n// Custom name and level\n#[timed::timed_instrument(level = \"trace\", name = \"low_level_io\")]\nfn read_from_socket() {\n    // Function code\n}\n\n// Works with any function type\n#[timed::timed_instrument]\nasync fn async_function() -\u003e Result\u003c(), Box\u003cdyn std::error::Error\u003e\u003e {\n    // Async function code\n    Ok(())\n}\n\n// Works with methods too\nimpl MyStruct {\n    #[timed::timed_instrument(name = \"my_struct_method\")]\n    fn my_method(\u0026self) {\n        // Method code\n    }\n}\n```\n\n### Setting Up Tracing\n\nWhen using `Output::Tracing`, you need to set up the tracing subscriber:\n\n```rust\nuse tracing::Level;\nuse tracing_subscriber::FmtSubscriber;\n\n// Setup tracing once at application startup\nfn setup_tracing() {\n    let subscriber = FmtSubscriber::builder()\n        .with_max_level(Level::TRACE)\n        .finish();\n    tracing::subscriber::set_global_default(subscriber)\n        .expect(\"Failed to set tracing subscriber\");\n}\n\nfn main() {\n    setup_tracing();\n    timed_core::set_output(timed_core::Output::Tracing);\n    \n    // Now timing output will appear in logs\n    my_function();\n}\n```\n\n### Environment Variable Configuration\n\nThe library can be configured using the `TIMED_OUTPUT` environment variable:\n\n```bash\n# Disable all timing (default if not set)\nTIMED_OUTPUT=off cargo run\n\n# Output timing using tracing logs\nTIMED_OUTPUT=tracing cargo run\n\n# Output timing to a CSV file\nTIMED_OUTPUT=timing_results.csv cargo run\n```\n\nIn your code, you can force a re-read of the environment variable:\n\n```rust\n// Update configuration from environment variable\ntimed_core::refresh_from_env();\n```\n\n### Reading Current Configuration\n\n```rust\nuse timed_core::{get_output, Output};\n\n// Get current output configuration\nlet current_output = get_output();\nmatch current_output {\n    Output::Off =\u003e println!(\"Timing is disabled\"),\n    Output::Tracing =\u003e println!(\"Using tracing output\"),\n    Output::CSV(filename) =\u003e println!(\"Writing to CSV: {}\", filename),\n}\n```\n\n## CSV Output Format\n\nWhen using CSV output, the library creates a file with:\n- Header row: `function,duration_ms`\n- One row per function call with the function name and execution time in milliseconds\n- The file is created fresh when `set_output()` is called and appended to on each function call\n\nExample CSV output:\n```csv\nfunction,duration_ms\nmy_function,12.345\nanother_function,3.890\n```\n\n## Testing\n\nThe library includes a comprehensive test suite:\n\n```bash\n# Run all tests\ncargo test\n\n# Run just the timed-test integration tests\ncargo test -p timed-test\n```\n\n## Workspace Structure\n\nThe library is organized as a Rust workspace with three crates:\n\n- `timed-core` - Core runtime with output configuration and timing functionality\n- `timed` - Procedural macro for the `timed_instrument` attribute\n- `timed-test` - Test suite and examples\n\n## Thread Safety\n\nThe library uses thread-safe constructs (Mutex, Lazy static) for configuration, making it safe to use in multi-threaded applications.\n\n## Common Issues\n\n1. **Timing output not appearing**: Make sure you've configured an output method with `set_output()` or the environment variable.\n\n2. **CSV file not created**: Check write permissions in the directory.\n\n3. **Tracing output not visible**: Ensure you've set up the tracing subscriber correctly.\n\n## License\n\n[MIT License](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnikkolasg%2Ftimed-rs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnikkolasg%2Ftimed-rs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnikkolasg%2Ftimed-rs/lists"}