8000 proposal: net/http/httptest: ResponseRecorder.Reset · Issue #73544 · golang/go · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

proposal: net/http/httptest: ResponseRecorder.Reset #73544

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
chmike opened this issue Apr 30, 2025 · 0 comments
Open

proposal: net/http/httptest: ResponseRecorder.Reset #73544

chmike opened this issue Apr 30, 2025 · 0 comments
Labels
LibraryProposal Issues describing a requested change to the Go standard library or x/ libraries, but not to a tool Proposal
Milestone

Comments

@chmike
Copy link
chmike commented Apr 30, 2025

Proposal Details

While benchmarking my ServeHTTP function I noticed that httptest.NewRecorded took a significant time and does 3 allocations.

req, _ := http.NewRequest("GET", "/public/file", nil)
b.ResetTimer()
for b.Loop() {
    rsp := httptest.NewRecorder()
    myServeHTTP(rsp, req)
}

I removed the httptest.NewRecorder() overhead by using the following code

req, _ := http.NewRequest("GET", "/public/file", nil)
rsp := httptest.NewRecorder()
b.ResetTimer()
for b.Loop() {
    rsp.Body().Reset()
    rsp.Code = http.StatusOK
    clear(rsp.HeaderMap)
    myServeHTTP(rsp, req)
}

But I get a warning that direct access to HeaderMap is deprecated. I don’t know if my reset is correct. Apparently, it works.

Benchmarking httptest.NewRecorder() yields 172ns/op 160 B/op 3 allocs/op. The Reset method I use yields 2.95ns/op, 0 B/op and 0 allocs/op.

A Reset method could significantly reduce the overhead of the recorder instantiation and make it straightforward how to reset the recorder.

@gopherbot gopherbot added this to the Proposal milestone Apr 30, 2025
@seankhliao seankhliao changed the title proposal: import/path: add a Reset method to httptest.ResponseRecorder proposal: net/http/httptest: ResponseRecorder.Reset Apr 30, 2025
@gabyhelp gabyhelp added the LibraryProposal Issues describing a requested change to the Go standard library or x/ libraries, but not to a tool label Apr 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
LibraryProposal Issues describing a requested change to the Go standard library or x/ libraries, but not to a tool Proposal
Projects
None yet
Development

No branches or pull requests

3 participants
0