{"id":21569440,"url":"https://github.com/naturalcycles/bench-lib","last_synced_at":"2025-04-10T14:06:41.944Z","repository":{"id":39618253,"uuid":"258650300","full_name":"NaturalCycles/bench-lib","owner":"NaturalCycles","description":"Benchmarking library, based on Benchmark.js and Autocannon","archived":false,"fork":false,"pushed_at":"2025-04-05T17:17:20.000Z","size":1093,"stargazers_count":2,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-05T17:22:39.527Z","etag":null,"topics":["autocannon","benchmark","benchmarkjs","javascript","typescript"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","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/NaturalCycles.png","metadata":{"files":{"readme":"readme.md","changelog":"CHANGELOG.md","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":"2020-04-24T23:58:10.000Z","updated_at":"2025-04-05T17:16:40.000Z","dependencies_parsed_at":"2024-03-06T13:39:39.291Z","dependency_job_id":"a60bed32-d523-428c-bc27-c5fc45e792c2","html_url":"https://github.com/NaturalCycles/bench-lib","commit_stats":{"total_commits":36,"total_committers":2,"mean_commits":18.0,"dds":"0.41666666666666663","last_synced_commit":"cb3791e9b2ef0c1f28be7b407a4df3156a4200fd"},"previous_names":[],"tags_count":28,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NaturalCycles%2Fbench-lib","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NaturalCycles%2Fbench-lib/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NaturalCycles%2Fbench-lib/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NaturalCycles%2Fbench-lib/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/NaturalCycles","download_url":"https://codeload.github.com/NaturalCycles/bench-lib/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248231899,"owners_count":21069424,"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":["autocannon","benchmark","benchmarkjs","javascript","typescript"],"created_at":"2024-11-24T11:09:30.255Z","updated_at":"2025-04-10T14:06:41.917Z","avatar_url":"https://github.com/NaturalCycles.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"## @naturalcycles/bench-lib\n\n\u003e Benchmarking library, based on [Benchmark.js](https://github.com/bestiejs/benchmark.js/) and\n\u003e [Autocannon](https://github.com/mcollina/autocannon)\n\n[![npm](https://img.shields.io/npm/v/@naturalcycles/bench-lib/latest.svg)](https://www.npmjs.com/package/@naturalcycles/bench-lib)\n[![min.gz size](https://badgen.net/bundlephobia/minzip/@naturalcycles/bench-lib)](https://bundlephobia.com/result?p=@naturalcycles/bench-lib)\n[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)\n\n# Why\n\nOpinionated, high-level benchmarking library.\n\nAllows to quickly benchmark your **functions** in a traditional benchmark.js or being served from a\nbare Express.js http server.\n\nSee examples below!\n\n# Show me the code 1 (benchmark.js)\n\n```typescript\nimport { runBenchScript } from '@naturalcycles/bench-lib'\n\nrunBenchScript({\n  fns: {\n    noop: done =\u003e done.resolve(),\n    random: done =\u003e {\n      const _ = Math.random()\n      done.resolve()\n    },\n    timeout: done =\u003e {\n      setTimeout(() =\u003e done.resolve(), 0)\n    },\n    immediate: done =\u003e {\n      setImmediate(() =\u003e done.resolve())\n    },\n    asyncAwait: async done =\u003e {\n      await new Promise(resolve =\u003e resolve())\n      done.resolve()\n    },\n  },\n  runs: 2,\n})\n```\n\nWill print:\n\n```\nnoop x 241,077 ops/sec ±48.87% (31 runs sampled)\nrandom x 280,523 ops/sec ±3.31% (33 runs sampled)\ntimeout x 768 ops/sec ±0.70% (79 runs sampled)\nimmediate x 59,573 ops/sec ±1.81% (76 runs sampled)\nasyncAwait x 6,749,279 ops/sec ±0.99% (81 runs sampled)\nFastest is asyncAwait\n```\n\nWill produce [runBench.json](./demo/runBench.json) (numbers are ops/sec, or Hertz):\n\n```json\n{\n  \"noop\": 239344,\n  \"random\": 285384,\n  \"timeout\": 775,\n  \"immediate\": 60214,\n  \"asyncAwait\": 6743787\n}\n```\n\nWill produce [runBench.svg](./demo/runBench.svg) plot:\n\n![runBench.svg](./demo/runBench.svg)\n\n# Show me the code 2 (autocannon)\n\n```typescript\nimport { runCannon, expressFunctionFactory } from '@naturalcycles/bench-lib'\nimport { _randomInt, pDelay } from '@naturalcycles/js-lib'\n\nrunCannon(\n  {\n    noop: expressFunctionFactory(() =\u003e 'yo'),\n    async: expressFunctionFactory(async () =\u003e await pDelay(0, 'yo')),\n    random: expressFunctionFactory(() =\u003e _randomInt(1, 10)),\n  },\n  {\n    runs: 2,\n    duration: 10,\n  },\n)\n```\n\nWill print:\n\n```\n┌─────────┬──────────┬─────────┬────────────┬───────────┬───────────┬───────────┬───────────────┬────────┬──────────┐\n│ (index) │   name   │ rpsAvg  │ latencyAvg │ latency50 │ latency90 │ latency99 │ throughputAvg │ errors │ timeouts │\n├─────────┼──────────┼─────────┼────────────┼───────────┼───────────┼───────────┼───────────────┼────────┼──────────┤\n│    0    │  'noop'  │ 31603.2 │    3.13    │     0     │     1     │    33     │     5.21      │   0    │    0     │\n│    1    │ 'async'  │ 26502.4 │    3.77    │     0     │    16     │    41     │     4.37      │   0    │    0     │\n│    2    │ 'random' │  32092  │    3.08    │     0     │     0     │    33     │     5.21      │   0    │    0     │\n└─────────┴──────────┴─────────┴────────────┴───────────┴───────────┴───────────┴───────────────┴────────┴──────────┘\n```\n\nWill produce [runCannon.summary.json](./demo/runCannon.json):\n\n```json\n[\n  {\n    \"name\": \"noop\",\n    \"rpsAvg\": 31603.2,\n    \"latencyAvg\": 3.13,\n    \"latency50\": 0,\n    \"latency90\": 1,\n    \"latency99\": 33,\n    \"throughputAvg\": 5.21,\n    \"errors\": 0,\n    \"timeouts\": 0\n  },\n  ...\n]\n```\n\nWill produce plots:\n\n![](./demo/runCannon.rpsAvg.svg) ![](./demo/runCannon.latencyAvg.svg)\n![](./demo/runCannon.latencyAvg.svg) ![](./demo/runCannon.latencyAvg.svg)\n![](./demo/runCannon.throughputAvg.svg)\n\n# How\n\nFundamental difference between Benchmark.js and Autocannon is that the former is doing **serial**\nexecution (one-after-another), while latter is calling requests **concurrently** (with concurrency\nas high as 100, by default). This results in \"no-op async function\" being executed ~700 times/second\nsequentially (needing to do await the \"tick\" for each Promise), and ~32K times/second (requests per\nsecond) while served from http server (concurrently).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnaturalcycles%2Fbench-lib","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnaturalcycles%2Fbench-lib","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnaturalcycles%2Fbench-lib/lists"}