Skip to content

Instantly share code, notes, and snippets.

@muhlemmer
Created January 31, 2020 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muhlemmer/6be45df7b325d47c70a96bb23ce85c3e to your computer and use it in GitHub Desktop.
Save muhlemmer/6be45df7b325d47c70a96bb23ce85c3e to your computer and use it in GitHub Desktop.
A small example used for presentations on Go routines.
package main
/*
Run with:
go run main.go
Flags:
-number int
Number of requests (default 10)
-wait int
Max wait for requests (default 100)
*/
import (
"flag"
"fmt"
"log"
"math/rand"
"sync"
"time"
)
// Request simulates an I/O operation by randomly waiting up to `w` milliseconds
func Request(w int64) string {
d := time.Duration(rand.Int63n(w)) * time.Millisecond
time.Sleep(d)
return fmt.Sprintf("Blocked for %v", d)
}
// SerialWorkers runs `n` amount of requests and logs the outputs.
// When all requests are done, it logs the total time taken.
// `w` is the maximum random wait time passed to each request.
func SerialWorkers(n int, w int64) {
s := time.Now()
for i := 0; i < n; i++ {
log.Printf("Req #%02d done: %s", i, Request(w))
}
log.Printf("%s: Requests completed after %v", "SerialWorkers", time.Now().Sub(s))
}
// ConcurrentWorkers does the same thing as SerialWorkers,
// only each request is run in a separate Go routine.
// It waits until all routines are finished
func ConcurrentWorkers(n int, w int64) {
s := time.Now()
var wg sync.WaitGroup
wg.Add(n)
for i := 0; i < n; i++ {
go func(i int) {
log.Printf("Req #%02d done: %s", i, Request(w))
wg.Done()
}(i)
}
wg.Wait()
log.Printf("%s: Requests completed after %v", "ConcurrentWorkers", time.Now().Sub(s))
}
var (
number int
maxWait int64
)
func init() {
flag.IntVar(&number, "number", 10, "Number of requests")
flag.Int64Var(&maxWait, "wait", 100, "Max wait for requests")
flag.Parse()
}
func main() {
log.Println("Running SerialWorkers")
SerialWorkers(number, maxWait)
log.Println("-------------------------")
log.Println("Running ConcurrentWorkers")
ConcurrentWorkers(number, maxWait)
}
@muhlemmer
Copy link
Author

Example output:

$ go run main.go -number=20 -wait=100
2020/01/31 15:57:43 Running SerialWorkers
2020/01/31 15:57:43 Req #00 done: Blocked for 10ms
2020/01/31 15:57:43 Req #01 done: Blocked for 51ms
2020/01/31 15:57:43 Req #02 done: Blocked for 21ms
2020/01/31 15:57:43 Req #03 done: Blocked for 51ms
2020/01/31 15:57:43 Req #04 done: Blocked for 37ms
2020/01/31 15:57:43 Req #05 done: Blocked for 20ms
2020/01/31 15:57:43 Req #06 done: Blocked for 58ms
2020/01/31 15:57:43 Req #07 done: Blocked for 48ms
2020/01/31 15:57:43 Req #08 done: Blocked for 16ms
2020/01/31 15:57:43 Req #09 done: Blocked for 49ms
2020/01/31 15:57:43 Req #10 done: Blocked for 84ms
2020/01/31 15:57:43 Req #11 done: Blocked for 87ms
2020/01/31 15:57:43 Req #12 done: Blocked for 74ms
2020/01/31 15:57:43 Req #13 done: Blocked for 36ms
2020/01/31 15:57:43 Req #14 done: Blocked for 15ms
2020/01/31 15:57:43 Req #15 done: Blocked for 73ms
2020/01/31 15:57:43 Req #16 done: Blocked for 68ms
2020/01/31 15:57:43 Req #17 done: Blocked for 91ms
2020/01/31 15:57:44 Req #18 done: Blocked for 90ms
2020/01/31 15:57:44 Req #19 done: Blocked for 31ms
2020/01/31 15:57:44 SerialWorkers: Requests completed after 1.013634068s
2020/01/31 15:57:44 -------------------------
2020/01/31 15:57:44 Running ConcurrentWorkers
2020/01/31 15:57:44 Req #13 done: Blocked for 3ms
2020/01/31 15:57:44 Req #17 done: Blocked for 7ms
2020/01/31 15:57:44 Req #12 done: Blocked for 9ms
2020/01/31 15:57:44 Req #04 done: Blocked for 11ms
2020/01/31 15:57:44 Req #00 done: Blocked for 23ms
2020/01/31 15:57:44 Req #18 done: Blocked for 32ms
2020/01/31 15:57:44 Req #05 done: Blocked for 37ms
2020/01/31 15:57:44 Req #16 done: Blocked for 39ms
2020/01/31 15:57:44 Req #11 done: Blocked for 43ms
2020/01/31 15:57:44 Req #02 done: Blocked for 44ms
2020/01/31 15:57:44 Req #08 done: Blocked for 50ms
2020/01/31 15:57:44 Req #03 done: Blocked for 56ms
2020/01/31 15:57:44 Req #15 done: Blocked for 59ms
2020/01/31 15:57:44 Req #10 done: Blocked for 71ms
2020/01/31 15:57:44 Req #07 done: Blocked for 72ms
2020/01/31 15:57:44 Req #06 done: Blocked for 73ms
2020/01/31 15:57:44 Req #19 done: Blocked for 78ms
2020/01/31 15:57:44 Req #01 done: Blocked for 80ms
2020/01/31 15:57:44 Req #14 done: Blocked for 83ms
2020/01/31 15:57:44 Req #09 done: Blocked for 88ms
2020/01/31 15:57:44 ConcurrentWorkers: Requests completed after 88.380596ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment