go-hep.org/x/hep@v0.38.1/fwk/hbooksvc/hsvc_test.go (about) 1 // Copyright ©2017 The go-hep 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 hbooksvc 6 7 import ( 8 "fmt" 9 "os" 10 "reflect" 11 "strings" 12 "testing" 13 14 "go-hep.org/x/hep/fwk" 15 "go-hep.org/x/hep/fwk/job" 16 ) 17 18 const ( 19 nentries = 100 20 nhists = 10 21 ) 22 23 func newapp(evtmax int64, nprocs int) *job.Job { 24 app := job.NewJob(nil, job.P{ 25 "EvtMax": evtmax, 26 "NProcs": nprocs, 27 "MsgLevel": job.MsgLevel("ERROR"), 28 }) 29 return app 30 } 31 32 func TestHbookSvcSeq(t *testing.T) { 33 34 app := newapp(nentries, 0) 35 36 for i := range nhists { 37 app.Create(job.C{ 38 Type: "go-hep.org/x/hep/fwk/hbooksvc.testhsvc", 39 Name: fmt.Sprintf("t%03d", i), 40 Props: job.P{}, 41 }) 42 } 43 44 app.Create(job.C{ 45 Type: "go-hep.org/x/hep/fwk/hbooksvc.hsvc", 46 Name: "histsvc", 47 Props: job.P{ 48 "Streams": map[string]Stream{ 49 "/my-hist": { 50 Name: "hist-seq.rio", 51 Mode: Write, 52 }, 53 }, 54 }, 55 }) 56 57 app.Run() 58 os.Remove("hist-seq.rio") 59 } 60 61 func TestHbookSvcConc(t *testing.T) { 62 63 for _, nprocs := range []int{1, 2, 4, 8} { 64 app := newapp(nentries, nprocs) 65 app.Infof("=== nprocs: %d ===\n", nprocs) 66 67 for i := range nhists { 68 app.Create(job.C{ 69 Type: "go-hep.org/x/hep/fwk/hbooksvc.testhsvc", 70 Name: fmt.Sprintf("t%03d", i), 71 Props: job.P{}, 72 }) 73 } 74 75 app.Create(job.C{ 76 Type: "go-hep.org/x/hep/fwk/hbooksvc.hsvc", 77 Name: "histsvc", 78 Props: job.P{ 79 "Streams": map[string]Stream{ 80 "/my-hist": { 81 Name: fmt.Sprintf("hist-conc-%d.rio", nprocs), 82 Mode: Write, 83 }, 84 }, 85 }, 86 }) 87 88 app.Run() 89 os.Remove(fmt.Sprintf("hist-conc-%d.rio", nprocs)) 90 } 91 } 92 93 func TestHbookStreamName(t *testing.T) { 94 var svc hsvc 95 for _, test := range []struct { 96 name string 97 want []string 98 }{ 99 { 100 name: "histo", 101 want: []string{"", "histo"}, 102 }, 103 { 104 name: "/histo", 105 want: []string{"", "histo"}, 106 }, 107 { 108 name: "/histo/", 109 want: []string{"", "histo"}, 110 }, 111 { 112 name: "/my-stream/histo", 113 want: []string{"my-stream", "histo"}, 114 }, 115 { 116 name: "my-stream/histo", 117 want: []string{"my-stream", "histo"}, 118 }, 119 { 120 name: "my-stream/histo/", 121 want: []string{"my-stream", "histo"}, 122 }, 123 { 124 name: "/my-stream/histo/", 125 want: []string{"my-stream", "histo"}, 126 }, 127 { 128 name: "/my-stream/hdir/histo", 129 want: []string{"my-stream", "hdir/histo"}, 130 }, 131 { 132 name: "/my-stream/hdir/histo/", 133 want: []string{"my-stream", "hdir/histo"}, 134 }, 135 { 136 name: "my-stream/hdir/histo", 137 want: []string{"my-stream", "hdir/histo"}, 138 }, 139 { 140 name: "my-stream/hdir/histo/", 141 want: []string{"my-stream", "hdir/histo"}, 142 }, 143 } { 144 stream, name := svc.split(test.name) 145 got := []string{stream, name} 146 if !reflect.DeepEqual(got, test.want) { 147 t.Errorf("test.split(%q): got=%v. want=%v\n", test.name, got, test.want) 148 } 149 } 150 } 151 152 type testhsvc struct { 153 fwk.TaskBase 154 155 hsvc fwk.HistSvc 156 h1d fwk.H1D 157 stream string 158 } 159 160 func (tsk *testhsvc) Configure(ctx fwk.Context) error { 161 var err error 162 163 return err 164 } 165 166 func (tsk *testhsvc) StartTask(ctx fwk.Context) error { 167 var err error 168 169 svc, err := ctx.Svc("histsvc") 170 if err != nil { 171 return err 172 } 173 174 tsk.hsvc = svc.(fwk.HistSvc) 175 176 if !strings.HasPrefix(tsk.stream, "/") { 177 tsk.stream = "/" + tsk.stream 178 } 179 tsk.stream = strings.TrimSuffix(tsk.stream, "/") 180 181 tsk.h1d, err = tsk.hsvc.BookH1D(tsk.stream+"/h1d-"+tsk.Name(), 100, -10, 10) 182 if err != nil { 183 return err 184 } 185 186 return err 187 } 188 189 func (tsk *testhsvc) StopTask(ctx fwk.Context) error { 190 var err error 191 192 h := tsk.h1d.Hist 193 if got := h.Entries(); got != nentries { 194 return fmt.Errorf("got %d entries. want=%d", got, nentries) 195 } 196 if got, want := h.XMean(), 49.5; got != want { 197 return fmt.Errorf("got mean=%v. want=%v", got, want) 198 } 199 200 if got, want := h.XRMS(), 57.301832431432764; got != want { 201 return fmt.Errorf("got RMS=%v. want=%v", got, want) 202 } 203 return err 204 } 205 206 func (tsk *testhsvc) Process(ctx fwk.Context) error { 207 var err error 208 id := ctx.ID() 209 tsk.hsvc.FillH1D(tsk.h1d.ID, float64(id), 1) 210 return err 211 } 212 213 func newtesthsvc(typ, name string, mgr fwk.App) (fwk.Component, error) { 214 var err error 215 216 tsk := &testhsvc{ 217 TaskBase: fwk.NewTask(typ, name, mgr), 218 stream: "", 219 } 220 221 err = tsk.DeclProp("Stream", &tsk.stream) 222 if err != nil { 223 return nil, err 224 } 225 226 return tsk, err 227 } 228 229 func init() { 230 fwk.Register(reflect.TypeOf(testhsvc{}), newtesthsvc) 231 }