package main
import (
"flag"
"fmt"
"net"
"runtime"
"runtime/debug"
"sync"
"time"
)
func main() {
numThreads := flag.Int("threads", 10, "number of threads (in addition to GOMAXPROCS)")
parallelism := flag.Int("parallel", 100, "number of parallel goroutines to start")
flag.Parse()
maxThreads := runtime.GOMAXPROCS(-1) + *numThreads
fmt.Printf("GOMAXPROCS=%d, max threads=%d\n", runtime.GOMAXPROCS(-1), maxThreads)
debug.SetMaxThreads(maxThreads)
// Server that does not accept any connections
if err != nil {
fmt.Println(err)
return
}
defer listener.Close()
wg := sync.WaitGroup{}
startSignal := make(chan struct{})
// Spawn all goroutines
for i := 0; i < *parallelism; i++ {
wg.Add(1)
go func(id int) {
defer wg.Done()
<-startSignal
if err != nil {
fmt.Printf("%d: error: %s\n", id, err)
return
}
defer conn.Close()
time.Sleep(time.Second)
}(i)
}
time.Sleep(time.Second)
// Start them all at once
close(startSignal)
wg.Wait()
}