github.com/annchain/OG@v0.0.9/engine_test/og_test.go (about)

     1  package engine_test
     2  
     3  import (
     4  	"github.com/annchain/OG/arefactor/common/mylog"
     5  	"github.com/annchain/OG/engine"
     6  	"github.com/annchain/OG/message"
     7  	"github.com/annchain/OG/ogcore/communication"
     8  	"github.com/annchain/OG/plugin/og"
     9  	"github.com/sirupsen/logrus"
    10  	"testing"
    11  	"time"
    12  )
    13  
    14  func preparePlugins(nodes int) ([]*og.OgPlugin, []*engine.Engine) {
    15  	plugins := make([]*og.OgPlugin, nodes)
    16  	chans := make([]chan *message.GeneralMessageEvent, nodes)
    17  	communicators := make([]*LocalGeneralPeerCommunicator, nodes)
    18  
    19  	engines := make([]*engine.Engine, nodes)
    20  
    21  	for i := 0; i < nodes; i++ {
    22  		chans[i] = make(chan *message.GeneralMessageEvent)
    23  	}
    24  
    25  	for i := 0; i < nodes; i++ {
    26  		communicators[i] = NewLocalGeneralPeerCommunicator(i, chans[i], chans)
    27  	}
    28  
    29  	for i := 0; i < nodes; i++ {
    30  		plugins[i] = og.NewOgPlugin()
    31  		plugins[i].SetOutgoing(communicators[i])
    32  	}
    33  
    34  	// init general processor
    35  	for i := 0; i < nodes; i++ {
    36  		eng := engine.Engine{
    37  			Config:       engine.EngineConfig{},
    38  			PeerOutgoing: communicators[i],
    39  			PeerIncoming: communicators[i],
    40  		}
    41  		eng.InitDefault()
    42  		eng.RegisterPlugin(plugins[i])
    43  		engines[i] = &eng
    44  		eng.Start()
    45  	}
    46  
    47  	logrus.Info("Started")
    48  	return plugins, engines
    49  }
    50  
    51  // TestPingPongBenchmark will try its best to send ping pong between two to benchmark
    52  func TestPingPongBenchmark(t *testing.T) {
    53  	mylog.LogInit(logrus.InfoLevel)
    54  	nodes := 2
    55  
    56  	plugins, engines := preparePlugins(nodes)
    57  
    58  	plugins[0].OgPartner.SendMessagePing(communication.OgPeer{Id: 1})
    59  
    60  	var lastValue uint = 0
    61  	for i := 0; i < 60; i++ {
    62  		v := engines[0].GetBenchmarks()["mps"].(uint)
    63  		if lastValue == 0 {
    64  			lastValue = v
    65  		} else {
    66  			logrus.WithField("mps", v-lastValue).Info("performance")
    67  		}
    68  		lastValue = v
    69  		time.Sleep(time.Second)
    70  	}
    71  }
    72  
    73  // TestQueryStatusBenchmark will try its best to send ping pong between two to benchmark
    74  func TestQueryStatusBenchmark(t *testing.T) {
    75  	mylog.LogInit(logrus.TraceLevel)
    76  	nodes := 2
    77  
    78  	plugins, engines := preparePlugins(nodes)
    79  
    80  	plugins[0].OgPartner.SendMessageQueryStatusRequest(communication.OgPeer{Id: 1})
    81  
    82  	var lastValue uint = 0
    83  	for i := 0; i < 60; i++ {
    84  		v := engines[0].GetBenchmarks()["mps"].(uint)
    85  		if lastValue == 0 {
    86  			lastValue = v
    87  		} else {
    88  			logrus.WithField("mps", v-lastValue).Info("performance")
    89  		}
    90  		lastValue = v
    91  		time.Sleep(time.Second)
    92  	}
    93  }