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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [gcc/] [testsuite/] [go.test/] [test/] [bench/] [go1/] [gob_test.go] - Blame information for rev 700

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 700 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
// This benchmark tests gob encoding and decoding performance.
6
 
7
package go1
8
 
9
import (
10
        "bytes"
11
        "encoding/gob"
12
        "encoding/json"
13
        "io/ioutil"
14
        "log"
15
        "reflect"
16
        "testing"
17
)
18
 
19
var (
20
        gobbytes []byte
21
        gobdata  *JSONResponse
22
)
23
 
24
func gobinit() {
25
        // gobinit is called after json's init,
26
        // because it uses jsondata.
27
        gobdata = gobResponse(&jsondata)
28
 
29
        var buf bytes.Buffer
30
        if err := gob.NewEncoder(&buf).Encode(gobdata); err != nil {
31
                panic(err)
32
        }
33
        gobbytes = buf.Bytes()
34
 
35
        var r JSONResponse
36
        if err := gob.NewDecoder(bytes.NewBuffer(gobbytes)).Decode(&r); err != nil {
37
                panic(err)
38
        }
39
        if !reflect.DeepEqual(gobdata, &r) {
40
                log.Printf("%v\n%v", jsondata, r)
41
                b, _ := json.Marshal(&jsondata)
42
                br, _ := json.Marshal(&r)
43
                log.Printf("%s\n%s\n", b, br)
44
                panic("gob: encode+decode lost data")
45
        }
46
}
47
 
48
// gob turns [] into null, so make a copy of the data structure like that
49
func gobResponse(r *JSONResponse) *JSONResponse {
50
        return &JSONResponse{gobNode(r.Tree), r.Username}
51
}
52
 
53
func gobNode(n *JSONNode) *JSONNode {
54
        n1 := new(JSONNode)
55
        *n1 = *n
56
        if len(n1.Kids) == 0 {
57
                n1.Kids = nil
58
        } else {
59
                for i, k := range n1.Kids {
60
                        n1.Kids[i] = gobNode(k)
61
                }
62
        }
63
        return n1
64
}
65
 
66
func gobdec() {
67
        if gobbytes == nil {
68
                panic("gobdata not initialized")
69
        }
70
        var r JSONResponse
71
        if err := gob.NewDecoder(bytes.NewBuffer(gobbytes)).Decode(&r); err != nil {
72
                panic(err)
73
        }
74
        _ = r
75
}
76
 
77
func gobenc() {
78
        if err := gob.NewEncoder(ioutil.Discard).Encode(&gobdata); err != nil {
79
                panic(err)
80
        }
81
}
82
 
83
func BenchmarkGobDecode(b *testing.B) {
84
        b.SetBytes(int64(len(gobbytes)))
85
        for i := 0; i < b.N; i++ {
86
                gobdec()
87
        }
88
}
89
 
90
func BenchmarkGobEncode(b *testing.B) {
91
        b.SetBytes(int64(len(gobbytes)))
92
        for i := 0; i < b.N; i++ {
93
                gobenc()
94
        }
95
}

powered by: WebSVN 2.1.0

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