/
h1d_test.go
204 lines (175 loc) · 3.85 KB
/
h1d_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright 2015 The go-hep 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 hbook_test
import (
"bytes"
"encoding/gob"
"reflect"
"sync"
"testing"
"github.com/go-hep/hbook"
"github.com/gonum/plot/plotter"
)
func TestH1D(t *testing.T) {
h1 := hbook.NewH1D(100, 0., 100.)
if h1 == nil {
t.Errorf("nil pointer to H1D")
}
h1.Annotation()["name"] = "h1"
n := h1.Name()
if n != "h1" {
t.Errorf("expected H1D.Name() == %q (got %q)\n",
n, "h1")
}
nbins := h1.Axis().Bins()
if nbins != 100 {
t.Errorf("expected H1D.Axis.Bins() == %v (got %v)\n",
100, nbins,
)
}
low := h1.Axis().LowerEdge()
if low != 0. {
t.Errorf("expected H1D.Axis.LowerEdge() == %v (got %v)\n",
0., low,
)
}
up := h1.Axis().UpperEdge()
if up != 100. {
t.Errorf("expected H1D.Axis.UpperEdge() == %v (got %v)\n",
100., up,
)
}
for idx := 0; idx < nbins; idx++ {
size := h1.Axis().BinWidth(idx)
if size != 1. {
t.Errorf("expected H1D.Axis.BinWidth(%v) == %v (got %v)\n",
idx, 1., size,
)
}
}
var _ plotter.XYer = h1
var _ plotter.Valuer = h1
}
func BenchmarkH1DSTFillConst(b *testing.B) {
b.StopTimer()
h1 := hbook.NewH1D(100, 0., 100.)
b.StartTimer()
for i := 0; i < b.N; i++ {
h1.Fill(10., 1.)
}
}
func BenchmarkH1DFillFlat(b *testing.B) {
b.StopTimer()
h1 := hbook.NewH1D(100, 0., 100.)
b.StartTimer()
for i := 0; i < b.N; i++ {
h1.Fill(rnd()*100., 1.)
}
}
func BenchmarkH1DFillFlatGo(b *testing.B) {
b.StopTimer()
h1 := hbook.NewH1D(100, 0., 100.)
wg := new(sync.WaitGroup)
//wg.Add(b.N)
b.StartTimer()
// throttle...
q := make(chan struct{}, 1000)
for i := 0; i < b.N; i++ {
q <- struct{}{}
go func() {
wg.Add(1)
h1.Fill(rnd()*100., 1.)
<-q
wg.Done()
}()
}
wg.Wait()
}
func st_process_evts(n int, hists []*hbook.H1D, process func(hists []*hbook.H1D)) {
var wg sync.WaitGroup
for i := 0; i < n; i++ {
wg.Add(1)
go func() {
process(hists)
wg.Done()
}()
}
wg.Wait()
}
func st_process_evts_const(hists []*hbook.H1D) {
for _, h := range hists {
h.Fill(10., 1.)
}
}
func BenchmarkNH1DFillConst(b *testing.B) {
b.StopTimer()
hists := make([]*hbook.H1D, 100)
for i := 0; i < 100; i++ {
hists[i] = hbook.NewH1D(100, 0., 100.)
}
b.StartTimer()
for i := 0; i < b.N; i++ {
st_process_evts(100, hists, st_process_evts_const)
}
}
func st_process_evts_flat(hists []*hbook.H1D) {
for _, h := range hists {
h.Fill(rnd()*100., 1.)
}
}
func BenchmarkNH1DFillFlat(b *testing.B) {
b.StopTimer()
hists := make([]*hbook.H1D, 100)
for i := 0; i < 100; i++ {
hists[i] = hbook.NewH1D(100, 0., 100.)
}
b.StartTimer()
for i := 0; i < b.N; i++ {
st_process_evts(100, hists, st_process_evts_flat)
}
}
func TestH1DSerialization(t *testing.T) {
const nentries = 50
href := hbook.NewH1D(100, 0., 100.)
for i := 0; i < nentries; i++ {
href.Fill(rnd()*100., 1.)
}
href.Annotation()["title"] = "histo title"
href.Annotation()["name"] = "histo name"
// test gob.GobDecode/gob.GobEncode interface
func() {
buf := new(bytes.Buffer)
enc := gob.NewEncoder(buf)
err := enc.Encode(href)
if err != nil {
t.Fatalf("could not serialize histogram: %v", err)
}
var hnew hbook.H1D
dec := gob.NewDecoder(buf)
err = dec.Decode(&hnew)
if err != nil {
t.Fatalf("could not deserialize histogram: %v", err)
}
if !reflect.DeepEqual(href, &hnew) {
t.Fatalf("ref=%v\nnew=%v\n", href, &hnew)
}
}()
// test rio.Marshaler/Unmarshaler
func() {
buf := new(bytes.Buffer)
err := href.RioMarshal(buf)
if err != nil {
t.Fatalf("could not serialize histogram: %v", err)
}
var hnew hbook.H1D
err = hnew.RioUnmarshal(buf)
if err != nil {
t.Fatalf("could not deserialize histogram: %v", err)
}
if !reflect.DeepEqual(href, &hnew) {
t.Fatalf("ref=%v\nnew=%v\n", href, &hnew)
}
}()
}
// EOF