Skip to content

🧹chore: benchmark for cache miss case#3836

Merged
ReneWerner87 merged 3 commits intogofiber:mainfrom
arturmelanchyk:handler
Nov 3, 2025
Merged

🧹chore: benchmark for cache miss case#3836
ReneWerner87 merged 3 commits intogofiber:mainfrom
arturmelanchyk:handler

Conversation

@arturmelanchyk
Copy link
Contributor

Description

Existing benchmarks for cache middlware seems to not cover case with cache miss, when the new entry needs to be generated

Changes introduced

List the new features or adjustments introduced in this pull request. Provide details on benchmarks, documentation updates, changelog entries, and if applicable, the migration guide.

  • Benchmarks: Describe any performance benchmarks and improvements related to the changes.
  • Documentation Update: Detail the updates made to the documentation and links to the changed files.
  • Changelog/What's New: Include a summary of the additions for the upcoming release notes.
  • Migration Guide: If necessary, provide a guide or steps for users to migrate their existing code to accommodate these changes.
  • API Alignment with Express: Explain how the changes align with the Express API.
  • API Longevity: Discuss the steps taken to ensure that the new or updated APIs are consistent and not prone to breaking changes.
  • Examples: Provide examples demonstrating the new features or changes in action.

Type of change

Please delete options that are not relevant.

  • New feature (non-breaking change which adds functionality)
  • Enhancement (improvement to existing features and functionality)
  • Documentation update (changes to documentation)
  • Performance improvement (non-breaking change which improves efficiency)
  • Code consistency (non-breaking change which improves code reliability and robustness)

Checklist

Before you submit your pull request, please make sure you meet these requirements:

  • Followed the inspiration of the Express.js framework for new functionalities, making them similar in usage.
  • Conducted a self-review of the code and provided comments for complex or critical parts.
  • Updated the documentation in the /docs/ directory for Fiber's documentation.
  • Added or updated unit tests to validate the effectiveness of the changes or new features.
  • Ensured that new and existing unit tests pass locally with the changes.
  • Verified that any new dependencies are essential and have been agreed upon by the maintainers/community.
  • Aimed for optimal performance with minimal allocations in the new code.
  • Provided benchmarks for the new code to analyze and improve upon.

Commit formatting

Please use emojis in commit messages for an easy way to identify the purpose or intention of a commit. Check out the emoji cheatsheet here: CONTRIBUTING.md

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 2, 2025

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

Walkthrough

A new benchmark function Benchmark_Cache_Miss was added to middleware/cache/cache_test.go to measure cache miss performance; the function was inserted twice, resulting in two identical definitions.

Changes

Cohort / File(s) Summary
Benchmark additions
middleware/cache/cache_test.go
Added benchmark function Benchmark_Cache_Miss to measure cache miss performance; duplicate identical definitions were introduced in the patch

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Verify whether the duplicated Benchmark_Cache_Miss is intentional or a merge/rebase artifact.
  • Confirm the benchmark matches structure and conventions of existing Benchmark_Cache.
  • Run benchmarks to ensure they compile and produce valid metrics.

Suggested reviewers

  • gaby
  • sixcolors
  • efectn
  • ReneWerner87

Poem

🐰 I hop through tests with timing cheer,
Measuring misses far and near,
Two little copies pause and stare,
Benchmarks ready — double the care!
hop hop ⏱️

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Description Check ⚠️ Warning The pull request description includes most of the required template sections with relevant content: the Description section explains the problem (cache-miss scenarios not being benchmarked), and the Changes introduced section appropriately checks only the Benchmarks checkbox as the relevant item. However, the "Type of change" section contains no selected options despite the template explicitly instructing authors to mark which type of change applies (leaving only relevant options). This is a structural requirement of the template, and leaving all options unchecked represents incomplete compliance with the template format.
✅ Passed checks (1 passed)
Check name Status Explanation
Title Check ✅ Passed The pull request title "🧹chore: benchmark for cache miss case" directly relates to the main change in the changeset, which is adding new benchmarks for cache-miss scenarios in the cache middleware tests. The title is concise, specific, and uses clear terminology that immediately conveys the purpose of the change. While it includes an emoji, the repository's CONTRIBUTING.md guidelines (referenced in the template) explicitly encourage emoji usage in commit messages. The title accurately summarizes the primary change without being vague or generic.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7ab1b2d and 2244114.

📒 Files selected for processing (1)
  • middleware/cache/cache_test.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • middleware/cache/cache_test.go
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: repeated
  • GitHub Check: Compare

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @arturmelanchyk, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the testing suite for the cache middleware by adding a dedicated benchmark for cache miss scenarios. This new benchmark will help in accurately measuring the performance overhead when the cache needs to generate and store new data, ensuring a more comprehensive understanding of the middleware's efficiency under various conditions.

