Limit the amount of goroutnes started
This commit is contained in:
30
main.go
30
main.go
@@ -8,6 +8,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
@@ -40,8 +41,11 @@ func main() {
|
|||||||
to := flag.String("to", ".jpg", "Extension to transcode to")
|
to := flag.String("to", ".jpg", "Extension to transcode to")
|
||||||
nosafe := flag.Bool("nosafe", false, "Prevents backup of the original before encoding")
|
nosafe := flag.Bool("nosafe", false, "Prevents backup of the original before encoding")
|
||||||
rm := flag.Bool("rm", false, "Removes the original after transcoding")
|
rm := flag.Bool("rm", false, "Removes the original after transcoding")
|
||||||
|
w := flag.Int("w", 8, "Workers")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
|
workers := make(chan struct{}, *w)
|
||||||
|
|
||||||
if _, ok := codecs[*to]; !ok {
|
if _, ok := codecs[*to]; !ok {
|
||||||
Error.Println("Invalid extension")
|
Error.Println("Invalid extension")
|
||||||
return
|
return
|
||||||
@@ -55,7 +59,9 @@ func main() {
|
|||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
for _, file := range flag.Args() {
|
for _, file := range flag.Args() {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
|
workers <- struct{}{}
|
||||||
go func(file string) {
|
go func(file string) {
|
||||||
|
defer func() { <-workers }()
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
file = filepath.ToSlash(file)
|
file = filepath.ToSlash(file)
|
||||||
file = filepath.Clean(file)
|
file = filepath.Clean(file)
|
||||||
@@ -138,3 +144,27 @@ func main() {
|
|||||||
}
|
}
|
||||||
wg.Wait()
|
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