github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/benchmark/ipc/grpc/client.go (about)

     1  // Copyright (C) 2018 go-nebulas authors
     2  //
     3  // This file is part of the go-nebulas library.
     4  //
     5  // the go-nebulas library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // the go-nebulas library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU General Public License
    16  // along with the go-nebulas library.  If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  
    19  package main
    20  
    21  import (
    22  	"context"
    23  	"crypto/rand"
    24  	"io"
    25  	"log"
    26  	"time"
    27  
    28  	"github.com/nebulasio/go-nebulas/nbre/benchmark/ipc/grpc/pb"
    29  	"google.golang.org/grpc"
    30  )
    31  
    32  func main() {
    33  	conn := DialServer()
    34  	defer conn.Close()
    35  
    36  	client := ipcpb.NewBenchmarkServiceClient(conn)
    37  
    38  	dataSize := 1024 * 1024
    39  	count := 100
    40  	data := RandomCSPRNG(dataSize)
    41  	timestamp := time.Now().UnixNano()
    42  	for i := 0; i < count; i++ {
    43  		_, err := client.Transfer(context.Background(), &ipcpb.Benchmark{
    44  			//Timestamp:time.Now().UnixNano(),
    45  			Data: data,
    46  		})
    47  		if err != nil {
    48  			log.Println("Transfer failed:", err)
    49  			break
    50  		}
    51  	}
    52  
    53  	speed := int64(dataSize*count*2*1e9/1000) / (time.Now().UnixNano() - timestamp)
    54  	log.Println("grpc data size:", dataSize/1024, "kb speed:", speed, "kb/s")
    55  }
    56  
    57  func DialServer() *grpc.ClientConn {
    58  	maxSize := 64 * 1024 * 1024
    59  	conn, err := grpc.Dial("127.0.0.1:8696", grpc.WithInsecure(), grpc.WithMaxMsgSize(maxSize))
    60  	if err != nil {
    61  		log.Println("rpc.Dial() failed: ", err)
    62  	}
    63  	if err != nil {
    64  		log.Fatal(err)
    65  	}
    66  	return conn
    67  }
    68  
    69  // RandomCSPRNG a cryptographically secure pseudo-random number generator
    70  func RandomCSPRNG(n int) []byte {
    71  	buff := make([]byte, n)
    72  	_, err := io.ReadFull(rand.Reader, buff)
    73  	if err != nil {
    74  		panic("reading from crypto/rand failed: " + err.Error())
    75  	}
    76  	return buff
    77  }