Highlights

  • New Benchmark Added: A new benchmark, Benchmark_Cache_Miss, has been introduced to specifically test the performance of the cache middleware when a cache miss occurs and a new entry needs to be generated.
  • Improved Test Coverage: This addition addresses a gap in existing benchmarks, which previously did not cover scenarios where the cache middleware has to generate and store a new entry due to a miss.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@arturmelanchyk arturmelanchyk changed the title chore: benchmark cache miss chore: benchmark for cache miss case Nov 2, 2025
@arturmelanchyk arturmelanchyk changed the title chore: benchmark for cache miss case 🧹chore: benchmark for cache miss case Nov 2, 2025
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds a benchmark for cache misses in the cache middleware. However, the current implementation of Benchmark_Cache_Miss is flawed, as it primarily measures cache hits instead of misses after the first iteration. I've provided a review comment with a suggested fix to ensure that each request in the benchmark loop results in a cache miss, providing an accurate measurement. This change will make the new benchmark correctly fulfill its intended purpose.

@codecov
Copy link

codecov bot commented Nov 2, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.19%. Comparing base (0be33db) to head (2244114).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3836      +/-   ##
==========================================
- Coverage   92.26%   92.19%   -0.07%     
==========================================
  Files         115      115              
  Lines        9742     9742              
