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/] [json_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 JSON encoding and decoding performance.
6
 
7
package go1
8
 
9
import (
10
        "compress/bzip2"
11
        "encoding/base64"
12
        "encoding/json"
13
        "io"
14
        "io/ioutil"
15
        "strings"
16
        "testing"
17
)
18
 
19
var (
20
        jsonbytes []byte
21
        jsondata  JSONResponse
22
)
23
 
24
func init() {
25
        var r io.Reader
26
        r = strings.NewReader(jsonbz2_base64)
27
        r = base64.NewDecoder(base64.StdEncoding, r)
28
        r = bzip2.NewReader(r)
29
        b, err := ioutil.ReadAll(r)
30
        if err != nil {
31
                panic(err)
32
        }
33
        jsonbytes = b
34
 
35
        if err := json.Unmarshal(jsonbytes, &jsondata); err != nil {
36
                panic(err)
37
        }
38
        gobinit()
39
}
40
 
41
type JSONResponse struct {
42
        Tree     *JSONNode `json:"tree"`
43
        Username string    `json:"username"`
44
}
45
 
46
type JSONNode struct {
47
        Name     string      `json:"name"`
48
        Kids     []*JSONNode `json:"kids"`
49
        CLWeight float64     `json:"cl_weight"`
50
        Touches  int         `json:"touches"`
51
        MinT     int64       `json:"min_t"`
52
        MaxT     int64       `json:"max_t"`
53
        MeanT    int64       `json:"mean_t"`
54
}
55
 
56
func jsondec() {
57
        var r JSONResponse
58
        if err := json.Unmarshal(jsonbytes, &r); err != nil {
59
                panic(err)
60
        }
61
        _ = r
62
}
63
 
64
func jsonenc() {
65
        buf, err := json.Marshal(&jsondata)
66
        if err != nil {
67
                panic(err)
68
        }
69
        _ = buf
70
}
71
 
72
func BenchmarkJSONEncode(b *testing.B) {
73
        b.SetBytes(int64(len(jsonbytes)))
74
        for i := 0; i < b.N; i++ {
75
                jsonenc()
76
        }
77
}
78
 
79
func BenchmarkJSONDecode(b *testing.B) {
80
        b.SetBytes(int64(len(jsonbytes)))
81
        for i := 0; i < b.N; i++ {
82
                jsondec()
83
        }
84
}

powered by: WebSVN 2.1.0

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