Skip to content

Instantly share code, notes, and snippets.

@elvuel
Created May 5, 2014 08:27
Show Gist options
  • Save elvuel/cbf309511056112b6abd to your computer and use it in GitHub Desktop.
Save elvuel/cbf309511056112b6abd to your computer and use it in GitHub Desktop.
Benchmark reflect json(KIND:MAP)
package main
import (
"encoding/json"
"reflect"
"testing"
)
func BenchmarkMapReflection(b *testing.B) {
jsonStr := `{"uuid":"abcdefghijklmnopqrstuvwxyz0123456789","pm25":65.7,"temperature":34.6,"humidity":40,"pa":125,"timestamp":13900000000,"status":0}`
for i := 0; i < b.N; i++ {
var payload interface{}
json.Unmarshal([]byte(jsonStr), &payload)
//t := reflect.TypeOf(&payload)
//t.Kind() == reflect.Map
//fmt.Println(t.String())
v := reflect.ValueOf(payload)
v.SetMapIndex(reflect.ValueOf("model"), reflect.ValueOf("pdr1500"))
//fmt.Printf("%#v", v.MapKeys())
for _, k := range v.MapKeys() {
//fmt.Printf("%s", k.Type().Name())
//fmt.Printf("%s:", k.String())
v.MapIndex(k)
//v1 := reflect.ValueOf(kv)
//fmt.Printf("%#v\n", kv.Interface())
//fmt.Printf("%#t", v.MapIndex(k))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment