| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package codec |
| 8 |
|
| 9 |
import ( |
| 10 |
"bytes" |
| 11 |
"encoding/gob" |
| 12 |
"encoding/json" |
| 13 |
"encoding/xml" |
| 14 |
"testing" |
| 15 |
) |
| 16 |
|
| 17 |
func init() { |
| 18 |
testPreInitFns = append(testPreInitFns, stdlibBenchPreInit) |
| 19 |
|
| 20 |
} |
| 21 |
|
| 22 |
func stdlibBenchPreInit() { |
| 23 |
benchCheckers = append(benchCheckers, |
| 24 |
benchChecker{"std-json", fnStdJsonEncodeFn, fnStdJsonDecodeFn}, |
| 25 |
benchChecker{"gob", fnGobEncodeFn, fnGobDecodeFn}, |
| 26 |
benchChecker{"std-xml", fnStdXmlEncodeFn, fnStdXmlDecodeFn}, |
| 27 |
) |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
func fnGobEncodeFn(ts interface{}, bsIn []byte) ([]byte, error) { |
| 33 |
buf := fnBenchmarkByteBuf(bsIn) |
| 34 |
err := gob.NewEncoder(buf).Encode(ts) |
| 35 |
return buf.Bytes(), err |
| 36 |
} |
| 37 |
|
| 38 |
func fnGobDecodeFn(buf []byte, ts interface{}) error { |
| 39 |
return gob.NewDecoder(bytes.NewReader(buf)).Decode(ts) |
| 40 |
} |
| 41 |
|
| 42 |
func fnStdXmlEncodeFn(ts interface{}, bsIn []byte) ([]byte, error) { |
| 43 |
buf := fnBenchmarkByteBuf(bsIn) |
| 44 |
err := xml.NewEncoder(buf).Encode(ts) |
| 45 |
return buf.Bytes(), err |
| 46 |
} |
| 47 |
|
| 48 |
func fnStdXmlDecodeFn(buf []byte, ts interface{}) error { |
| 49 |
return xml.NewDecoder(bytes.NewReader(buf)).Decode(ts) |
| 50 |
} |
| 51 |
|
| 52 |
func fnStdJsonEncodeFn(ts interface{}, bsIn []byte) ([]byte, error) { |
| 53 |
if testUseIoEncDec >= 0 { |
| 54 |
buf := fnBenchmarkByteBuf(bsIn) |
| 55 |
err := json.NewEncoder(buf).Encode(ts) |
| 56 |
return buf.Bytes(), err |
| 57 |
} |
| 58 |
return json.Marshal(ts) |
| 59 |
} |
| 60 |
|
| 61 |
func fnStdJsonDecodeFn(buf []byte, ts interface{}) error { |
| 62 |
if testUseIoEncDec >= 0 { |
| 63 |
return json.NewDecoder(bytes.NewReader(buf)).Decode(ts) |
| 64 |
} |
| 65 |
return json.Unmarshal(buf, ts) |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
func Benchmark__Std_Json___Encode(b *testing.B) { |
| 71 |
fnBenchmarkEncode(b, "std-json", benchTs, fnStdJsonEncodeFn) |
| 72 |
} |
| 73 |
|
| 74 |
func Benchmark__Gob________Encode(b *testing.B) { |
| 75 |
fnBenchmarkEncode(b, "gob", benchTs, fnGobEncodeFn) |
| 76 |
} |
| 77 |
|
| 78 |
func Benchmark__Std_Xml____Encode(b *testing.B) { |
| 79 |
fnBenchmarkEncode(b, "std-xml", benchTs, fnStdXmlEncodeFn) |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
func Benchmark__Std_Json___Decode(b *testing.B) { |
| 85 |
fnBenchmarkDecode(b, "std-json", benchTs, fnStdJsonEncodeFn, fnStdJsonDecodeFn, fnBenchNewTs) |
| 86 |
} |
| 87 |
|
| 88 |
func Benchmark__Gob________Decode(b *testing.B) { |
| 89 |
fnBenchmarkDecode(b, "gob", benchTs, fnGobEncodeFn, fnGobDecodeFn, fnBenchNewTs) |
| 90 |
} |
| 91 |
|
| 92 |
func Benchmark__Std_Xml____Decode(b *testing.B) { |
| 93 |
fnBenchmarkDecode(b, "std-xml", benchTs, fnStdXmlEncodeFn, fnStdXmlDecodeFn, fnBenchNewTs) |
| 94 |
} |