-
Notifications
You must be signed in to change notification settings - Fork 35
/
hsvc_test.go
233 lines (198 loc) · 4.31 KB
/
hsvc_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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Copyright 2017 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 hbooksvc
import (
"fmt"
"os"
"reflect"
"strings"
"testing"
"go-hep.org/x/hep/fwk"
"go-hep.org/x/hep/fwk/job"
)
const (
nentries = 100
nhists = 10
)
func newapp(evtmax int64, nprocs int) *job.Job {
app := job.NewJob(nil, job.P{
"EvtMax": evtmax,
"NProcs": nprocs,
"MsgLevel": job.MsgLevel("ERROR"),
})
return app
}
func TestHbookSvcSeq(t *testing.T) {
app := newapp(nentries, 0)
for i := 0; i < nhists; i++ {
app.Create(job.C{
Type: "go-hep.org/x/hep/fwk/hbooksvc.testhsvc",
Name: fmt.Sprintf("t%03d", i),
Props: job.P{},
})
}
app.Create(job.C{
Type: "go-hep.org/x/hep/fwk/hbooksvc.hsvc",
Name: "histsvc",
Props: job.P{
"Streams": map[string]Stream{
"/my-hist": {
Name: "hist-seq.rio",
Mode: Write,
},
},
},
})
app.Run()
os.Remove("hist-seq.rio")
}
func TestHbookSvcConc(t *testing.T) {
for _, nprocs := range []int{1, 2, 4, 8} {
app := newapp(nentries, nprocs)
app.Infof("=== nprocs: %d ===\n", nprocs)
for i := 0; i < nhists; i++ {
app.Create(job.C{
Type: "go-hep.org/x/hep/fwk/hbooksvc.testhsvc",
Name: fmt.Sprintf("t%03d", i),
Props: job.P{},
})
}
app.Create(job.C{
Type: "go-hep.org/x/hep/fwk/hbooksvc.hsvc",
Name: "histsvc",
Props: job.P{
"Streams": map[string]Stream{
"/my-hist": {
Name: fmt.Sprintf("hist-conc-%d.rio", nprocs),
Mode: Write,
},
},
},
})
app.Run()
os.Remove(fmt.Sprintf("hist-conc-%d.rio", nprocs))
}
}
func TestHbookStreamName(t *testing.T) {
var svc hsvc
for _, test := range []struct {
name string
want []string
}{
{
name: "histo",
want: []string{"", "histo"},
},
{
name: "/histo",
want: []string{"", "histo"},
},
{
name: "/histo/",
want: []string{"", "histo"},
},
{
name: "/my-stream/histo",
want: []string{"my-stream", "histo"},
},
{
name: "my-stream/histo",
want: []string{"my-stream", "histo"},
},
{
name: "my-stream/histo/",
want: []string{"my-stream", "histo"},
},
{
name: "/my-stream/histo/",
want: []string{"my-stream", "histo"},
},
{
name: "/my-stream/hdir/histo",
want: []string{"my-stream", "hdir/histo"},
},
{
name: "/my-stream/hdir/histo/",
want: []string{"my-stream", "hdir/histo"},
},
{
name: "my-stream/hdir/histo",
want: []string{"my-stream", "hdir/histo"},
},
{
name: "my-stream/hdir/histo/",
want: []string{"my-stream", "hdir/histo"},
},
} {
stream, name := svc.split(test.name)
got := []string{stream, name}
if !reflect.DeepEqual(got, test.want) {
t.Errorf("test.split(%q): got=%v. want=%v\n", test.name, got, test.want)
}
}
}
type testhsvc struct {
fwk.TaskBase
hsvc fwk.HistSvc
h1d fwk.H1D
stream string
}
func (tsk *testhsvc) Configure(ctx fwk.Context) error {
var err error
return err
}
func (tsk *testhsvc) StartTask(ctx fwk.Context) error {
var err error
svc, err := ctx.Svc("histsvc")
if err != nil {
return err
}
tsk.hsvc = svc.(fwk.HistSvc)
if !strings.HasPrefix(tsk.stream, "/") {
tsk.stream = "/" + tsk.stream
}
if strings.HasSuffix(tsk.stream, "/") {
tsk.stream = tsk.stream[:len(tsk.stream)-1]
}
tsk.h1d, err = tsk.hsvc.BookH1D(tsk.stream+"/h1d-"+tsk.Name(), 100, -10, 10)
if err != nil {
return err
}
return err
}
func (tsk *testhsvc) StopTask(ctx fwk.Context) error {
var err error
h := tsk.h1d.Hist
if got := h.Entries(); got != nentries {
return fwk.Errorf("got %d entries. want=%d", got, nentries)
}
if got, want := h.XMean(), 49.5; got != want {
return fwk.Errorf("got mean=%v. want=%v", got, want)
}
if got, want := h.XRMS(), 57.301832431432764; got != want {
return fwk.Errorf("got RMS=%v. want=%v", got, want)
}
return err
}
func (tsk *testhsvc) Process(ctx fwk.Context) error {
var err error
id := ctx.ID()
tsk.hsvc.FillH1D(tsk.h1d.ID, float64(id), 1)
return err
}
func newtesthsvc(typ, name string, mgr fwk.App) (fwk.Component, error) {
var err error
tsk := &testhsvc{
TaskBase: fwk.NewTask(typ, name, mgr),
stream: "",
}
err = tsk.DeclProp("Stream", &tsk.stream)
if err != nil {
return nil, err
}
return tsk, err
}
func init() {
fwk.Register(reflect.TypeOf(testhsvc{}), newtesthsvc)
}