Skip to content

Instantly share code, notes, and snippets.

@syedrakib
Created February 13, 2022 16:16
Show Gist options
  • Save syedrakib/16feb3147eaf95915bf0caecebf8519b to your computer and use it in GitHub Desktop.
Save syedrakib/16feb3147eaf95915bf0caecebf8519b to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"math/rand"
"time"
)
var program_start_time time.Time
var program_end_time time.Time
var total_completed_jobs = 0
var total_worker_busyness = 0
func init() {
rand.Seed(time.Now().UnixNano())
}
func main() {
program_start_time = time.Now()
fmt.Println("----------------------------------------------------")
go do_some_work("A")
go do_some_work("B")
go do_some_work("C")
go do_some_work("D")
time.Sleep(1 * time.Second)
fmt.Println("----------------------------------------------------")
program_end_time = time.Now()
produce_final_report()
}
func do_some_work(work_id string) {
const NUM_OF_JOBS = 5
for job_id := 1; job_id <= NUM_OF_JOBS; job_id++ {
wait_ms := random_wait()
fmt.Printf("job%d of work%s took: %d ms\n", job_id, work_id, wait_ms)
total_completed_jobs++
total_worker_busyness += wait_ms
}
}
func random_wait() int {
const MIN_WAIT_MS = 50
const MAX_WAIT_MS = 350
wait_milliseconds := MIN_WAIT_MS + rand.Intn(MAX_WAIT_MS-MIN_WAIT_MS)
time.Sleep(time.Duration(wait_milliseconds) * time.Millisecond)
return wait_milliseconds
}
func produce_final_report() {
fmt.Println(" Program time: ", program_end_time.Sub(program_start_time))
fmt.Println(" Total completed jobs: ", total_completed_jobs)
fmt.Println("Total worker busyness: ", total_worker_busyness, "ms")
}
----------------------------------------------------
job1 of workB took: 84 ms
job1 of workA took: 126 ms
job1 of workC took: 140 ms
job2 of workA took: 107 ms
job1 of workD took: 255 ms
job2 of workD took: 66 ms
job2 of workB took: 277 ms
job3 of workA took: 206 ms
job3 of workD took: 128 ms
job2 of workC took: 335 ms
job3 of workB took: 118 ms
job3 of workC took: 149 ms
job4 of workD took: 234 ms
job4 of workA took: 294 ms
job4 of workC took: 150 ms
job4 of workB took: 299 ms
job5 of workA took: 55 ms
job5 of workD took: 103 ms
job5 of workC took: 98 ms
job5 of workB took: 169 ms
----------------------------------------------------
Program time: 1.001243381s
Total completed jobs: 20
Total worker busyness: 3393 ms
----------------------------------------------------
job1 of workD took: 143 ms
job1 of workB took: 149 ms
job1 of workA took: 250 ms
job1 of workC took: 344 ms
job2 of workD took: 249 ms
job2 of workB took: 254 ms
job2 of workC took: 196 ms
job2 of workA took: 303 ms
job3 of workD took: 237 ms
job3 of workB took: 283 ms
job3 of workA took: 157 ms
job3 of workC took: 180 ms
job4 of workA took: 60 ms
job4 of workC took: 119 ms
job4 of workB took: 175 ms
job4 of workD took: 304 ms
job5 of workC took: 135 ms
----------------------------------------------------
Program time: 1.000816761s
Total completed jobs: 17
Total worker busyness: 3538 ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment