Skip to content

Instantly share code, notes, and snippets.

@syedrakib
Last active February 13, 2022 15:49
Show Gist options
  • Save syedrakib/ac8d1412adb6601a769f3a1f665e9a5b to your computer and use it in GitHub Desktop.
Save syedrakib/ac8d1412adb6601a769f3a1f665e9a5b 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("----------------------------------------------------")
do_some_work("A")
do_some_work("B")
do_some_work("C")
do_some_work("D")
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 workA took: 177 ms
job2 of workA took: 254 ms
job3 of workA took: 340 ms
job4 of workA took: 257 ms
job5 of workA took: 112 ms
job1 of workB took: 254 ms
job2 of workB took: 200 ms
job3 of workB took: 331 ms
job4 of workB took: 76 ms
job5 of workB took: 331 ms
job1 of workC took: 199 ms
job2 of workC took: 170 ms
job3 of workC took: 247 ms
job4 of workC took: 260 ms
job5 of workC took: 188 ms
job1 of workD took: 79 ms
job2 of workD took: 53 ms
job3 of workD took: 86 ms
job4 of workD took: 296 ms
job5 of workD took: 244 ms
----------------------------------------------------
Program time: 4.163113198s
Total completed jobs: 20
Total worker busyness: 4154 ms
----------------------------------------------------
job1 of workA took: 217 ms
job2 of workA took: 218 ms
job3 of workA took: 209 ms
job4 of workA took: 226 ms
job5 of workA took: 316 ms
job1 of workB took: 302 ms
job2 of workB took: 283 ms
job3 of workB took: 198 ms
job4 of workB took: 215 ms
job5 of workB took: 150 ms
job1 of workC took: 87 ms
job2 of workC took: 294 ms
job3 of workC took: 287 ms
job4 of workC took: 228 ms
job5 of workC took: 327 ms
job1 of workD took: 285 ms
job2 of workD took: 50 ms
job3 of workD took: 285 ms
job4 of workD took: 347 ms
job5 of workD took: 328 ms
----------------------------------------------------
Program time: 4.862113742s
Total completed jobs: 20
Total worker busyness: 4852 ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment