mirror of
https://github.com/lxc/incus.git
synced 2026-02-05 09:46:19 +01:00
Using more modern features of Go, such as: - conditional assignment -> built-in min or max in go1.21, - sort.Slice -> slices.Sort in go1.21, - loop assign map -> maps.Copy in go1.21, - []byte(fmt.Sprintf...) -> fmt.Appendf(nil,...) in go1.19, - strings.HasPrefix / strings.TrimPrefix -> strings.CutPrefix in go1.20 Signed-off-by: JUN JIE NAN <nanjunjie@gmail.com>
63 lines
1.2 KiB
Go
63 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
func getBatchSize(parallel int) (int, error) {
|
|
batchSize := parallel
|
|
if batchSize < 1 {
|
|
// Detect the number of parallel actions
|
|
cpus, err := os.ReadDir("/sys/bus/cpu/devices")
|
|
if err != nil {
|
|
return -1, err
|
|
}
|
|
|
|
batchSize = len(cpus)
|
|
}
|
|
|
|
return batchSize, nil
|
|
}
|
|
|
|
func processBatch(count int, batchSize int, process func(index int, wg *sync.WaitGroup)) time.Duration {
|
|
batches := count / batchSize
|
|
remainder := count % batchSize
|
|
processed := 0
|
|
wg := sync.WaitGroup{}
|
|
nextStat := batchSize
|
|
|
|
logf("Batch processing start")
|
|
timeStart := time.Now()
|
|
|
|
for range batches {
|
|
for range batchSize {
|
|
wg.Add(1)
|
|
go process(processed, &wg)
|
|
processed++
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
if processed >= nextStat {
|
|
interval := time.Since(timeStart).Seconds()
|
|
logf("Processed %d containers in %.3fs (%.3f/s)", processed, interval, float64(processed)/interval)
|
|
nextStat = nextStat * 2
|
|
}
|
|
}
|
|
|
|
for range remainder {
|
|
wg.Add(1)
|
|
go process(processed, &wg)
|
|
processed++
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
timeEnd := time.Now()
|
|
duration := timeEnd.Sub(timeStart)
|
|
logf("Batch processing completed in %.3fs", duration.Seconds())
|
|
return duration
|
|
}
|