Skip to content

Commit

Permalink
testing concorrency
Browse files Browse the repository at this point in the history
  • Loading branch information
thorstendb-ARM committed Aug 31, 2023
1 parent 0e98b58 commit d6b7923
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
46 changes: 20 additions & 26 deletions cmd/installer/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,20 @@ func massDownloadPdscFiles(pdscTag xml.PdscTag, skipInstalledPdscFiles bool, tim
}
}

func CheckConcurrency(concurrency int) int {
maxWorkers := runtime.GOMAXPROCS(0)

if concurrency > 1 {
if concurrency > maxWorkers {
concurrency = maxWorkers
}
} else {
concurrency = 0
}

return concurrency
}

func DownloadPDSCFiles(skipInstalledPdscFiles bool, concurrency int, timeout int) error {
log.Info("Downloading all PDSC files available on the public index")
if err := Installation.PublicIndexXML.Read(); err != nil {
Expand All @@ -309,18 +323,8 @@ func DownloadPDSCFiles(skipInstalledPdscFiles bool, concurrency int, timeout int
}

ctx := context.TODO()
maxWorkers := runtime.GOMAXPROCS(0)

if concurrency > 1 {
if maxWorkers > concurrency {
maxWorkers = concurrency
}
if maxWorkers == 0 {
concurrency = 0
}
}

sem := semaphore.NewWeighted(int64(maxWorkers))
concurrency = CheckConcurrency(concurrency)
sem := semaphore.NewWeighted(int64(concurrency))

for _, pdscTag := range pdscTags {
if concurrency == 0 {
Expand All @@ -338,7 +342,7 @@ func DownloadPDSCFiles(skipInstalledPdscFiles bool, concurrency int, timeout int
}
}
if concurrency > 1 {
if err := sem.Acquire(ctx, int64(maxWorkers)); err != nil {
if err := sem.Acquire(ctx, int64(concurrency)); err != nil {
log.Errorf("Failed to acquire semaphore: %v", err)
}
}
Expand All @@ -354,18 +358,8 @@ func UpdateInstalledPDSCFiles(pidxXML *xml.PidxXML, concurrency int, timeout int
}

ctx := context.TODO()
maxWorkers := runtime.GOMAXPROCS(0)

if concurrency > 1 {
if maxWorkers > concurrency {
maxWorkers = concurrency
}
if maxWorkers == 0 {
concurrency = 0
}
}

sem := semaphore.NewWeighted(int64(maxWorkers))
concurrency = CheckConcurrency(concurrency)
sem := semaphore.NewWeighted(int64(concurrency))

for _, pdscFile := range pdscFiles {
log.Debugf("Checking if \"%s\" needs updating", pdscFile)
Expand Down Expand Up @@ -415,7 +409,7 @@ func UpdateInstalledPDSCFiles(pidxXML *xml.PidxXML, concurrency int, timeout int
}

if concurrency > 1 {
if err := sem.Acquire(ctx, int64(maxWorkers)); err != nil {
if err := sem.Acquire(ctx, int64(concurrency)); err != nil {
log.Errorf("Failed to acquire semaphore: %v", err)
}
}
Expand Down
5 changes: 5 additions & 0 deletions cmd/installer/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,11 @@ func TestUpdatePublicIndex(t *testing.T) {
assert.Equal(copied, indexContent)
})

t.Run("test check concurrency function call", func(t *testing.T) {
assert.Equal(installer.CheckConcurrency(0), 0)
assert.Equal(installer.CheckConcurrency(5), 5)
})

t.Run("test add remote index.pidx and dowload pdsc files", func(t *testing.T) {
localTestingDir := "test-add-remote-index-download-pdsc"
assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot))
Expand Down

0 comments on commit d6b7923

Please sign in to comment.