{"id":20085410,"url":"https://github.com/klaxit/micro_bench","last_synced_at":"2025-06-21T17:42:39.875Z","repository":{"id":56883540,"uuid":"155108192","full_name":"klaxit/micro_bench","owner":"klaxit","description":"⏰ Dead simple, non intrusive, realtime benchmarks","archived":false,"fork":false,"pushed_at":"2020-07-15T07:30:47.000Z","size":27,"stargazers_count":13,"open_issues_count":0,"forks_count":0,"subscribers_count":11,"default_branch":"master","last_synced_at":"2025-04-09T06:51:11.905Z","etag":null,"topics":["benchmark","ruby"],"latest_commit_sha":null,"homepage":"","language":"Ruby","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/klaxit.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-10-28T19:36:18.000Z","updated_at":"2022-02-14T16:27:49.000Z","dependencies_parsed_at":"2022-08-20T22:31:08.401Z","dependency_job_id":null,"html_url":"https://github.com/klaxit/micro_bench","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/klaxit%2Fmicro_bench","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/klaxit%2Fmicro_bench/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/klaxit%2Fmicro_bench/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/klaxit%2Fmicro_bench/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/klaxit","download_url":"https://codeload.github.com/klaxit/micro_bench/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252604606,"owners_count":21775120,"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","ruby"],"created_at":"2024-11-13T15:55:51.526Z","updated_at":"2025-05-06T01:32:56.620Z","avatar_url":"https://github.com/klaxit.png","language":"Ruby","readme":"\n# MicroBench\n\n[![Gem Version](https://badge.fury.io/rb/micro_bench.svg)](https://badge.fury.io/rb/micro_bench)\n[![CircleCI](https://circleci.com/gh/klaxit/micro_bench.svg?style=shield)](https://circleci.com/gh/klaxit/micro_bench)\n\n```\ngem install micro_bench\n```\n\n## Why ?\n\nRuby `Benchmark` module is nice but it uses blocks. We see 2 problems to it :\n- if we want to instrument a snippet of code, it breaks git history,\n- variables declared in the benchmark block cannot be used subsequently.\n\nLet's say you want to output the duration of `method_1` from :\n\n```ruby\nfoo = method_1\nmethod_2(foo)\n```\n\nWith `Benchmark`, we would write something like :\n```ruby\nfoo = nil\nduration = Benchmark.realtime do\n  foo = method_1\nend\nputs \"Method 1 duration : #{duration} seconds\"\nmethod_2(foo)\n```\n\nWith `MicroBench`, it will look like this :\n```ruby\nMicroBench.start\nfoo = method_1\nputs \"Method 1 duration : #{MicroBench.duration} seconds\"\nmethod_2(foo)\n```\n\n## Project maturity\n\nThis has been running in production at [Klaxit](https://www.klaxit.com) for some months now, but it is still a young library. API may be subject to breaking changes on MINOR versions (but not on PATCH versions).\n\n\n## Install\n\n```\ngem install micro_bench\n```\n\nor in Bundler:\n```ruby\ngem \"micro_bench\"\n```\n\n## Usage\n\n### Basic usage\n\n```ruby\nrequire \"micro_bench\"\n\nMicroBench.start\n\nMicroBench.duration\n# 1.628093000501394\n\nMicroBench.duration\n# 2.999483000487089\n\nMicroBench.stop\n# true\n\nMicroBench.duration\n# 5.4341670004651\n\nMicroBench.duration\n# 5.4341670004651\n```\n\n### Named benchmarks\n\n```ruby\nrequire \"micro_bench\"\n\nMicroBench.start(\"timer1\")\n\nMicroBench.stop(\"timer1\")\n\nMicroBench.duration(\"timer1\")\n# 1.628093000501394\n```\n\n### Multiple starts\n\nCalling `.start` multiple times with the same `bench_id` (or none) will cause a \"restart\" of the given benchmark.\n\n### Thread safety\n\nA benchmark is tied to a thread, ensuring that `MicroBench` is thread-safe. At the same time, it doesn't allow to share a benchmark between multiple threads.\n\n### Methods isolation\n\nA benchmark is tied to its calling method so the following code will output 2 separated durations for `method_1` and `method_2`. This prevent collisions when using `MicroBench` in a large codebase.\n\n```ruby\ndef method_1\n  MicroBench.start\n  method_2\n  # Do something\n  MicroBench.stop\n  puts \"method_1 duration : #{MicroBench.duration}\"\nend\n\ndef method_2\n  MicroBench.start\n  # Do something\n  MicroBench.stop\n  puts \"method_2 duration : #{MicroBench.duration}\"\nend\n\nmethod_1\n```\n\n### Configuration\n\nYou can configure a bit `MicroBench` to have an even simpler time using it afterwards. Here's how:\n\n```ruby\nMicroBench.configure do |config|\n  config.formatter = -\u003e(duration) { \"#{duration.round} seconds\" }\nend\n\nMicroBench.start\nsleep 2\nMicroBench.duration == \"2 seconds\"\n```\n\nThere are some default formatters that you can set:\n\n| id        | result                       | description                              |\n| --------- | ---------------------------- | ---------------------------------------- |\n| `default` | `722.327823832`              | the raw original float, for computation  |\n| `simple`  | `722.33`                     | rounds to 2 digits                       |\n| `mmss`    | `\"12:02.328\"`                | Easy to understand, lossless and compact |\n| `human`   | `\"12 minutes and 2 seconds\"` | For humans, clutters logs                |\n\nTo use one of those, simply write `config.formatter = :simple` instead of a lambda.\n\n## Versioning\n\nWe use [SemVer](http://semver.org/) for versioning. For the versions available, see the [tags on this repository](https://github.com/klaxit/micro_bench/tags).\n\nWhile still in beta (before `1.0.0`), API may be subject to breaking changes on MINOR versions (but not on PATCH versions).\n\n## Authors\n\nSee the list of [contributors](https://github.com/klaxit/micro_bench/contributors) who participated in this project.\n\n## License\n\nPlease see LICENSE\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fklaxit%2Fmicro_bench","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fklaxit%2Fmicro_bench","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fklaxit%2Fmicro_bench/lists"}