Last active
July 19, 2019 22:10
-
-
Save krishbhanushali/f715cfac6ee9717ce323ca7777c466e1 to your computer and use it in GitHub Desktop.
This is a gist about benchmarking Go Marshaling, Unmarshaling, Encoding and Decoding
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" | |
"os" | |
"strings" | |
"testing" | |
) | |
type TB testing.B | |
func BenchmarkMarshaling(b *testing.B) { | |
b.StopTimer() | |
type Employee struct { | |
EmployeeID int `json:"employee_id"` | |
EmployeeName string `json:"employee_name"` | |
EmployeeEmailAddress string `json:"employee_email_address"` | |
} | |
employees := []Employee{ | |
Employee{ | |
EmployeeID: 1, | |
EmployeeName: "XYZ", | |
EmployeeEmailAddress: "[email protected]", | |
}, | |
Employee{ | |
EmployeeID: 2, | |
EmployeeName: "ABC", | |
EmployeeEmailAddress: "[email protected]", | |
}, | |
} | |
b.StartTimer() | |
encoder := json.NewEncoder(os.Stdout) | |
encoder.Encode(employees) | |
} | |
func BenchmarkUnmarshaling(b *testing.B) { | |
b.StopTimer() | |
jsonData := []byte(`[{"EmployeeID":1,"EmployeeName":"XYZ","EmployeeEmailAddress":"[email protected]"},{"EmployeeID":2,"EmployeeName":"ABC","EmployeeEmailAddress":"[email protected]"}]`) | |
type Employee struct { | |
EmployeeID int | |
EmployeeName string | |
EmployeeEmailAddress string | |
} | |
var employees []Employee | |
b.StartTimer() | |
err := json.Unmarshal(jsonData, &employees) | |
if err != nil { | |
fmt.Println("error: ", err) | |
} | |
//fmt.Printf("%+v", employees) | |
} | |
func BenchmarkEncoder(b *testing.B) { | |
b.StopTimer() | |
type Employee struct { | |
EmployeeID int `json:"employee_id"` | |
EmployeeName string `json:"employee_name"` | |
EmployeeEmailAddress string `json:"employee_email_address"` | |
} | |
employees := []Employee{ | |
Employee{ | |
EmployeeID: 1, | |
EmployeeName: "XYZ", | |
EmployeeEmailAddress: "[email protected]", | |
}, | |
Employee{ | |
EmployeeID: 2, | |
EmployeeName: "ABC", | |
EmployeeEmailAddress: "[email protected]", | |
}, | |
} | |
b.StartTimer() | |
encoder := json.NewEncoder(os.Stdout) | |
encoder.Encode(employees) | |
} | |
func BenchmarkDecoder(b *testing.B) { | |
b.StopTimer() | |
jsonData := `[{"EmployeeID":1,"EmployeeName":"XYZ","EmployeeEmailAddress":"[email protected]"},{"EmployeeID":2,"EmployeeName":"ABC","EmployeeEmailAddress":"[email protected]"}]` | |
type Employee struct { | |
EmployeeID int | |
EmployeeName string | |
EmployeeEmailAddress string | |
} | |
var employees []Employee | |
b.StartTimer() | |
decoder := json.NewDecoder(strings.NewReader(jsonData)) | |
if err := decoder.Decode(&employees); err != nil { | |
fmt.Println("error:", err) | |
} | |
//fmt.Printf("%v", employees) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment