v / examples / coroutines / coroutines_bench.v
36 lines · 32 sloc · 732 bytes · a80bc2331450fc28c900097f8afafe173f161d27
Raw
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.
3import coroutines
4import time
5import net.http
6import sync
7
8const run_time = 10 * time.second
9
10fn 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
21fn 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