| 1 | // vtest build: false // This should be build with: `v -use-coroutines coroutine_benchs.v` |
| 2 | // Note: the Photon wrapper is not yet trivial enough to build/install on the CI. |
| 3 | import coroutines |
| 4 | import time |
| 5 | import net.http |
| 6 | import sync |
| 7 | |
| 8 | const run_time = 10 * time.second |
| 9 | |
| 10 | fn request(mut mu sync.Mutex, count &int) { |
| 11 | for { |
| 12 | http.get('http://vlang.io/utc_now') or { panic(err) } |
| 13 | mu.lock() |
| 14 | unsafe { |
| 15 | (*count)++ |
| 16 | } |
| 17 | mu.unlock() |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | fn main() { |
| 22 | mut mu := sync.new_mutex() |
| 23 | mut count := 0 |
| 24 | |
| 25 | for _ in 0 .. 8 { |
| 26 | go request(mut mu, &count) |
| 27 | } |
| 28 | $if is_coroutine ? { |
| 29 | println('IS COROUTINE=true') |
| 30 | coroutines.sleep(run_time) |
| 31 | } $else { |
| 32 | println('IS COROUTINE=false') |
| 33 | time.sleep(run_time) |
| 34 | } |
| 35 | println('${count} requests made.') |
| 36 | } |
| 37 | |