-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
See: <golang/go#26106> time.LoadLocation shall not be called on a hot path, since it's do a lot of syscalls and actually read files every time, despite this file is a same one. This package introduce a naive static cache, locales by the nature of it very static, so added up a cache layer before it. Before: ``` BenchmarkReceiverTest BenchmarkReceiverTest/parse BenchmarkReceiverTest/parse/tz BenchmarkReceiverTest/parse/tz-10 37561 30858 ns/op 12645 B/op 77 allocs/op BenchmarkReceiverTest/parse/no-tz BenchmarkReceiverTest/parse/no-tz-10 58916 20015 ns/op 8963 B/op 53 allocs/op PASS ``` After: ``` BenchmarkReceiverTest/parse BenchmarkReceiverTest/parse/tz BenchmarkReceiverTest/parse/tz-10 58303 20599 ns/op 8964 B/op 53 allocs/op BenchmarkReceiverTest/parse/no-tz BenchmarkReceiverTest/parse/no-tz-10 59144 19973 ns/op 8963 B/op 53 allocs/op ``` commit_hash:abe8a3155d7a10363e2dc14ace42e81a72d268fd
- Loading branch information
1 parent
fb18d20
commit a996331
Showing
8 changed files
with
133 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package tests | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/doublecloud/transfer/internal/logger" | ||
"github.com/doublecloud/transfer/pkg/abstract" | ||
"github.com/doublecloud/transfer/pkg/debezium" | ||
debeziumcommon "github.com/doublecloud/transfer/pkg/debezium/common" | ||
debeziumparameters "github.com/doublecloud/transfer/pkg/debezium/parameters" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
func BenchmarkReceiverTest(b *testing.B) { | ||
b.Run("parse", func(b *testing.B) { | ||
b.Run("tz", func(b *testing.B) { | ||
canonDebeziumMsgWithoutSequence := wipeSequenceAndIncremental(debeziumMsg30) | ||
receiver := debezium.NewReceiver(map[abstract.TableID]map[string]*debeziumcommon.OriginalTypeInfo{ | ||
{Namespace: "public", Name: "basic_types"}: { | ||
"id": {OriginalType: "pg:integer"}, | ||
"val": {OriginalType: `pg:timestamp without time zone`, Properties: map[string]string{"timezone": "Europe/Moscow"}}, | ||
}, | ||
}, nil) | ||
|
||
b.ResetTimer() | ||
for n := 0; n < b.N; n++ { | ||
co, err := receiver.Receive(canonDebeziumMsgWithoutSequence) | ||
require.NoError(b, err) | ||
b.SetBytes(int64(co.Size.Values)) | ||
} | ||
b.ReportAllocs() | ||
}) | ||
b.Run("no-tz", func(b *testing.B) { | ||
canonDebeziumMsgWithoutSequence := wipeSequenceAndIncremental(debeziumMsg30) | ||
receiver := debezium.NewReceiver(map[abstract.TableID]map[string]*debeziumcommon.OriginalTypeInfo{ | ||
{Namespace: "public", Name: "basic_types"}: { | ||
"id": {OriginalType: "pg:integer"}, | ||
"val": {OriginalType: `pg:timestamp without time zone`}, | ||
}, | ||
}, nil) | ||
|
||
b.ResetTimer() | ||
for n := 0; n < b.N; n++ { | ||
co, err := receiver.Receive(canonDebeziumMsgWithoutSequence) | ||
require.NoError(b, err) | ||
b.SetBytes(int64(co.Size.Values)) | ||
} | ||
b.ReportAllocs() | ||
}) | ||
}) | ||
b.Run("serialize", func(b *testing.B) { | ||
changeItem := extractCI(b, debeziumMsg31, map[abstract.TableID]map[string]*debeziumcommon.OriginalTypeInfo{ | ||
{Namespace: "public", Name: "basic_types"}: { | ||
"id": {OriginalType: "pg:integer"}, | ||
"val": {OriginalType: `pg:timestamp with time zone`}, | ||
}, | ||
}) | ||
emitter, err := debezium.NewMessagesEmitter(map[string]string{ | ||
debeziumparameters.DatabaseDBName: "pguser", | ||
debeziumparameters.TopicPrefix: "fullfillment", | ||
debeziumparameters.AddOriginalTypes: "false", | ||
debeziumparameters.SourceType: "pg", | ||
}, "1.8.0.Final", false, logger.LoggerWithLevel(zapcore.WarnLevel)) | ||
require.NoError(b, err) | ||
|
||
b.ResetTimer() | ||
for n := 0; n < b.N; n++ { | ||
result, err := emitter.EmitKV(&changeItem, debezium.GetPayloadTSMS(&changeItem), false, nil) | ||
require.NoError(b, err) | ||
require.Equal(b, len(result), 1) | ||
b.SetBytes(int64(len(*result[0].DebeziumVal))) | ||
} | ||
b.ReportAllocs() | ||
}) | ||
} | ||
|
||
func extractCI( | ||
t require.TestingT, | ||
debeziumMsg string, | ||
originalTypes map[abstract.TableID]map[string]*debeziumcommon.OriginalTypeInfo, | ||
) abstract.ChangeItem { | ||
canonDebeziumMsgWithoutSequence := wipeSequenceAndIncremental(debeziumMsg) | ||
receiver := debezium.NewReceiver(originalTypes, nil) | ||
changeItemStr, err := ReceiveStr(receiver, canonDebeziumMsgWithoutSequence) | ||
require.NoError(t, err) | ||
changeItem, err := abstract.UnmarshalChangeItem([]byte(changeItemStr)) | ||
require.NoError(t, err) | ||
return *changeItem | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package xlocale | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
) | ||
|
||
var ( | ||
locationCache sync.Map | ||
cacheMutex sync.Mutex | ||
) | ||
|
||
func Load(name string) (*time.Location, error) { | ||
if entry, ok := locationCache.Load(name); ok { | ||
cacheEntry := entry.(*time.Location) | ||
return cacheEntry, nil | ||
} | ||
|
||
loc, err := time.LoadLocation(name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
cacheMutex.Lock() | ||
locationCache.Store(name, loc) | ||
cacheMutex.Unlock() | ||
|
||
return loc, nil | ||
} |