github.com/vipernet-xyz/tm@v0.34.24/test/loadtime/cmd/load/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/google/uuid"
     7  	"github.com/informalsystems/tm-load-test/pkg/loadtest"
     8  
     9  	"github.com/vipernet-xyz/tm/test/loadtime/payload"
    10  )
    11  
    12  // Ensure all of the interfaces are correctly satisfied.
    13  var (
    14  	_ loadtest.ClientFactory = (*ClientFactory)(nil)
    15  	_ loadtest.Client        = (*TxGenerator)(nil)
    16  )
    17  
    18  // ClientFactory implements the loadtest.ClientFactory interface.
    19  type ClientFactory struct {
    20  	ID []byte
    21  }
    22  
    23  // TxGenerator is responsible for generating transactions.
    24  // TxGenerator holds the set of information that will be used to generate
    25  // each transaction.
    26  type TxGenerator struct {
    27  	id    []byte
    28  	conns uint64
    29  	rate  uint64
    30  	size  uint64
    31  }
    32  
    33  func main() {
    34  	u := [16]byte(uuid.New()) // generate run ID on startup
    35  	if err := loadtest.RegisterClientFactory("loadtime-client", &ClientFactory{ID: u[:]}); err != nil {
    36  		panic(err)
    37  	}
    38  	loadtest.Run(&loadtest.CLIConfig{
    39  		AppName:              "loadtime",
    40  		AppShortDesc:         "Generate timestamped transaction load.",
    41  		AppLongDesc:          "loadtime generates transaction load for the purpose of measuring the end-to-end latency of a transaction from submission to execution in a Tendermint network.", //nolint:lll
    42  		DefaultClientFactory: "loadtime-client",
    43  	})
    44  }
    45  
    46  func (f *ClientFactory) ValidateConfig(cfg loadtest.Config) error {
    47  	psb, err := payload.MaxUnpaddedSize()
    48  	if err != nil {
    49  		return err
    50  	}
    51  	if psb > cfg.Size {
    52  		return fmt.Errorf("payload size exceeds configured size")
    53  	}
    54  	return nil
    55  }
    56  
    57  func (f *ClientFactory) NewClient(cfg loadtest.Config) (loadtest.Client, error) {
    58  	return &TxGenerator{
    59  		id:    f.ID,
    60  		conns: uint64(cfg.Connections),
    61  		rate:  uint64(cfg.Rate),
    62  		size:  uint64(cfg.Size),
    63  	}, nil
    64  }
    65  
    66  func (c *TxGenerator) GenerateTx() ([]byte, error) {
    67  	return payload.NewBytes(&payload.Payload{
    68  		Connections: c.conns,
    69  		Rate:        c.rate,
    70  		Size:        c.size,
    71  		Id:          c.id,
    72  	})
    73  }