Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "testing"
- "time"
- )
- type msg struct {
- sysid int
- payload int
- crTime time.Time
- }
- type entity struct {
- sum int
- sysid int
- finishReport chan int64
- countTo int
- }
- func (e *entity) Id() AnyId {
- return e.sysid
- }
- func (e *entity) Update(message Message) ([]OutgoingMessage, bool) {
- if msg, ok := message.(msg); ok {
- e.sum += msg.payload
- }
- if e.sum%e.countTo == 0 {
- return []OutgoingMessage{&entity{e.sum, e.sysid, e.finishReport, e.countTo}}, true
- }
- return nil, false
- }
- func TestCoreTransmition(t *testing.T) {
- fmt.Println("testing core")
- countTo := 3
- totalEnt := 50
- finishChan := make(chan int64)
- factory := func(message Message) (Entity, bool) {
- if msg, ok := message.(msg); ok {
- return &entity{0, msg.sysid, finishChan, countTo}, true
- }
- return &entity{}, false
- }
- core := NewCore()
- downstream := make(Downstream)
- upstream := make(Upstream)
- go func() {
- for i := range core.debugChan {
- fmt.Printf("From CORE: %+v\n", i)
- }
- }()
- done := make(chan bool)
- go func() {
- finished := 0
- for {
- <-upstream
- finished++
- if finished == totalEnt {
- t.Log("Finished")
- break
- }
- }
- done <- true
- }()
- core.WithDownstreams(0, downstream).WithUpstreams(0, upstream).Run(factory, 8)
- var warmingTimeSum int64
- var hotTimeSum int64
- for i := 0; i < totalEnt*countTo; i++ {
- t := time.Now()
- downstream <- msg{i % totalEnt, 1, time.Now()}
- el := time.Since(t).Nanoseconds()
- if i < totalEnt {
- warmingTimeSum += el
- } else {
- hotTimeSum += el
- }
- time.Sleep(10 * time.Microsecond)
- }
- t.Logf("Warming ns/op: %f ", float64(warmingTimeSum)/float64(totalEnt))
- t.Logf("Hot ns/op: %f", float64(hotTimeSum)/float64(totalEnt*(countTo-1)))
- <-done
- core.Stop()
- // time.Sleep(500 * time.Millisecond)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement