Limit the amount of goroutnes started
This commit is contained in:
30
main.go
30
main.go
@@ -8,6 +8,7 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
@@ -40,7 +41,10 @@ func main() {
|
||||
to := flag.String("to", ".jpg", "Extension to transcode to")
|
||||
nosafe := flag.Bool("nosafe", false, "Prevents backup of the original before encoding")
|
||||
rm := flag.Bool("rm", false, "Removes the original after transcoding")
|
||||
w := flag.Int("w", 8, "Workers")
|
||||
flag.Parse()
|
||||
|
||||
workers := make(chan struct{}, *w)
|
||||
|
||||
if _, ok := codecs[*to]; !ok {
|
||||
Error.Println("Invalid extension")
|
||||
@@ -55,7 +59,9 @@ func main() {
|
||||
var wg sync.WaitGroup
|
||||
for _, file := range flag.Args() {
|
||||
wg.Add(1)
|
||||
workers <- struct{}{}
|
||||
go func(file string) {
|
||||
defer func() { <-workers }()
|
||||
defer wg.Done()
|
||||
file = filepath.ToSlash(file)
|
||||
file = filepath.Clean(file)
|
||||
@@ -138,3 +144,27 @@ func main() {
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func instrument() {
|
||||
numGoroutines := runtime.NumGoroutine()
|
||||
|
||||
var m runtime.MemStats
|
||||
runtime.ReadMemStats(&m)
|
||||
// log.Printf("%+v", m)
|
||||
|
||||
sys := float64(m.Sys)
|
||||
ramUsedMB := sys / 1024 / 1024
|
||||
kbPerGoroutine := sys / 1024 / float64(numGoroutines)
|
||||
|
||||
var numGoroutinesPretty string
|
||||
switch {
|
||||
case numGoroutines >= 1_000_000:
|
||||
numGoroutinesPretty = fmt.Sprintf("%.2fM", float64(numGoroutines)/1_000_000)
|
||||
case numGoroutines >= 1_000:
|
||||
numGoroutinesPretty = fmt.Sprintf("%.2fk", float64(numGoroutines)/1_000)
|
||||
default:
|
||||
numGoroutinesPretty = fmt.Sprintf("%d", numGoroutines)
|
||||
}
|
||||
|
||||
fmt.Printf("\rNumber of active goroutines: %d (%s); RAM used: %.2f MB; KB per goroutine: %.2f", numGoroutines, numGoroutinesPretty, ramUsedMB, kbPerGoroutine)
|
||||
}
|
Reference in New Issue
Block a user