==========================================
- Hits         8988     8982       -6     
- Misses        480      485       +5     
- Partials      274      275       +1     
Flag Coverage Δ
unittests 92.19% <ø> (-0.07%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@arturmelanchyk arturmelanchyk marked this pull request as draft November 2, 2025 19:19
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 11be17d and 9c5c0e0.

📒 Files selected for processing (1)
  • middleware/cache/cache_test.go (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.go

📄 CodeRabbit inference engine (AGENTS.md)

**/*.go: Apply formatting using gofumpt (Make target: format)
Optimize struct field alignment using betteralign (Make target: betteralign)
Modernize code using gopls modernize (Make target: modernize)

Files:

  • middleware/cache/cache_test.go
🧬 Code graph analysis (1)
middleware/cache/cache_test.go (3)
internal/storage/memory/memory.go (1)
  • New (29-45)
app.go (2)
  • New (521-634)
  • Handler (40-40)
middleware/cache/cache.go (1)
  • New (70-374)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
  • GitHub Check: unit (1.25.x, windows-latest)
  • GitHub Check: unit (1.25.x, macos-13)
  • GitHub Check: repeated
  • GitHub Check: Compare

Signed-off-by: Artur Melanchyk <13834276+arturmelanchyk@users.noreply.github.com>
@arturmelanchyk arturmelanchyk marked this pull request as ready for review November 2, 2025 19:43
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
middleware/cache/cache_test.go (1)

1628-1653: Consider using for b.Loop() for consistency with other benchmarks.

The cache miss logic is correctly implemented—varying the URI ensures each iteration misses the cache. However, all other benchmarks in this file use the newer for b.Loop() pattern (lines 1620, 1676, 1703, 1734), while this benchmark uses the traditional for n := 0; n < b.N; n++ style.

Apply this diff for consistency:

 func Benchmark_Cache_Miss(b *testing.B) {
 	app := fiber.New()
 
 	app.Use(New())
 
 	app.Get("/*", func(c fiber.Ctx) error {
 		data, _ := os.ReadFile("../../.github/README.md") //nolint:errcheck // We're inside a benchmark
 		return c.Status(fiber.StatusOK).Send(data)
 	})
 
 	h := app.Handler()
 
 	fctx := &fasthttp.RequestCtx{}
 	fctx.Request.Header.SetMethod(fiber.MethodGet)
 
 	b.ReportAllocs()
 	b.ResetTimer()
 
-	for n := 0; n < b.N; n++ {
+	var n int
+	for b.Loop() {
+		n++
 		fctx.Request.SetRequestURI("/demo/" + strconv.Itoa(n))
 		h(fctx)
 	}
 
 	require.Equal(b, fiber.StatusOK, fctx.Response.Header.StatusCode())
 	require.Greater(b, len(fctx.Response.Body()), 30000)
 }
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9c5c0e0 and 3441187.

📒 Files selected for processing (1)
  • middleware/cache/cache_test.go (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.go

📄 CodeRabbit inference engine (AGENTS.md)

**/*.go: Apply formatting using gofumpt (Make target: format)
Optimize struct field alignment using betteralign (Make target: betteralign)
Modernize code using gopls modernize (Make target: modernize)

Files:

  • middleware/cache/cache_test.go
🧠 Learnings (1)
📚 Learning: 2024-11-10T23:44:13.704Z
Learnt from: gaby
Repo: gofiber/fiber PR: 3193
File: middleware/adaptor/adaptor.go:111-111
Timestamp: 2024-11-10T23:44:13.704Z
Learning: In the `middleware/adaptor/adaptor.go` file of the Fiber framework, when updating context handling, replacing `c.Context()` with `c.RequestCtx()` is appropriate to access the `fasthttp.RequestCtx`.

Applied to files:

  • middleware/cache/cache_test.go
🧬 Code graph analysis (1)
middleware/cache/cache_test.go (2)
internal/storage/memory/memory.go (1)
  • New (29-45)
middleware/cache/cache.go (1)
  • New (70-374)

@ReneWerner87
Copy link
Member

@arturmelanchyk thx, can you check the hint from the AI and my comment

Signed-off-by: Artur Melanchyk <13834276+arturmelanchyk@users.noreply.github.com>
@arturmelanchyk
Copy link
Contributor Author

@ReneWerner87
changed,
my idea was that having one line is more concise than having b.Loop with 3 lines

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
middleware/cache/cache_test.go (1)

1628-1655: LGTM! Correctly implements cache miss benchmark.

The implementation properly addresses previous review feedback by varying the request URI on each iteration (lines 1648-1649), ensuring every call results in a cache miss. The use of b.ResetTimer() after setup is correct, and the wildcard route /* appropriately matches all unique paths.

Optional consistency improvements:

  1. Consider adding a benchmark invocation comment for consistency with other benchmarks:
+// go test -v -run=^$ -bench=Benchmark_Cache_Miss -benchmem -count=4
 func Benchmark_Cache_Miss(b *testing.B) {
  1. For easier performance comparison with Benchmark_Cache, consider using the same status code:
-		return c.Status(fiber.StatusOK).Send(data)
+		return c.Status(fiber.StatusTeapot).Send(data)

And update the assertion:

-	require.Equal(b, fiber.StatusOK, fctx.Response.Header.StatusCode())
+	require.Equal(b, fiber.StatusTeapot, fctx.Response.Header.StatusCode())
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3441187 and 7ab1b2d.

📒 Files selected for processing (1)
  • middleware/cache/cache_test.go (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.go

📄 CodeRabbit inference engine (AGENTS.md)

**/*.go: Apply formatting using gofumpt (Make target: format)
Optimize struct field alignment using betteralign (Make target: betteralign)
Modernize code using gopls modernize (Make target: modernize)

Files:

  • middleware/cache/cache_test.go
🧠 Learnings (2)
📚 Learning: 2024-11-10T23:44:13.704Z
Learnt from: gaby
Repo: gofiber/fiber PR: 3193
File: middleware/adaptor/adaptor.go:111-111
Timestamp: 2024-11-10T23:44:13.704Z
Learning: In the `middleware/adaptor/adaptor.go` file of the Fiber framework, when updating context handling, replacing `c.Context()` with `c.RequestCtx()` is appropriate to access the `fasthttp.RequestCtx`.

Applied to files:

  • middleware/cache/cache_test.go
📚 Learning: 2025-09-18T00:32:59.671Z
Learnt from: sixcolors
Repo: gofiber/fiber PR: 0
File: :0-0
Timestamp: 2025-09-18T00:32:59.671Z
Learning: In Go 1.22, manual copying of loop variables (like `x := x`) is no longer necessary due to the fix for variable capture in closures. Each loop iteration now creates its own instance of the loop variable automatically.

Applied to files:

  • middleware/cache/cache_test.go
🧬 Code graph analysis (1)
middleware/cache/cache_test.go (1)
middleware/cache/cache.go (1)
  • New (70-374)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (6)
  • GitHub Check: lint
  • GitHub Check: repeated
  • GitHub Check: unit (1.25.x, macos-13)
  • GitHub Check: unit (1.25.x, windows-latest)
  • GitHub Check: unit (1.25.x, macos-latest)
  • GitHub Check: Compare

@ReneWerner87 ReneWerner87 added this to v3 Nov 3, 2025
@ReneWerner87 ReneWerner87 added this to the v3 milestone Nov 3, 2025
@arturmelanchyk
Copy link
Contributor Author

coderabbit's comment regarding fiber.StatusTeapot is irrelevant because this status code gets cached, with fiber.StatusOk we get cache miss which is desired behaviour

@ReneWerner87 ReneWerner87 merged commit 0edd81d into gofiber:main Nov 3, 2025
16 checks passed
@github-project-automation github-project-automation bot moved this to Done in v3 Nov 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants