github.com/hyperledger/burrow@v0.34.5-0.20220512172541-77f09336001d/forensics/spin_test.go (about)

     1  // +build forensics
     2  
     3  package forensics
     4  
     5  import (
     6  	"context"
     7  	"fmt"
     8  	"sync"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/stretchr/testify/require"
    13  
    14  	"github.com/hyperledger/burrow/encoding"
    15  	"github.com/hyperledger/burrow/execution/exec"
    16  	"github.com/hyperledger/burrow/rpc/rpcevents"
    17  )
    18  
    19  func TestSpin(t *testing.T) {
    20  	const listenAddress = "localhost:10997"
    21  	wg := new(sync.WaitGroup)
    22  	wg.Add(1)
    23  	err := consume("spin", listenAddress, wg)
    24  	wg.Wait()
    25  	require.NoError(t, err)
    26  }
    27  
    28  func TestSpinAll(t *testing.T) {
    29  	const listenAddress = "localhost:10997"
    30  	numConsumer := 100
    31  	wg := new(sync.WaitGroup)
    32  	wg.Add(numConsumer)
    33  	for i := 0; i < numConsumer; i++ {
    34  		go consume(fmt.Sprintf("consumer %d", i), listenAddress, wg)
    35  	}
    36  	wg.Wait()
    37  }
    38  
    39  func consume(name, listenAddress string, wg *sync.WaitGroup) error {
    40  	defer wg.Done()
    41  	conn, err := encoding.GRPCDial(listenAddress)
    42  	if err != nil {
    43  		return err
    44  	}
    45  	cli := rpcevents.NewExecutionEventsClient(conn)
    46  	stream, err := cli.Stream(context.Background(), &rpcevents.BlocksRequest{
    47  		BlockRange: rpcevents.NewBlockRange(rpcevents.AbsoluteBound(0), rpcevents.StreamBound()),
    48  	})
    49  	startTime := time.Now()
    50  	timer := time.NewTicker(5 * time.Second)
    51  	var blocks, height uint64
    52  	defer timer.Stop()
    53  	go func() {
    54  		for t := range timer.C {
    55  			dur := t.Sub(startTime)
    56  			blocksSec := float64(blocks*uint64(time.Second)) / float64(dur)
    57  			fmt.Printf("%s height %d: blocks per second: %f\n", name, height, blocksSec)
    58  		}
    59  		fmt.Printf("BYE")
    60  	}()
    61  	return rpcevents.ConsumeBlockExecutions(stream, func(be *exec.BlockExecution) error {
    62  		blocks++
    63  		height = be.Height
    64  		return nil
    65  	})
    66  
    67  }