Example #1
0
func bench_grpc() {
	conn, err := grpc.Dial(addr, grpc.WithInsecure())
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c := pb.NewKvsClient(conn)
	c.Put(context.Background(), &pb.PutRequest{Key: "key", Value: "value"})
	time.Sleep(2 * time.Second)

	wg := &sync.WaitGroup{}
	ch := make(chan struct{})
	for i := 0; i < concurrency; i++ {
		wg.Add(1)
		go func(ch chan struct{}) {
			defer wg.Done()
			for _ = range ch {

				// gRPC I/F
				_, err := c.Get(context.Background(), &pb.GetRequest{Key: "key"})
				if err != nil {
					log.Fatal(err)
				}
				//fmt.Println(resp.Value)
			}
		}(ch)
	}

	start := time.Now()
	for i := 0; i < numJobs; i++ {
		ch <- struct{}{}
	}

	close(ch)

	fmt.Printf("Waiting for workers...\n")
	wg.Wait()
	elapsed := time.Since(start)

	fmt.Printf("%d jobs in %0.2f seconds (%0.2f jobs/sec)\n", numJobs, float64(elapsed)/float64(time.Second), float64(numJobs)/(float64(elapsed)/float64(time.Second)))
}
Example #2
0
func main() {
	conn, err := grpc.Dial(addr, grpc.WithInsecure())
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c := pb.NewKvsClient(conn)

	scanner := bufio.NewScanner(os.Stdin)
	for {
		fmt.Print("> ")
		if !scanner.Scan() {
			os.Exit(0)
		}
		args := strings.Split(scanner.Text(), " ")

		switch args[0] {
		case "get":
			if len(args) < 1 {
				continue
			}
			var key = args[1]
			resp, err := c.Get(context.Background(), &pb.GetRequest{Key: key})
			if err != nil {
				log.Printf("Error:%s", err.Error())
				continue
			}
			fmt.Printf("Key:[%s] Value:[%s]\n", key, resp.Value)
		case "put":
			if len(args) < 3 {
				continue
			}
			key, value := args[1], args[2]
			_, err = c.Put(context.Background(), &pb.PutRequest{Key: key, Value: value})
			if err != nil {
				log.Fatalf("Error:%v", err)
				continue
			}
			fmt.Println("OK")
		case "delete":
			if len(args) < 1 {
				continue
			}
			var key = args[1]
			_, err := c.Delete(context.Background(), &pb.DeleteRequest{Key: key})
			if err != nil {
				log.Printf("Error:%s", err.Error())
				continue
			}
			fmt.Println("OK")
		case "range":
			startKey := ""
			if len(args) > 1 {
				startKey = args[1]
			}
			rc, err := c.Range(context.Background(), &pb.RangeRequest{StartKey: startKey})
			if err != nil {
				log.Fatalf("Error:%v", err)
				continue
			}
			for {
				resp, err := rc.Recv()
				if err == io.EOF {
					break
				}
				if err != nil {
					log.Println(err.Error())
					break
				}
				fmt.Printf("Key: [%s] Value: [%s]\n", resp.Key, resp.Value)
			}
		case "watch":
			prefix := ""
			if len(args) > 1 {
				prefix = args[1]
			}
			rc, err := c.Watch(context.Background(), &pb.WatchRequest{Prefix: prefix})
			if err != nil {
				log.Fatalf("Error:%v", err)
				continue
			}
			for {
				resp, err := rc.Recv()
				if err == io.EOF {
					break
				}
				if err != nil {
					log.Println(err.Error())
					break
				}
				fmt.Printf("Key: [%s] Value: [%s]\n", resp.Key, resp.Value)
			}
		default:
			log.Printf("Unknown command. cmd:[%v]", args)
		}
	}
}