github.com/pokt-network/tendermint@v0.32.11-0.20230426215212-59310158d3e9/abci/tests/benchmarks/simple/simple.go (about)

     1  package main
     2  
     3  import (
     4  	"bufio"
     5  	"fmt"
     6  	"io"
     7  	"log"
     8  	"reflect"
     9  
    10  	"github.com/tendermint/tendermint/abci/types"
    11  	tmnet "github.com/tendermint/tendermint/libs/net"
    12  )
    13  
    14  func main() {
    15  
    16  	conn, err := tmnet.Connect("unix://test.sock")
    17  	if err != nil {
    18  		log.Fatal(err.Error())
    19  	}
    20  
    21  	// Make a bunch of requests
    22  	counter := 0
    23  	for i := 0; ; i++ {
    24  		req := types.ToRequestEcho("foobar")
    25  		_, err := makeRequest(conn, req)
    26  		if err != nil {
    27  			log.Fatal(err.Error())
    28  		}
    29  		counter++
    30  		if counter%1000 == 0 {
    31  			fmt.Println(counter)
    32  		}
    33  	}
    34  }
    35  
    36  func makeRequest(conn io.ReadWriter, req *types.Request) (*types.Response, error) {
    37  	var bufWriter = bufio.NewWriter(conn)
    38  
    39  	// Write desired request
    40  	err := types.WriteMessage(req, bufWriter)
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  	err = types.WriteMessage(types.ToRequestFlush(), bufWriter)
    45  	if err != nil {
    46  		return nil, err
    47  	}
    48  	err = bufWriter.Flush()
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  
    53  	// Read desired response
    54  	var res = &types.Response{}
    55  	err = types.ReadMessage(conn, res)
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  	var resFlush = &types.Response{}
    60  	err = types.ReadMessage(conn, resFlush)
    61  	if err != nil {
    62  		return nil, err
    63  	}
    64  	if _, ok := resFlush.Value.(*types.Response_Flush); !ok {
    65  		return nil, fmt.Errorf("expected flush response but got something else: %v", reflect.TypeOf(resFlush))
    66  	}
    67  
    68  	return res, nil
    69  }