Skip to content

Commit 46872c5

Browse files
perf: pass Device by value (#488)
Signed-off-by: Artur Melanchyk <13834276+arturmelanchyk@users.noreply.github.com> Co-authored-by: Artur Melanchyk <13834276+arturmelanchyk@users.noreply.github.com>
1 parent ba298bc commit 46872c5

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

lessons/215/go-app/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,5 @@ func (ms *MyServer) getHealth(w http.ResponseWriter, req *http.Request) {
5050
func (ms *MyServer) getDevices(w http.ResponseWriter, req *http.Request) {
5151
device := Device{Id: 1, Mac: "EF-2B-C4-F5-D6-34", Firmware: "2.1.5"}
5252

53-
renderJSON(w, &device, 200)
53+
renderJSON(w, device, 200)
5454
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
import (
4+
"net/http"
5+
"net/http/httptest"
6+
"testing"
7+
)
8+
9+
func BenchmarkGetDevicesParallel(b *testing.B) {
10+
ms := &MyServer{}
11+
b.ReportAllocs()
12+
13+
b.RunParallel(func(pb *testing.PB) {
14+
for pb.Next() {
15+
req := httptest.NewRequest("GET", "/api/devices", nil)
16+
rec := httptest.NewRecorder()
17+
18+
ms.getDevices(rec, req)
19+
20+
res := rec.Result()
21+
if res.StatusCode != http.StatusOK {
22+
b.Fatalf("expected status %d, got %d", http.StatusOK, res.StatusCode)
23+
}
24+
res.Body.Close()
25+
}
26+
})
27+
}

0 commit comments

Comments
 (0)