{"id":18471481,"url":"https://github.com/emanzione/mhlab.benchmark","last_synced_at":"2025-07-05T00:04:59.681Z","repository":{"id":143022135,"uuid":"152988136","full_name":"emanzione/MHLab.Benchmark","owner":"emanzione","description":"A library to measure and profile some metrics of your code (execution time, average execution time, garbage collections)","archived":false,"fork":false,"pushed_at":"2021-03-19T15:30:19.000Z","size":21,"stargazers_count":7,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-05T00:04:57.579Z","etag":null,"topics":["benchmark","benchmarking-framework","dotnet","dotnetcore","measure"],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/emanzione.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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":"2018-10-14T15:21:34.000Z","updated_at":"2021-03-19T15:24:25.000Z","dependencies_parsed_at":"2023-04-24T08:37:27.042Z","dependency_job_id":null,"html_url":"https://github.com/emanzione/MHLab.Benchmark","commit_stats":null,"previous_names":["manhunterita/mhlab.benchmark"],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/emanzione/MHLab.Benchmark","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emanzione%2FMHLab.Benchmark","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emanzione%2FMHLab.Benchmark/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emanzione%2FMHLab.Benchmark/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emanzione%2FMHLab.Benchmark/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/emanzione","download_url":"https://codeload.github.com/emanzione/MHLab.Benchmark/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emanzione%2FMHLab.Benchmark/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263636791,"owners_count":23492304,"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":["benchmark","benchmarking-framework","dotnet","dotnetcore","measure"],"created_at":"2024-11-06T10:17:06.195Z","updated_at":"2025-07-05T00:04:59.673Z","avatar_url":"https://github.com/emanzione.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MHLab.Benchmark\nA single-file utility to measure and profile some metrics from your code execution (execution time, average execution time, garbage collections).\n\n![Build](https://github.com/manhunterita/MHLab.Benchmark/workflows/Build/badge.svg)\n[![Nuget](https://img.shields.io/nuget/v/MHLab.Benchmark)](https://www.nuget.org/packages/MHLab.Benchmark/)\n\n## Why?\nProfiling/measuring how a piece of code performs is always a good thing. It helps to optimize resources usage and it saves costs!\n\nI know that the great [Benchmark.NET](https://github.com/dotnet/BenchmarkDotNet) already exists and you really should use it for precise and in-depth profiling, but sometimes I just wanted to test a little snippet of code in a single line, without too many settings.\n\n## How to use it\nIt is really simple to use this utility. Just add it as reference from NuGet or include `Benchmarker.cs` file into your project.\n\nThen just take a look at the following snippets.\n\nPrepare your benchmark parameters:\n\n```csharp\nvar parameters = new BenchmarkParameters()\n{\n    // How many times your action will be invoked.\n    BenchmarkIterations = 100_000,\n\n    // Determines if the warmup will be performed.\n    PerformWarmup = true,\n\n    // The initialization action that will be invoked before the warmup.\n    InitializeAction = null,\n\n    // How many times your action will be invoked without collecting results.\n    WarmupIterations = 1000\n};\n```\n\nThen you are ready to collect metrics from your code:\n\n```csharp\nprivate static void ActionToTest()\n{\n    // Your code to profile here\n}\n\nBenchmarkResult result = Benchmarker.Start(ActionToTest, parameters);\n```\n\nOr, if you have particular needs and the classic linear loop performed by this utility is not enough for you, simply call `StartScope` to define the benchmarking logic by yourself:\n\n```csharp\nBenchmarkResult result = new BenchmarkResult(\"MyCode-1\");\n\nusing (var benchmark = Benchmarker.StartScope(result))\n{\n    // Your code to profile here\n}\n\n// You can now use the result here, it has been populated with metrics.\n```\n\nAlso, take in mind that `BenchmarkResult` has a `ToString` override to show results easily:\n\n```csharp\nConsole.WriteLine(result.ToString());\n\n/*\nOutput:\n\nExecution Time (TimeSpan): {result.Elapsed}\nExecution Time (ms): {result.ElapsedMilliseconds}\nExecution Ticks: {result.ElapsedTicks}\nAverage Execution Time (ms): {result.AverageMilliseconds}\nAverage Execution Ticks: {result.AverageTicks}\nGarbage Collections (0): {result.GarbageCollections0Count}\nGarbage Collections (1): {result.GarbageCollections1Count}\nGarbage Collections (2): {result.GarbageCollections2Count}\n*/\n```\n\n## Comparing results\nA good thing when you benchmark your code is comparing results from different methods against a baseline and check what changed.\n\nTo do so, check this snippet:\n\n```csharp\nprivate static void ActionToTest()\n{\n    // Your code to profile here\n}\n\nprivate static void ActionToTest1()\n{\n    // Your code to profile here\n}\n\nprivate static void ActionToTest2()\n{\n    // Your code to profile here\n}\n\nvar actions = new Action[]\n{\n    ActionToTest1,\n    ActionToTest2\n};\n\nBenchmarkComparison[] comparisons = Benchmarker.StartAndCompare(ActionToTest, parameters, actions);\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femanzione%2Fmhlab.benchmark","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femanzione%2Fmhlab.benchmark","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femanzione%2Fmhlab.benchmark/lists"}