OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [encoding/] [gob/] [timing_test.go] - Blame information for rev 747

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// Copyright 2011 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
4
 
5
package gob
6
 
7
import (
8
        "bytes"
9
        "fmt"
10
        "io"
11
        "os"
12
        "runtime"
13
        "testing"
14
)
15
 
16
type Bench struct {
17
        A int
18
        B float64
19
        C string
20
        D []byte
21
}
22
 
23
func benchmarkEndToEnd(r io.Reader, w io.Writer, b *testing.B) {
24
        b.StopTimer()
25
        enc := NewEncoder(w)
26
        dec := NewDecoder(r)
27
        bench := &Bench{7, 3.2, "now is the time", []byte("for all good men")}
28
        b.StartTimer()
29
        for i := 0; i < b.N; i++ {
30
                if enc.Encode(bench) != nil {
31
                        panic("encode error")
32
                }
33
                if dec.Decode(bench) != nil {
34
                        panic("decode error")
35
                }
36
        }
37
}
38
 
39
func BenchmarkEndToEndPipe(b *testing.B) {
40
        r, w, err := os.Pipe()
41
        if err != nil {
42
                b.Fatal("can't get pipe:", err)
43
        }
44
        benchmarkEndToEnd(r, w, b)
45
}
46
 
47
func BenchmarkEndToEndByteBuffer(b *testing.B) {
48
        var buf bytes.Buffer
49
        benchmarkEndToEnd(&buf, &buf, b)
50
}
51
 
52
func TestCountEncodeMallocs(t *testing.T) {
53
        var buf bytes.Buffer
54
        enc := NewEncoder(&buf)
55
        bench := &Bench{7, 3.2, "now is the time", []byte("for all good men")}
56
        memstats := new(runtime.MemStats)
57
        runtime.ReadMemStats(memstats)
58
        mallocs := 0 - memstats.Mallocs
59
        const count = 1000
60
        for i := 0; i < count; i++ {
61
                err := enc.Encode(bench)
62
                if err != nil {
63
                        t.Fatal("encode:", err)
64
                }
65
        }
66
        runtime.ReadMemStats(memstats)
67
        mallocs += memstats.Mallocs
68
        fmt.Printf("mallocs per encode of type Bench: %d\n", mallocs/count)
69
}
70
 
71
func TestCountDecodeMallocs(t *testing.T) {
72
        var buf bytes.Buffer
73
        enc := NewEncoder(&buf)
74
        bench := &Bench{7, 3.2, "now is the time", []byte("for all good men")}
75
        const count = 1000
76
        for i := 0; i < count; i++ {
77
                err := enc.Encode(bench)
78
                if err != nil {
79
                        t.Fatal("encode:", err)
80
                }
81
        }
82
        dec := NewDecoder(&buf)
83
        memstats := new(runtime.MemStats)
84
        runtime.ReadMemStats(memstats)
85
        mallocs := 0 - memstats.Mallocs
86
        for i := 0; i < count; i++ {
87
                *bench = Bench{}
88
                err := dec.Decode(&bench)
89
                if err != nil {
90
                        t.Fatal("decode:", err)
91
                }
92
        }
93
        runtime.ReadMemStats(memstats)
94
        mallocs += memstats.Mallocs
95
        fmt.Printf("mallocs per decode of type Bench: %d\n", mallocs/count)
96
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.