Answer the question
In order to leave comments, you need to log in
Is it possible to benchmark functions from packages in golang?
Hello. There is a models file with the following code:
package api
import "encoding/json"
type Awesome struct {
Id string `json:"id"`
Message string `json:"message"`
Score float64 `json:"score"`
Confirmed bool `json:"confirmed"`
}
func NewAwesome(id string, message string, score float64, confirmed bool) *Awesome {
return &Awesome{
id,
message,
score,
confirmed,
}
}
func(a *Awesome) ToJSON() ([]byte, error) {
return json.Marshal(a)
}
package api
import "testing"
func BenchmarkAwesomeToJSON(b *testing.B) {
for i := 0; i < b.N; i++ {
awesome := NewAwesome("123456789", "Total awesomeness", 9.99, true)
awesome.ToJSON()
}
}
Answer the question
In order to leave comments, you need to log in
Of course you can, but you specify only one file in the project, run it through go test -bench .
and it will work.
$ go test -bench .
goos: darwin
goarch: amd64
pkg: api
BenchmarkAwesomeToJSON-8 3000000 470 ns/op
PASS
ok api 1.895s
go test -bench . test_test.go test.go
, but it's easier without them. func BenchmarkAwesomeToJSON(b *testing.B) {
awesome := NewAwesome("123456789", "Total awesomeness", 9.99, true)
b.ResetTimer()
for i := 0; i < b.N; i++ {
awesome.ToJSON()
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question