From a5d61d2478b0a3f7aeb899fd9a367d2b065c9602 Mon Sep 17 00:00:00 2001 From: Aleksey Myasnikov Date: Thu, 5 Dec 2024 19:46:22 +0300 Subject: [PATCH] refactored benchmarks --- params_builder_test.go | 192 ----------------- params_test.go | 455 ++++++++++++++--------------------------- 2 files changed, 159 insertions(+), 488 deletions(-) delete mode 100644 params_builder_test.go diff --git a/params_builder_test.go b/params_builder_test.go deleted file mode 100644 index 501c15056..000000000 --- a/params_builder_test.go +++ /dev/null @@ -1,192 +0,0 @@ -package ydb_test - -import ( - "testing" - - "github.com/stretchr/testify/require" - "github.com/ydb-platform/ydb-go-genproto/protos/Ydb" - - "github.com/ydb-platform/ydb-go-sdk/v3" - "github.com/ydb-platform/ydb-go-sdk/v3/internal/allocator" -) - -func must[T any](v T, err error) T { - if err != nil { - panic(err) - } - - return v -} - -func TestParamsBuilder(t *testing.T) { - t.Run("UsingHelpers", func(t *testing.T) { - params := ydb.ParamsBuilder(). - Param("test").BeginTuple().Add().Uint64(123).Add().Uint64(321).EndTuple(). - Build() - require.EqualValues(t, - map[string]*Ydb.TypedValue{ - "test": { - Type: &Ydb.Type{ - Type: &Ydb.Type_TupleType{ - TupleType: &Ydb.TupleType{ - Elements: []*Ydb.Type{ - { - Type: &Ydb.Type_TypeId{ - TypeId: Ydb.Type_UINT64, - }, - }, - { - Type: &Ydb.Type_TypeId{ - TypeId: Ydb.Type_UINT64, - }, - }, - }, - }, - }, - }, - Value: &Ydb.Value{ - Items: []*Ydb.Value{ - { - Value: &Ydb.Value_Uint64Value{ - Uint64Value: 123, - }, - }, - { - Value: &Ydb.Value_Uint64Value{ - Uint64Value: 321, - }, - }, - }, - }, - }, - }, - must(params.ToYDB(allocator.New())), - ) - }) - t.Run("Raw", func(t *testing.T) { - params := ydb.ParamsBuilder().Param("test").Raw(&Ydb.TypedValue{ - Type: &Ydb.Type{ - Type: &Ydb.Type_TupleType{ - TupleType: &Ydb.TupleType{ - Elements: []*Ydb.Type{ - { - Type: &Ydb.Type_TypeId{ - TypeId: Ydb.Type_UINT64, - }, - }, - { - Type: &Ydb.Type_TypeId{ - TypeId: Ydb.Type_UINT64, - }, - }, - }, - }, - }, - }, - Value: &Ydb.Value{ - Items: []*Ydb.Value{ - { - Value: &Ydb.Value_Uint64Value{ - Uint64Value: 123, - }, - }, - { - Value: &Ydb.Value_Uint64Value{ - Uint64Value: 321, - }, - }, - }, - }, - }).Build() - require.EqualValues(t, - map[string]*Ydb.TypedValue{ - "test": { - Type: &Ydb.Type{ - Type: &Ydb.Type_TupleType{ - TupleType: &Ydb.TupleType{ - Elements: []*Ydb.Type{ - { - Type: &Ydb.Type_TypeId{ - TypeId: Ydb.Type_UINT64, - }, - }, - { - Type: &Ydb.Type_TypeId{ - TypeId: Ydb.Type_UINT64, - }, - }, - }, - }, - }, - }, - Value: &Ydb.Value{ - Items: []*Ydb.Value{ - { - Value: &Ydb.Value_Uint64Value{ - Uint64Value: 123, - }, - }, - { - Value: &Ydb.Value_Uint64Value{ - Uint64Value: 321, - }, - }, - }, - }, - }, - }, - must(params.ToYDB(allocator.New())), - ) - }) -} - -func BenchmarkParamsBuilder(b *testing.B) { - b.Run("UsingHelpers", func(b *testing.B) { - b.ReportAllocs() - for i := 0; i < b.N; i++ { - _ = ydb.ParamsBuilder(). - Param("test").BeginTuple().Add().Uint64(123).Add().Uint64(321).EndTuple(). - Build() - } - }) - b.Run("Raw", func(b *testing.B) { - raw := Ydb.TypedValue{ - Type: &Ydb.Type{ - Type: &Ydb.Type_TupleType{ - TupleType: &Ydb.TupleType{ - Elements: []*Ydb.Type{ - { - Type: &Ydb.Type_TypeId{ - TypeId: Ydb.Type_UINT64, - }, - }, - { - Type: &Ydb.Type_TypeId{ - TypeId: Ydb.Type_UINT64, - }, - }, - }, - }, - }, - }, - Value: &Ydb.Value{ - Items: []*Ydb.Value{ - { - Value: &Ydb.Value_Uint64Value{ - Uint64Value: 123, - }, - }, - { - Value: &Ydb.Value_Uint64Value{ - Uint64Value: 321, - }, - }, - }, - }, - } - b.ReportAllocs() - for i := 0; i < b.N; i++ { - _ = ydb.ParamsBuilder().Param("test").Raw(&raw).Build() - } - }) -} diff --git a/params_test.go b/params_test.go index 2f1f5c15e..324c08e95 100644 --- a/params_test.go +++ b/params_test.go @@ -15,232 +15,117 @@ import ( "github.com/ydb-platform/ydb-go-sdk/v3/table/types" ) -func makeParamsUsingBuilder(tb testing.TB) *params.Parameters { - return ydb.ParamsBuilder(). - Param("$a").Uint64(123). - Param("$b").Uuid(uuid.UUID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}). - Param("$c").BeginOptional().Uint64(func(v uint64) *uint64 { return &v }(123)).EndOptional(). - Param("$d").BeginList().Add().Uint64(123).Add().Uint64(123).Add().Uint64(123).Add().Uint64(123).EndList(). - Build() -} - -func TestParamsBuilder(t *testing.T) { - params := makeParamsUsingBuilder(t) - a := allocator.New() - v := params.ToYDB(a) - require.Equal(t, - fmt.Sprintf("%v", map[string]*Ydb.TypedValue{ - "$a": { - Type: &Ydb.Type{ - Type: &Ydb.Type_TypeId{ - TypeId: Ydb.Type_UINT64, - }, - }, - Value: &Ydb.Value{ - Value: &Ydb.Value_Uint64Value{ - Uint64Value: 123, - }, - }, +var ( + a = &Ydb.TypedValue{ + Type: &Ydb.Type{ + Type: &Ydb.Type_TypeId{ + TypeId: Ydb.Type_UINT64, + }, + }, + Value: &Ydb.Value{ + Value: &Ydb.Value_Uint64Value{ + Uint64Value: 123, + }, + }, + } + b = &Ydb.TypedValue{ + Type: &Ydb.Type{ + Type: &Ydb.Type_TypeId{ + TypeId: Ydb.Type_UUID, + }, + }, + Value: &Ydb.Value{ + Value: &Ydb.Value_Low_128{ + Low_128: 506660481424032516, }, - "$b": { - Type: &Ydb.Type{ - Type: &Ydb.Type_TypeId{ - TypeId: Ydb.Type_UUID, + High_128: 1157159078456920585, + }, + } + c = &Ydb.TypedValue{ + Type: &Ydb.Type{ + Type: &Ydb.Type_OptionalType{ + OptionalType: &Ydb.OptionalType{ + Item: &Ydb.Type{ + Type: &Ydb.Type_TypeId{ + TypeId: Ydb.Type_UINT64, + }, }, }, - Value: &Ydb.Value{ - Value: &Ydb.Value_Low_128{ - Low_128: 506660481424032516, + }, + }, + Value: &Ydb.Value{ + Value: &Ydb.Value_Uint64Value{ + Uint64Value: 123, + }, + }, + } + d = &Ydb.TypedValue{ + Type: &Ydb.Type{ + Type: &Ydb.Type_ListType{ + ListType: &Ydb.ListType{ + Item: &Ydb.Type{ + Type: &Ydb.Type_TypeId{ + TypeId: Ydb.Type_UINT64, + }, }, - High_128: 1157159078456920585, }, }, - "$c": { - Type: &Ydb.Type{ - Type: &Ydb.Type_OptionalType{ - OptionalType: &Ydb.OptionalType{ - Item: &Ydb.Type{ - Type: &Ydb.Type_TypeId{ - TypeId: Ydb.Type_UINT64, - }, - }, - }, + }, + Value: &Ydb.Value{ + Items: []*Ydb.Value{ + { + Value: &Ydb.Value_Uint64Value{ + Uint64Value: 123, }, }, - Value: &Ydb.Value{ + { Value: &Ydb.Value_Uint64Value{ Uint64Value: 123, }, }, - }, - "$d": { - Type: &Ydb.Type{ - Type: &Ydb.Type_ListType{ - ListType: &Ydb.ListType{ - Item: &Ydb.Type{ - Type: &Ydb.Type_TypeId{ - TypeId: Ydb.Type_UINT64, - }, - }, - }, + { + Value: &Ydb.Value_Uint64Value{ + Uint64Value: 123, }, }, - Value: &Ydb.Value{ - Items: []*Ydb.Value{ - { - Value: &Ydb.Value_Uint64Value{ - Uint64Value: 123, - }, - }, - { - Value: &Ydb.Value_Uint64Value{ - Uint64Value: 123, - }, - }, - { - Value: &Ydb.Value_Uint64Value{ - Uint64Value: 123, - }, - }, - { - Value: &Ydb.Value_Uint64Value{ - Uint64Value: 123, - }, - }, + { + Value: &Ydb.Value_Uint64Value{ + Uint64Value: 123, }, }, }, - }), - fmt.Sprintf("%v", v), - ) - a.Free() + }, + } +) + +func makeParamsUsingParamsBuilder(tb testing.TB) params.Parameters { + return ydb.ParamsBuilder(). + Param("$a").Uint64(123). + Param("$b").Uuid(uuid.UUID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}). + Param("$c").BeginOptional().Uint64(func(v uint64) *uint64 { return &v }(123)).EndOptional(). + Param("$d").BeginList().Add().Uint64(123).Add().Uint64(123).Add().Uint64(123).Add().Uint64(123).EndList(). + Build() } -func BenchmarkParamsBuilder(b *testing.B) { - b.ReportAllocs() - for i := 0; i < b.N; i++ { - params := makeParamsUsingBuilder(b) - a := allocator.New() - _ = params.ToYDB(a) - a.Free() - } +func makeParamsUsingRawProtobuf(tb testing.TB) params.Parameters { + return ydb.ParamsBuilder(). + Param("$a").Raw(a). + Param("$b").Raw(b). + Param("$c").Raw(c). + Param("$d").Raw(d). + Build() } -func makeParamsUsingParamsMap(tb testing.TB) *params.Parameters { - params, err := ydb.ParamsFromMap(map[string]any{ +func makeParamsUsingParamsFromMap(tb testing.TB) params.Parameters { + return ydb.ParamsFromMap(map[string]any{ "$a": uint64(123), "$b": uuid.UUID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}, "$c": func(v uint64) *uint64 { return &v }(123), "$d": []uint64{123, 123, 123, 123}, }) - require.NoError(tb, err) - - return params } -func TestParamsMap(t *testing.T) { - params := makeParamsUsingParamsMap(t) - a := allocator.New() - v := params.ToYDB(a) - require.Equal(t, - fmt.Sprintf("%v", map[string]*Ydb.TypedValue{ - "$a": { - Type: &Ydb.Type{ - Type: &Ydb.Type_TypeId{ - TypeId: Ydb.Type_UINT64, - }, - }, - Value: &Ydb.Value{ - Value: &Ydb.Value_Uint64Value{ - Uint64Value: 123, - }, - }, - }, - "$b": { - Type: &Ydb.Type{ - Type: &Ydb.Type_TypeId{ - TypeId: Ydb.Type_UUID, - }, - }, - Value: &Ydb.Value{ - Value: &Ydb.Value_Low_128{ - Low_128: 506660481424032516, - }, - High_128: 1157159078456920585, - }, - }, - "$c": { - Type: &Ydb.Type{ - Type: &Ydb.Type_OptionalType{ - OptionalType: &Ydb.OptionalType{ - Item: &Ydb.Type{ - Type: &Ydb.Type_TypeId{ - TypeId: Ydb.Type_UINT64, - }, - }, - }, - }, - }, - Value: &Ydb.Value{ - Value: &Ydb.Value_Uint64Value{ - Uint64Value: 123, - }, - }, - }, - "$d": { - Type: &Ydb.Type{ - Type: &Ydb.Type_ListType{ - ListType: &Ydb.ListType{ - Item: &Ydb.Type{ - Type: &Ydb.Type_TypeId{ - TypeId: Ydb.Type_UINT64, - }, - }, - }, - }, - }, - Value: &Ydb.Value{ - Items: []*Ydb.Value{ - { - Value: &Ydb.Value_Uint64Value{ - Uint64Value: 123, - }, - }, - { - Value: &Ydb.Value_Uint64Value{ - Uint64Value: 123, - }, - }, - { - Value: &Ydb.Value_Uint64Value{ - Uint64Value: 123, - }, - }, - { - Value: &Ydb.Value_Uint64Value{ - Uint64Value: 123, - }, - }, - }, - }, - }, - }), - fmt.Sprintf("%v", v), - ) - a.Free() -} - -func BenchmarkParamsMap(b *testing.B) { - b.ReportAllocs() - for i := 0; i < b.N; i++ { - params := makeParamsUsingParamsMap(b) - a := allocator.New() - _ = params.ToYDB(a) - a.Free() - } -} - -func makeParamsUsingTypes(tb testing.TB) *params.Parameters { +func makeParamsUsingTableTypes(tb testing.TB) params.Parameters { return table.NewQueryParameters( table.ValueParam("$a", types.Uint64Value(123)), table.ValueParam("$b", types.UuidValue(uuid.UUID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16})), @@ -254,104 +139,82 @@ func makeParamsUsingTypes(tb testing.TB) *params.Parameters { ) } -func TestParamsFromTypes(t *testing.T) { - params := makeParamsUsingTypes(t) - a := allocator.New() - v := params.ToYDB(a) - require.Equal(t, - fmt.Sprintf("%v", map[string]*Ydb.TypedValue{ - "$a": { - Type: &Ydb.Type{ - Type: &Ydb.Type_TypeId{ - TypeId: Ydb.Type_UINT64, - }, - }, - Value: &Ydb.Value{ - Value: &Ydb.Value_Uint64Value{ - Uint64Value: 123, - }, - }, - }, - "$b": { - Type: &Ydb.Type{ - Type: &Ydb.Type_TypeId{ - TypeId: Ydb.Type_UUID, - }, - }, - Value: &Ydb.Value{ - Value: &Ydb.Value_Low_128{ - Low_128: 506660481424032516, - }, - High_128: 1157159078456920585, - }, - }, - "$c": { - Type: &Ydb.Type{ - Type: &Ydb.Type_OptionalType{ - OptionalType: &Ydb.OptionalType{ - Item: &Ydb.Type{ - Type: &Ydb.Type_TypeId{ - TypeId: Ydb.Type_UINT64, - }, - }, - }, - }, - }, - Value: &Ydb.Value{ - Value: &Ydb.Value_Uint64Value{ - Uint64Value: 123, - }, - }, - }, - "$d": { - Type: &Ydb.Type{ - Type: &Ydb.Type_ListType{ - ListType: &Ydb.ListType{ - Item: &Ydb.Type{ - Type: &Ydb.Type_TypeId{ - TypeId: Ydb.Type_UINT64, - }, - }, - }, - }, - }, - Value: &Ydb.Value{ - Items: []*Ydb.Value{ - { - Value: &Ydb.Value_Uint64Value{ - Uint64Value: 123, - }, - }, - { - Value: &Ydb.Value_Uint64Value{ - Uint64Value: 123, - }, - }, - { - Value: &Ydb.Value_Uint64Value{ - Uint64Value: 123, - }, - }, - { - Value: &Ydb.Value_Uint64Value{ - Uint64Value: 123, - }, - }, - }, - }, - }, - }), - fmt.Sprintf("%v", v), - ) - a.Free() -} - -func BenchmarkParamsFromTypes(b *testing.B) { - b.ReportAllocs() - for i := 0; i < b.N; i++ { - params := makeParamsUsingTypes(b) +func TestParams(t *testing.T) { + exp := map[string]*Ydb.TypedValue{ + "$a": a, + "$b": b, + "$c": c, + "$d": d, + } + t.Run("ParamsBuilder", func(t *testing.T) { + params := makeParamsUsingParamsBuilder(t) a := allocator.New() - _ = params.ToYDB(a) + pb, err := params.ToYDB(a) + require.NoError(t, err) + require.Equal(t, fmt.Sprint(exp), fmt.Sprint(pb)) a.Free() - } + t.Run("Raw", func(t *testing.T) { + params := makeParamsUsingRawProtobuf(t) + a := allocator.New() + pb, err := params.ToYDB(a) + require.NoError(t, err) + require.Equal(t, fmt.Sprint(exp), fmt.Sprint(pb)) + a.Free() + }) + }) + t.Run("ParamsFromMap", func(t *testing.T) { + params := makeParamsUsingParamsFromMap(t) + a := allocator.New() + pb, err := params.ToYDB(a) + require.NoError(t, err) + require.Equal(t, fmt.Sprint(exp), fmt.Sprint(pb)) + a.Free() + }) + t.Run("table/types", func(t *testing.T) { + params := makeParamsUsingTableTypes(t) + a := allocator.New() + pb, err := params.ToYDB(a) + require.NoError(t, err) + require.Equal(t, fmt.Sprint(exp), fmt.Sprint(pb)) + a.Free() + }) +} + +func BenchmarkParams(b *testing.B) { + b.Run("ParamsBuilder", func(b *testing.B) { + b.ReportAllocs() + for i := 0; i < b.N; i++ { + params := makeParamsUsingParamsBuilder(b) + a := allocator.New() + _, _ = params.ToYDB(a) + a.Free() + } + }) + b.Run("RawProtobuf", func(b *testing.B) { + b.ReportAllocs() + for i := 0; i < b.N; i++ { + params := makeParamsUsingRawProtobuf(b) + a := allocator.New() + _, _ = params.ToYDB(a) + a.Free() + } + }) + b.Run("ParamsFromMap", func(b *testing.B) { + b.ReportAllocs() + for i := 0; i < b.N; i++ { + params := makeParamsUsingParamsFromMap(b) + a := allocator.New() + _, _ = params.ToYDB(a) + a.Free() + } + }) + b.Run("table/types", func(b *testing.B) { + b.ReportAllocs() + for i := 0; i < b.N; i++ { + params := makeParamsUsingTableTypes(b) + a := allocator.New() + _, _ = params.ToYDB(a) + a.Free() + } + }) }