Skip to content

Instantly share code, notes, and snippets.

@bejaneps
Created June 17, 2020 13:00
Show Gist options
  • Save bejaneps/6467c9687125cbce52e642da1cf209b1 to your computer and use it in GitHub Desktop.
Save bejaneps/6467c9687125cbce52e642da1cf209b1 to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"testing"
)
type Response struct {
Message string
}
func BenchmarkHelloHandlerVariable(b *testing.B) {
b.ResetTimer()
var writer = ioutil.Discard
response := Response{Message: "Hello World"}
for i := 0; i < b.N; i++ {
data, _ := json.Marshal(response)
fmt.Fprint(writer, string(data))
}
}
func BenchmarkHelloHandlerEncoder(b *testing.B) {
b.ResetTimer()
var writer = ioutil.Discard
response := Response{Message: "Hello World"}
for i := 0; i < b.N; i++ {
encoder := json.NewEncoder(writer)
encoder.Encode(response)
}
}
func BenchmarkHelloHandlerEncoderReference(b *testing.B) {
b.ResetTimer()
var writer = ioutil.Discard
response := Response{Message: "Hello World"}
for i := 0; i < b.N; i++ {
encoder := json.NewEncoder(writer)
encoder.Encode(&response)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment