Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Sep 7, 2023
1 parent d642021 commit 231ec67
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
14 changes: 8 additions & 6 deletions storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,20 @@ type entry struct {

type _storage map[*cobra.Command]*entry

var storageMutex sync.Mutex
var storageMutex sync.RWMutex

func (s _storage) get(cmd *cobra.Command) (e *entry) {
storageMutex.RLock()

var ok bool
if e, ok = s[cmd]; !ok {
storageMutex.RUnlock()
storageMutex.Lock()
defer storageMutex.Unlock()

if e, ok = s[cmd]; !ok {
e = &entry{}
s[cmd] = e
}
e = &entry{}
s[cmd] = e
} else {
storageMutex.RUnlock()
}
return
}
Expand Down
24 changes: 13 additions & 11 deletions storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,22 @@ func TestCheck(t *testing.T) {
}
}

// BenchmarkStorage test for concurrent map read/write
// BenchmarkStorage tests for concurrent map read/write
func BenchmarkStorage(b *testing.B) {
cmd := &cobra.Command{}
cmd2 := &cobra.Command{}
b.RunParallel(func(p *testing.PB) {
Gen(cmd).FlagCompletion(ActionMap{
"flag1": ActionValues("a", "b"),
})
Gen(cmd).PositionalCompletion(ActionValues("a", "b"))

Gen(cmd2).FlagCompletion(ActionMap{
"flag2": ActionValues("a", "b"),
})
Gen(cmd).PositionalCompletion(ActionValues("a", "b"))
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
// Gen(cmd).FlagCompletion(ActionMap{
// "flag1": ActionValues("a", "b"),
// })
Gen(cmd).PositionalCompletion(ActionValues("a", "b"))

// Gen(cmd2).FlagCompletion(ActionMap{
// "flag2": ActionValues("a", "b"),
// })
Gen(cmd2).PositionalCompletion(ActionValues("a", "b"))
}
})

}

0 comments on commit 231ec67

Please sign in to comment.