Created
June 17, 2020 13:00
-
-
Save bejaneps/6467c9687125cbce52e642da1cf209b1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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