Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
wg := &sync.WaitGroup{}
ch := make(chan struct{})
for i := 0; i < 100; i++ {
wg.Add(1)
go func(ch chan struct{}) {
defer wg.Done()
for _ = range ch {
r, err := c.SayHello(context.Background(), &pb.HelloRequest{Name: name})
if err != nil {
log.Fatalf("could not greet: %v", err)
}
log.Printf("Greeting: %s", r.Message)
}
}(ch)
}
numJobs := 10000
start := time.Now()
for i := 0; i < numJobs; i++ {
ch <- struct{}{}
}
close(ch)
fmt.Printf("Waiting for workers...\n")
wg.Wait()
elapsed := time.Since(start)
fmt.Printf("%d jobs in %0.2f seconds (%0.2f jobs/sec)\n", numJobs, float64(elapsed)/float64(time.Second), float64(numJobs)/(float64(elapsed)/float64(time.Second)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.