Skip to content

Commit

Permalink
Limit CPU usage
Browse files Browse the repository at this point in the history
  • Loading branch information
ajardin committed May 12, 2019
1 parent 5c2481e commit 93b37c9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
27 changes: 18 additions & 9 deletions internal/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,31 @@ func Analyze() {
raw := getRawStatistics()
extractContributors(raw)

var wg sync.WaitGroup

if len(contributors) > 0 {
for _, author := range contributors {
tasks := make(chan *Contributor)
var wg sync.WaitGroup

workersLimit := getMaxParallelism()
for worker := 0; worker < workersLimit; worker++ {
wg.Add(1)

go func(author *Contributor) {
go func() {
defer wg.Done()

loadContributorStats(author)
loadContributorActivity(author)
}(author)
for author := range tasks {
loadContributorStats(author)
loadContributorActivity(author)
}
}()
}
}

wg.Wait()
for _, author := range contributors {
tasks <- author
}

close(tasks)
wg.Wait()
}

deduplicate()
render()
Expand Down
13 changes: 13 additions & 0 deletions internal/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"regexp"
"runtime"

"github.com/olekukonko/tablewriter"
)
Expand All @@ -15,6 +16,18 @@ func check(e error) {
}
}

// getMaxParallelism retrieves the maximum number of concurrent workers.
func getMaxParallelism() int {
maxProcs := runtime.GOMAXPROCS(0)
numCPU := runtime.NumCPU()

if maxProcs < numCPU {
return maxProcs
} else {
return numCPU
}
}

// getRegexResults retrieves regular expression results with namespaces.
func getRegexResults(str string, reg *regexp.Regexp) map[string]string {
result := make(map[string]string)
Expand Down

0 comments on commit 93b37c9

Please sign in to comment.