Advertisement
Guest User

Untitled

a guest
Mar 19th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.86 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "testing"
  6.     "time"
  7. )
  8.  
  9. type msg struct {
  10.     sysid   int
  11.     payload int
  12.     crTime  time.Time
  13. }
  14.  
  15. type entity struct {
  16.     sum          int
  17.     sysid        int
  18.     finishReport chan int64
  19.     countTo      int
  20. }
  21.  
  22. func (e *entity) Id() AnyId {
  23.     return e.sysid
  24. }
  25.  
  26. func (e *entity) Update(message Message) ([]OutgoingMessage, bool) {
  27.     if msg, ok := message.(msg); ok {
  28.         e.sum += msg.payload
  29.     }
  30.     if e.sum%e.countTo == 0 {
  31.         return []OutgoingMessage{&entity{e.sum, e.sysid, e.finishReport, e.countTo}}, true
  32.     }
  33.     return nil, false
  34. }
  35.  
  36. func TestCoreTransmition(t *testing.T) {
  37.     fmt.Println("testing core")
  38.     countTo := 3
  39.     totalEnt := 50
  40.     finishChan := make(chan int64)
  41.     factory := func(message Message) (Entity, bool) {
  42.         if msg, ok := message.(msg); ok {
  43.             return &entity{0, msg.sysid, finishChan, countTo}, true
  44.         }
  45.         return &entity{}, false
  46.     }
  47.     core := NewCore()
  48.  
  49.     downstream := make(Downstream)
  50.  
  51.     upstream := make(Upstream)
  52.  
  53.     go func() {
  54.         for i := range core.debugChan {
  55.             fmt.Printf("From CORE: %+v\n", i)
  56.         }
  57.     }()
  58.  
  59.     done := make(chan bool)
  60.     go func() {
  61.         finished := 0
  62.         for {
  63.             <-upstream
  64.             finished++
  65.             if finished == totalEnt {
  66.                 t.Log("Finished")
  67.                 break
  68.             }
  69.  
  70.         }
  71.         done <- true
  72.  
  73.     }()
  74.     core.WithDownstreams(0, downstream).WithUpstreams(0, upstream).Run(factory, 8)
  75.  
  76.     var warmingTimeSum int64
  77.     var hotTimeSum int64
  78.     for i := 0; i < totalEnt*countTo; i++ {
  79.         t := time.Now()
  80.         downstream <- msg{i % totalEnt, 1, time.Now()}
  81.         el := time.Since(t).Nanoseconds()
  82.         if i < totalEnt {
  83.             warmingTimeSum += el
  84.         } else {
  85.             hotTimeSum += el
  86.         }
  87.         time.Sleep(10 * time.Microsecond)
  88.     }
  89.  
  90.     t.Logf("Warming ns/op: %f ", float64(warmingTimeSum)/float64(totalEnt))
  91.     t.Logf("Hot ns/op: %f", float64(hotTimeSum)/float64(totalEnt*(countTo-1)))
  92.  
  93.     <-done
  94.     core.Stop()
  95.     // time.Sleep(500 * time.Millisecond)
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement