Exemplo n.º 1
0
func BenchmarkMutateSerializeWithReflection(b *testing.B) {
	b.ReportAllocs()

	type teststr struct {
		AnInt       int        `hbase:"cf:a"`
		AnInt8      int8       `hbase:"cf:b"`
		AnInt16     int16      `hbase:"cf:c"`
		AnInt32     int32      `hbase:"cf:d"`
		AnInt64     int64      `hbase:"cf:e"`
		AnUInt      uint       `hbase:"cf:f"`
		AnUInt8     uint8      `hbase:"cf:g"`
		AnUInt16    uint16     `hbase:"cf:h"`
		AnUInt32    uint32     `hbase:"cf:i"`
		AnUInt64    uint64     `hbase:"cf:j"`
		AFloat32    float32    `hbase:"cf:k"`
		AFloat64    float64    `hbase:"cf:l"`
		AComplex64  complex64  `hbase:"cf:m"`
		AComplex128 complex128 `hbase:"cf:n"`
		APointer    *int       `hbase:"cf:o"`
		AnArray     [6]uint8   `hbase:"cf:p"`
		ASlice      []uint8    `hbase:"cf:q"`
		AString     string     `hbase:"cf:r"`
	}

	number := 150
	for i := 0; i < b.N; i++ {
		str := teststr{
			AnInt:       10,
			AnInt8:      20,
			AnInt16:     30,
			AnInt32:     40,
			AnInt64:     50,
			AnUInt:      60,
			AnUInt8:     70,
			AnUInt16:    80,
			AnUInt32:    90,
			AnUInt64:    100,
			AFloat32:    110,
			AFloat64:    120,
			AComplex64:  130,
			AComplex128: 140,
			APointer:    &number,
			AnArray:     [6]uint8{4, 8, 15, 26, 23, 42},
			ASlice:      []uint8{1, 1, 3, 5, 8, 13, 21, 34, 55},
			AString:     "This is a test string.",
		}
		mutate, err := hrpc.NewPutStrRef(context.Background(), "", "", str)
		if err != nil {
			b.Errorf("Error creating mutate: %v", err)
		}
		mutate.SetRegion(region.NewInfo(nil, nil, nil, nil))
		mutate.Serialize()
	}
}
Exemplo n.º 2
0
func TestPutReflection(t *testing.T) {
	key := "row2.25"
	number := 150
	data := struct {
		AnInt       int        `hbase:"cf:a"`
		AnInt8      int8       `hbase:"cf:b"`
		AnInt16     int16      `hbase:"cf:c"`
		AnInt32     int32      `hbase:"cf:d"`
		AnInt64     int64      `hbase:"cf:e"`
		AnUInt      uint       `hbase:"cf:f"`
		AnUInt8     uint8      `hbase:"cf:g"`
		AnUInt16    uint16     `hbase:"cf:h"`
		AnUInt32    uint32     `hbase:"cf:i"`
		AnUInt64    uint64     `hbase:"cf:j"`
		AFloat32    float32    `hbase:"cf:k"`
		AFloat64    float64    `hbase:"cf:l"`
		AComplex64  complex64  `hbase:"cf:m"`
		AComplex128 complex128 `hbase:"cf:n"`
		APointer    *int       `hbase:"cf:o"`
		AnArray     [6]uint8   `hbase:"cf:p"`
		ASlice      []uint8    `hbase:"cf:q"`
		AString     string     `hbase:"cf:r"`
	}{
		AnInt:       10,
		AnInt8:      20,
		AnInt16:     30,
		AnInt32:     40,
		AnInt64:     50,
		AnUInt:      60,
		AnUInt8:     70,
		AnUInt16:    80,
		AnUInt32:    90,
		AnUInt64:    100,
		AFloat32:    110,
		AFloat64:    120,
		AComplex64:  130,
		AComplex128: 140,
		APointer:    &number,
		AnArray:     [6]uint8{4, 8, 15, 26, 23, 42},
		ASlice:      []uint8{1, 1, 3, 5, 8, 13, 21, 34, 55},
		AString:     "This is a test string.",
	}

	if host == nil {
		t.Fatal("Host is not set!")
	}

	c := gohbase.NewClient(*host)
	putRequest, err := hrpc.NewPutStrRef(context.Background(), table, key, data)
	if err != nil {
		t.Errorf("NewPutStrRef returned an error: %v", err)
	}

	_, err = c.Put(putRequest)
	if err != nil {
		t.Errorf("Put returned an error: %v", err)
	}

	headers := map[string][]string{"cf": nil}
	get, err := hrpc.NewGetStr(context.Background(), table, key, hrpc.Families(headers))
	if err != nil {
		t.Fatalf("Failed to create Get request: %s", err)
	}
	rsp, err := c.Get(get)
	if err != nil {
		t.Errorf("Get returned an error: %v", err)
	}

	expected := map[string][]byte{
		"a": []byte{10},
		"b": []byte{20},
		"c": []byte{30, 0},
		"d": []byte{40, 0, 0, 0},
		"e": []byte{50, 0, 0, 0, 0, 0, 0, 0},
		"f": []byte{60},
		"g": []byte{70},
		"h": []byte{80, 0},
		"i": []byte{90, 0, 0, 0},
		"j": []byte{100, 0, 0, 0, 0, 0, 0, 0},
		"k": []byte{0, 0, 220, 66},
		"l": []byte{0, 0, 0, 0, 0, 0, 94, 64},
		"m": []byte{0, 0, 2, 67, 0, 0, 0, 0},
		"n": []byte{0, 0, 0, 0, 0, 128, 97, 64, 0, 0, 0, 0, 0, 0, 0, 0},
		"o": []byte{150},
		"p": []byte{4, 8, 15, 26, 23, 42},
		"q": []byte{1, 1, 3, 5, 8, 13, 21, 34, 55},
		"r": []byte("This is a test string."),
	}

	for _, cell := range rsp.Cells {
		want, ok := expected[string(cell.Qualifier)]
		if !ok {
			t.Errorf("Unexpected qualifier: %q in %#v", cell.Qualifier, rsp)
		} else if !bytes.Equal(cell.Value, want) {
			t.Errorf("qualifier %q didn't match: wanted %q, but got %q",
				cell.Qualifier, want, cell.Value)
		}
	}

}