// txnCommandFunc executes the "txn" command. func txnCommandFunc(c *cli.Context) { if len(c.Args()) != 0 { panic("unexpected args") } reader := bufio.NewReader(os.Stdin) next := compareState txn := &pb.TxnRequest{} for next != nil { next = next(txn, reader) } conn, err := grpc.Dial("127.0.0.1:12379") if err != nil { panic(err) } etcd := pb.NewEtcdClient(conn) resp, err := etcd.Txn(context.Background(), txn) if err != nil { fmt.Println(err) } if resp.Succeeded { fmt.Println("executed success request list") } else { fmt.Println("executed failure request list") } }
// deleteRangeCommandFunc executes the "delegeRange" command. func deleteRangeCommandFunc(c *cli.Context) { if len(c.Args()) == 0 { panic("bad arg") } var rangeEnd []byte key := []byte(c.Args()[0]) if len(c.Args()) > 1 { rangeEnd = []byte(c.Args()[1]) } conn, err := grpc.Dial(c.GlobalString("endpoint")) if err != nil { panic(err) } etcd := pb.NewEtcdClient(conn) req := &pb.DeleteRangeRequest{Key: key, RangeEnd: rangeEnd} etcd.DeleteRange(context.Background(), req) if rangeEnd != nil { fmt.Printf("range [%s, %s) is deleted\n", string(key), string(rangeEnd)) } else { fmt.Printf("key %s is deleted\n", string(key)) } }
func benchPut(conn *grpc.ClientConn, key []byte, kc, n, c, size int) { wg.Add(c) requests := make(chan *etcdserverpb.PutRequest, n) v := make([]byte, size) _, err := rand.Read(v) if err != nil { fmt.Printf("failed to generate value: %v\n", err) os.Exit(1) return } for i := 0; i < c; i++ { go put(etcdserverpb.NewEtcdClient(conn), requests) } suffixb := make([]byte, 8) suffix := 0 for i := 0; i < n; i++ { binary.BigEndian.PutUint64(suffixb, uint64(suffix)) r := &etcdserverpb.PutRequest{ Key: append(key, suffixb...), Value: v, } requests <- r if suffix > kc { suffix = 0 } suffix++ } close(requests) }
func main() { var c, n int var url string flag.IntVar(&c, "c", 50, "number of connections") flag.IntVar(&n, "n", 200, "number of requests") // TODO: config the number of concurrency in each connection flag.StringVar(&url, "u", "127.0.0.1:12379", "etcd server endpoint") flag.Parse() if flag.NArg() < 1 { flag.Usage() os.Exit(1) } if act := flag.Args()[0]; act != "get" { fmt.Errorf("unsupported action %v", act) os.Exit(1) } var rangeEnd []byte key := []byte(flag.Args()[1]) if len(flag.Args()) > 2 { rangeEnd = []byte(flag.Args()[2]) } results = make(chan *result, n) bar = pb.New(n) bar.Format("Bom !") bar.Start() start := time.Now() wg.Add(c) requests := make(chan struct{}, n) conn, err := grpc.Dial(url) if err != nil { fmt.Errorf("dial error: %v", err) os.Exit(1) } for i := 0; i < c; i++ { go get(etcdserverpb.NewEtcdClient(conn), key, rangeEnd, requests) } for i := 0; i < n; i++ { requests <- struct{}{} } close(requests) wg.Wait() bar.Finish() printReport(n, results, time.Now().Sub(start)) }
func benchGet(conn *grpc.ClientConn, key, rangeEnd []byte, n, c int) { wg.Add(c) requests := make(chan struct{}, n) for i := 0; i < c; i++ { go get(etcdserverpb.NewEtcdClient(conn), key, rangeEnd, requests) } for i := 0; i < n; i++ { requests <- struct{}{} } close(requests) }
// putCommandFunc executes the "put" command. func putCommandFunc(c *cli.Context) { if len(c.Args()) != 2 { panic("bad arg") } key := []byte(c.Args()[0]) value := []byte(c.Args()[1]) conn, err := grpc.Dial(c.GlobalString("endpoint"), grpc.WithInsecure()) if err != nil { panic(err) } etcd := pb.NewEtcdClient(conn) req := &pb.PutRequest{Key: key, Value: value} etcd.Put(context.Background(), req) fmt.Printf("%s %s\n", key, value) }
// putCommandFunc executes the "put" command. func putCommandFunc(c *cli.Context) { if len(c.Args()) != 2 { panic("bad arg") } key := []byte(c.Args()[0]) value := []byte(c.Args()[1]) conn, err := grpc.Dial("127.0.0.1:12379") if err != nil { panic(err) } etcd := pb.NewEtcdClient(conn) req := &pb.PutRequest{Key: key, Value: value} etcd.Put(context.Background(), req) fmt.Printf("%s %s\n", key, value) }
// compactionCommandFunc executes the "compaction" command. func compactionCommandFunc(c *cli.Context) { if len(c.Args()) != 1 { panic("bad arg") } rev, err := strconv.ParseInt(c.Args()[0], 10, 64) if err != nil { panic("bad arg") } conn, err := grpc.Dial(c.GlobalString("endpoint"), grpc.WithInsecure()) if err != nil { panic(err) } etcd := pb.NewEtcdClient(conn) req := &pb.CompactionRequest{Revision: rev} etcd.Compact(context.Background(), req) }
// rangeCommandFunc executes the "range" command. func rangeCommandFunc(c *cli.Context) { if len(c.Args()) == 0 { panic("bad arg") } var rangeEnd []byte key := []byte(c.Args()[0]) if len(c.Args()) > 1 { rangeEnd = []byte(c.Args()[1]) } conn, err := grpc.Dial(c.GlobalString("endpoint")) if err != nil { panic(err) } etcd := pb.NewEtcdClient(conn) req := &pb.RangeRequest{Key: key, RangeEnd: rangeEnd} resp, err := etcd.Range(context.Background(), req) for _, kv := range resp.Kvs { fmt.Printf("%s %s\n", string(kv.Key), string(kv.Value)) } }
func main() { var c, n int var url string flag.IntVar(&c, "c", 50, "number of connections") flag.IntVar(&n, "n", 200, "number of requests") // TODO: config the number of concurrency in each connection flag.StringVar(&url, "u", "127.0.0.1:12379", "etcd server endpoint") flag.Parse() if flag.NArg() < 1 { flag.Usage() os.Exit(1) } if act := flag.Args()[0]; act != "get" { fmt.Errorf("unsupported action %v", act) os.Exit(1) } var rangeEnd []byte key := []byte(flag.Args()[1]) if len(flag.Args()) > 2 { rangeEnd = []byte(flag.Args()[2]) } results := make(chan *result, n) bar := pb.New(n) bar.Format("Bom !") bar.Start() start := time.Now() defer func() { bar.Finish() printReport(n, results, time.Now().Sub(start)) }() var wg sync.WaitGroup wg.Add(c) jobs := make(chan struct{}, n) for i := 0; i < c; i++ { go func() { defer wg.Done() conn, err := grpc.Dial(url) if err != nil { fmt.Errorf("dial error: %v", err) os.Exit(1) } etcd := etcdserverpb.NewEtcdClient(conn) req := &etcdserverpb.RangeRequest{Key: key, RangeEnd: rangeEnd} for _ = range jobs { st := time.Now() resp, err := etcd.Range(context.Background(), req) var errStr string if err != nil { errStr = err.Error() } else { errStr = resp.Header.Error } results <- &result{ errStr: errStr, duration: time.Now().Sub(st), } bar.Increment() } }() } for i := 0; i < n; i++ { jobs <- struct{}{} } close(jobs) wg.Wait() }