Skip to content

Commit

Permalink
⚡️ Add matching topic to strictMappings
Browse files Browse the repository at this point in the history
Signed-off-by: Paul MARS <paul.mars@intrinsec.com>
  • Loading branch information
Paul MARS authored and upils committed Aug 14, 2023
1 parent d3a91b3 commit c59f665
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 17 deletions.
44 changes: 27 additions & 17 deletions backend/pkg/proto/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,23 +112,9 @@ func NewService(cfg config.Proto, logger *zap.Logger, schemaSvc *schema.Service)
}
}

strictMappingsByTopic := make(map[string]config.ProtoTopicMapping)
regexMappingsByTopic := make(map[string]RegexProtoTopicMapping)

for _, mapping := range cfg.Mappings {
if mapping.IsRegex {
r, err := regexp.Compile(mapping.TopicName)
if err != nil {
return nil, fmt.Errorf("invalid regexp as a topic name: %w", err)
}

regexMappingsByTopic[mapping.TopicName] = RegexProtoTopicMapping{
ProtoTopicMapping: mapping,
r: r,
}
continue
}
strictMappingsByTopic[mapping.TopicName] = mapping
strictMappingsByTopic, regexMappingsByTopic, err := setMappingsByTopic(cfg.Mappings)
if err != nil {
return nil, err
}

return &Service{
Expand All @@ -146,6 +132,29 @@ func NewService(cfg config.Proto, logger *zap.Logger, schemaSvc *schema.Service)
}, nil
}

func setMappingsByTopic(mappings []config.ProtoTopicMapping) (strictMappingsByTopic map[string]config.ProtoTopicMapping, regexMappingsByTopic map[string]RegexProtoTopicMapping, err error) {
strictMappingsByTopic = make(map[string]config.ProtoTopicMapping)
regexMappingsByTopic = make(map[string]RegexProtoTopicMapping)

for _, mapping := range mappings {
if mapping.IsRegex {
r, err := regexp.Compile(mapping.TopicName)
if err != nil {
return nil, nil, fmt.Errorf("invalid regexp as a topic name: %w", err)
}

regexMappingsByTopic[mapping.TopicName] = RegexProtoTopicMapping{
ProtoTopicMapping: mapping,
r: r,
}
continue
}
strictMappingsByTopic[mapping.TopicName] = mapping
}

return
}

// Start polling the prototypes from the configured provider (e.g. filesystem or Git) and sync these
// into our in-memory prototype registry.
func (s *Service) Start() error {
Expand Down Expand Up @@ -285,6 +294,7 @@ func (s *Service) getMatchingMapping(topicName string) (mapping config.ProtoTopi
match = rMapping.r.MatchString(topicName)
if match {
mapping = rMapping.ProtoTopicMapping
s.strictMappingsByTopic[topicName] = mapping
break
}
}
Expand Down
82 changes: 82 additions & 0 deletions backend/pkg/proto/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package proto
import (
"bytes"
"encoding/binary"
"fmt"
"reflect"
"regexp"
"testing"
Expand Down Expand Up @@ -61,6 +62,18 @@ var (
}
)

func genNTopicMappings(n int, baseName string, isRegex bool) []config.ProtoTopicMapping {
m := make([]config.ProtoTopicMapping, 0)
for i := 0; i < n; i++ {
m = append(m, config.ProtoTopicMapping{
TopicName: fmt.Sprintf("%v%v", baseName, i),
IsRegex: isRegex,
})
}

return m
}

func TestService_getMatchingMapping(t *testing.T) {
type fields struct {
strictMappingsByTopic map[string]config.ProtoTopicMapping
Expand Down Expand Up @@ -127,3 +140,72 @@ func TestService_getMatchingMapping(t *testing.T) {
})
}
}

func BenchmarkService_getMatchingMapping(b *testing.B) {
benchs := []struct {
name string
baseName string
topicCount int
iter int
ratio float32 // must be between 0 and 1
}{
{
name: "Only strict mappings",
baseName: "strictMapping",
topicCount: 100,
iter: 100,
ratio: 0.0,
},
{
name: "10% regex mappings",
baseName: "regexMapping",
topicCount: 100,
iter: 100,
ratio: 0.1,
},
{
name: "50% regex mappings",
baseName: "regexMapping",
topicCount: 100,
iter: 100,
ratio: 0.5,
},
{
name: "90% regex mappings",
baseName: "regexMapping",
topicCount: 100,
iter: 100,
ratio: 1.0,
},
}
for _, bench := range benchs {
b.Run(bench.name, func(b *testing.B) {

strictTopicMappings := genNTopicMappings(int(float32(bench.topicCount)*(1.0-bench.ratio)), "strictMapping", false)
regexTopicMappings := genNTopicMappings(int(float32(bench.topicCount)*bench.ratio), "regexMapping", true)

strictMappingsByTopic, regexMappingsByTopic, err := setMappingsByTopic(append(strictTopicMappings, regexTopicMappings...))
if err != nil {
b.Error(err)
}

topicNames := make([]string, 0)

for i := 0; i < bench.topicCount; i++ {
topicNames = append(topicNames, fmt.Sprintf("%v%v", bench.baseName, i))
}

for i := 0; i < b.N; i++ {
s := &Service{
strictMappingsByTopic: strictMappingsByTopic,
regexMappingsByTopic: regexMappingsByTopic,
}
for n := 0; n < bench.iter; n++ {
for _, topicName := range topicNames {
_, _ = s.getMatchingMapping(topicName)
}
}
}
})
}
}

0 comments on commit c59f665

Please sign in to comment.