Skip to content

Commit

Permalink
chore: refine add benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
crimson-gao committed Dec 6, 2024
1 parent d892373 commit 449af35
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
4 changes: 2 additions & 2 deletions producer/pack_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func newPackIdGenerator() *PackIdGenerator {
func (g *PackIdGenerator) GeneratePackId(project, logstore string) string {
key := project + "|" + logstore

// fast path, logstore already has a counter
// fast path, logstore already has a generator
g.mutex.RLock()
if l, ok := g.logstorePackIdGenerator[key]; ok {
packNumber := l.packNumber.Add(1)
Expand All @@ -34,12 +34,12 @@ func (g *PackIdGenerator) GeneratePackId(project, logstore string) string {

// slow path
g.mutex.Lock()
defer g.mutex.Unlock()
if _, ok := g.logstorePackIdGenerator[key]; !ok {
g.logstorePackIdGenerator[key] = newLogStorePackIdGenerator(g.count.Add(1))
}
l := g.logstorePackIdGenerator[key]
packNumber := l.packNumber.Add(1)
g.mutex.Unlock()
return fmt.Sprintf("%s%X", l.prefix, packNumber-1)
}

Expand Down
7 changes: 7 additions & 0 deletions producer/pack_id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,12 @@ func TestPackIdGenerator(t *testing.T) {
}(i)
}
wg.Wait()
}

// BenchmarkPackIdGenerator-12 8366719 120.7 ns/op 64 B/op 4 allocs/op
func BenchmarkPackIdGenerator(b *testing.B) {
g := newPackIdGenerator()
for i := 0; i < b.N; i++ {
g.GeneratePackId("test", "test")
}
}

0 comments on commit 449af35

Please sign in to comment.