Last active
February 13, 2022 16:26
-
-
Save syedrakib/c03d30c4913826681601ec986a7c9378 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math/rand" | |
"sync" | |
"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("----------------------------------------------------") | |
var wg sync.WaitGroup | |
wg.Add(4) // 4 = once for each goroutine | |
go do_some_work("A", &wg) | |
go do_some_work("B", &wg) | |
go do_some_work("C", &wg) | |
go do_some_work("D", &wg) | |
wg.Wait() | |
fmt.Println("----------------------------------------------------") | |
program_end_time = time.Now() | |
produce_final_report() | |
} | |
func do_some_work(work_id string, wg *sync.WaitGroup) { | |
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 | |
} | |
wg.Done() | |
} | |
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") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
---------------------------------------------------- | |
job1 of workB took: 63 ms | |
job1 of workC took: 82 ms | |
job1 of workD took: 94 ms | |
job1 of workA took: 223 ms | |
job2 of workB took: 235 ms | |
job2 of workA took: 88 ms | |
job2 of workD took: 253 ms | |
job3 of workA took: 67 ms | |
job2 of workC took: 329 ms | |
job4 of workA took: 57 ms | |
job3 of workD took: 113 ms | |
job3 of workC took: 101 ms | |
job3 of workB took: 240 ms | |
job5 of workA took: 117 ms | |
job4 of workD took: 244 ms | |
job4 of workC took: 247 ms | |
job4 of workB took: 250 ms | |
job5 of workB took: 140 ms | |
job5 of workD took: 310 ms | |
job5 of workC took: 272 ms | |
---------------------------------------------------- | |
Program time: 1.034071978s | |
Total completed jobs: 20 | |
Total worker busyness: 3525 ms |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
---------------------------------------------------- | |
job1 of workB took: 55 ms | |
job1 of workD took: 199 ms | |
job1 of workA took: 306 ms | |
job1 of workC took: 312 ms | |
job2 of workB took: 308 ms | |
job2 of workA took: 79 ms | |
job2 of workD took: 348 ms | |
job3 of workB took: 215 ms | |
job4 of workB took: 50 ms | |
job2 of workC took: 323 ms | |
job3 of workA took: 300 ms | |
job5 of workB took: 85 ms | |
job4 of workA took: 112 ms | |
job3 of workD took: 296 ms | |
job3 of workC took: 239 ms | |
job5 of workA took: 118 ms | |
job4 of workD took: 89 ms | |
job4 of workC took: 223 ms | |
job5 of workD took: 165 ms | |
job5 of workC took: 195 ms | |
---------------------------------------------------- | |
Program time: 1.296523594s | |
Total completed jobs: 20 | |
Total worker busyness: 4017 ms |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment