URL
https://opencores.org/ocsvn/openrisc/openrisc/trunk
Subversion Repositories openrisc
[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [encoding/] [gob/] [timing_test.go] - Rev 747
Compare with Previous | Blame | View Log
// Copyright 2011 The Go Authors. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.package gobimport ("bytes""fmt""io""os""runtime""testing")type Bench struct {A intB float64C stringD []byte}func benchmarkEndToEnd(r io.Reader, w io.Writer, b *testing.B) {b.StopTimer()enc := NewEncoder(w)dec := NewDecoder(r)bench := &Bench{7, 3.2, "now is the time", []byte("for all good men")}b.StartTimer()for i := 0; i < b.N; i++ {if enc.Encode(bench) != nil {panic("encode error")}if dec.Decode(bench) != nil {panic("decode error")}}}func BenchmarkEndToEndPipe(b *testing.B) {r, w, err := os.Pipe()if err != nil {b.Fatal("can't get pipe:", err)}benchmarkEndToEnd(r, w, b)}func BenchmarkEndToEndByteBuffer(b *testing.B) {var buf bytes.BufferbenchmarkEndToEnd(&buf, &buf, b)}func TestCountEncodeMallocs(t *testing.T) {var buf bytes.Bufferenc := NewEncoder(&buf)bench := &Bench{7, 3.2, "now is the time", []byte("for all good men")}memstats := new(runtime.MemStats)runtime.ReadMemStats(memstats)mallocs := 0 - memstats.Mallocsconst count = 1000for i := 0; i < count; i++ {err := enc.Encode(bench)if err != nil {t.Fatal("encode:", err)}}runtime.ReadMemStats(memstats)mallocs += memstats.Mallocsfmt.Printf("mallocs per encode of type Bench: %d\n", mallocs/count)}func TestCountDecodeMallocs(t *testing.T) {var buf bytes.Bufferenc := NewEncoder(&buf)bench := &Bench{7, 3.2, "now is the time", []byte("for all good men")}const count = 1000for i := 0; i < count; i++ {err := enc.Encode(bench)if err != nil {t.Fatal("encode:", err)}}dec := NewDecoder(&buf)memstats := new(runtime.MemStats)runtime.ReadMemStats(memstats)mallocs := 0 - memstats.Mallocsfor i := 0; i < count; i++ {*bench = Bench{}err := dec.Decode(&bench)if err != nil {t.Fatal("decode:", err)}}runtime.ReadMemStats(memstats)mallocs += memstats.Mallocsfmt.Printf("mallocs per decode of type Bench: %d\n", mallocs/count)}
