github.com/tachunwu/scale@v0.0.0-20230215135019-20d7de63a560/cmd/leaf/client.go (about)

     1  package main
     2  
     3  import (
     4  	"log"
     5  	"sync"
     6  	"time"
     7  
     8  	"github.com/google/uuid"
     9  	"github.com/nats-io/nats.go"
    10  	"github.com/tachunwu/scale/pkg/jetstream"
    11  )
    12  
    13  var wg sync.WaitGroup
    14  
    15  func Client() {
    16  
    17  	nc, err := nats.Connect(nats.DefaultURL)
    18  	defer nc.Close()
    19  	if err != nil {
    20  		log.Fatal(err)
    21  	}
    22  	js, err := nc.JetStream(nats.PublishAsyncMaxPending(256))
    23  	if err != nil {
    24  		log.Fatal(err)
    25  	}
    26  	// Test
    27  
    28  	wg.Add(8)
    29  	start := time.Now()
    30  	go MicroBench(js)
    31  	go MicroBench(js)
    32  	go MicroBench(js)
    33  	go MicroBench(js)
    34  	go MicroBench(js)
    35  	go MicroBench(js)
    36  	go MicroBench(js)
    37  	go MicroBench(js)
    38  	wg.Wait()
    39  	log.Println(time.Since(start).Seconds())
    40  
    41  }
    42  
    43  func MicroBench(js nats.JetStreamContext) {
    44  	for i := 0; i < 100000; i++ {
    45  		kv, _ := js.KeyValue(jetstream.BucketName)
    46  		k := uuid.New().String()
    47  		kv.Put(k, []byte("value"))
    48  	}
    49  	defer wg.Done()
    50  }