Skip to content

Commit 4456dac

Browse files
committed
ci: add performance compare
1 parent d7603c7 commit 4456dac

File tree

2 files changed

+248
-0
lines changed

2 files changed

+248
-0
lines changed
+148
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
import net.http
2+
import sync.pool
3+
import time
4+
import vibe
5+
import cli
6+
import os
7+
8+
pub struct GetResult {
9+
error_kind ?GetError
10+
}
11+
12+
enum GetError {
13+
request_err
14+
status_err
15+
}
16+
17+
fn std_request(mut pp pool.PoolProcessor, idx int, wid int) &GetResult {
18+
url := pp.get_item[string](idx)
19+
20+
start := time.new_stopwatch()
21+
defer { println(' ${url}: ${start.elapsed()}') }
22+
23+
mut req := http.Request{
24+
url: 'https://${url}'
25+
method: .get
26+
}
27+
req.add_header(.user_agent, 'net-vibe-perf-test/0.0.1')
28+
29+
resp := req.do() or {
30+
eprintln(' Failed to get: "${url}": ${err}')
31+
return &GetResult{.request_err}
32+
}
33+
if resp.status_code != 200 {
34+
eprintln(' Failed to get success response: "${resp.status}"')
35+
return &GetResult{.status_err}
36+
}
37+
38+
// println(' Response content length: ${resp.body.len}')
39+
40+
return &GetResult{}
41+
}
42+
43+
fn vibe_request(mut pp pool.PoolProcessor, idx int, wid int) &GetResult {
44+
url := pp.get_item[string](idx)
45+
46+
start := time.new_stopwatch()
47+
defer { println(' ${url}: ${start.elapsed()}') }
48+
49+
mut req := vibe.Request{
50+
headers: {
51+
.user_agent: 'net-vibe-perf-test/0.0.1'
52+
}
53+
}
54+
55+
resp := req.get('https://${url}') or {
56+
eprintln(' Failed to get: "${url}": ${err}')
57+
return &GetResult{.request_err}
58+
}
59+
if resp.status != 200 {
60+
eprintln(' Failed to get success response: "${resp.status}"')
61+
return &GetResult{.status_err}
62+
}
63+
// println(' Response content length: ${resp.body.len}')
64+
65+
return &GetResult{}
66+
}
67+
68+
fn prep_urls(single_host bool, request_num int) ![]string {
69+
return if single_host {
70+
base := 'google.com/search?q='
71+
[]string{len: request_num, init: base + index.str()}
72+
} else {
73+
urls := 'https://gist.githubusercontent.com/ttytm/b2c6e348dac6b3f0ffa150639ad94211/raw/0823f71f18d5567d231dabbafe4ad22d006f0e61/100-popular-urls.txt'
74+
resp := http.get(urls)!
75+
resp.body.split_into_lines()[..request_num]
76+
}
77+
}
78+
79+
fn main() {
80+
mut app := cli.Command{
81+
name: 'net-vibe-perf-test'
82+
posix_mode: true
83+
execute: run
84+
flags: [
85+
cli.Flag{
86+
name: 'request-num'
87+
flag: .int
88+
abbrev: 'r'
89+
default_value: ['100'] // Currently the maximium number is capped by the number of prepared urls.
90+
},
91+
cli.Flag{
92+
name: 'single-host'
93+
flag: .bool
94+
abbrev: 's'
95+
},
96+
cli.Flag{
97+
name: 'use-vibe'
98+
flag: .bool
99+
},
100+
]
101+
}
102+
app.parse(os.args)
103+
}
104+
105+
fn run(cmd cli.Command) ! {
106+
single_host := cmd.flags.get_bool('single-host')!
107+
use_vibe := cmd.flags.get_bool('use-vibe')!
108+
request_num := cmd.flags.get_int('request-num')!
109+
110+
mut urls := prep_urls(single_host, if request_num < 100 { request_num } else { 100 })!
111+
112+
start := time.new_stopwatch()
113+
114+
mut pp := pool.new_pool_processor(
115+
callback: if use_vibe { vibe_request } else { std_request }
116+
)
117+
pp.work_on_items(urls)
118+
119+
results := pp.get_results[GetResult]()
120+
mut request_errs, mut status_errs := 0, 0
121+
for v in results {
122+
err_kind := v.error_kind or { continue }
123+
if err_kind == GetError.request_err {
124+
request_errs++
125+
} else {
126+
status_errs++
127+
}
128+
}
129+
130+
mod := if use_vibe { 'vibe' } else { 'net.http' }
131+
host_details := if single_host { ', single host' } else { '' }
132+
res := '
133+
┌–––––––––––––––––––––––––––––––––––––––––––––––––––––––––┐
134+
Results for "${mod}"${host_details}
135+
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––
136+
Requests: ${results.len}, Request errors: ${request_errs}, Status errors: ${status_errs}
137+
Total time: ${start.elapsed()}
138+
└–––––––––––––––––––––––––––––––––––––––––––––––––––––––––┘
139+
'
140+
println(res)
141+
if os.getenv('GITHUB_JOB') != '' {
142+
os.system('echo ``` >> \$GITHUB_STEP_SUMMARY')
143+
for l in res.trim_space().split_into_lines() {
144+
os.system("echo '${l}' >> \$GITHUB_STEP_SUMMARY")
145+
}
146+
os.system("echo '```' >> \$GITHUB_STEP_SUMMARY")
147+
}
148+
}
+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
name: Performance comparison
2+
3+
on:
4+
workflow_dispatch:
5+
6+
jobs:
7+
setup:
8+
if: github.ref_name != 'main'
9+
runs-on: ${{ matrix.os }}
10+
strategy:
11+
matrix:
12+
include:
13+
- os: macos-latest
14+
v_archive: v_macos_arm64.zip
15+
- os: ubuntu-latest
16+
v_archive: v_linux.zip
17+
- os: windows-latest
18+
v_archive: v_windows.zip
19+
fail-fast: false
20+
defaults:
21+
run:
22+
shell: bash
23+
steps:
24+
- name: Setup V
25+
run: |
26+
curl -LO https://github.com/vlang/v/releases/latest/download/${{ matrix.v_archive }}
27+
if [[ $RUNNER_OS == "Windows" ]]; then
28+
7z x ${{ matrix.v_archive }}
29+
else
30+
unzip -o ${{ matrix.v_archive }}
31+
fi
32+
mv v ~/v
33+
~/v/v symlink
34+
- run: v -showcc self && v doctor
35+
- uses: actions/checkout@v4
36+
with:
37+
path: vibe
38+
- name: Setup V module
39+
run: |
40+
mv vibe ~/.vmodules/vibe
41+
~/.vmodules/vibe/curl/setup.vsh --silent
42+
- name: Save cache
43+
uses: actions/cache/save@v4
44+
with:
45+
path: |
46+
~/v
47+
~/.vmodules
48+
key: ${{ matrix.os }}-
49+
50+
compare:
51+
needs: setup
52+
if: ${{ !cancelled() }}
53+
runs-on: ${{ matrix.os }}
54+
strategy:
55+
matrix:
56+
os: [ubuntu-latest, windows-latest, macos-latest]
57+
fail-fast: false
58+
steps:
59+
- name: Restore cache
60+
uses: actions/cache/restore@v4
61+
with:
62+
path: |
63+
~/v
64+
~/.vmodules
65+
key: ${{ matrix.os }}-${{ github.sha }}
66+
fail-on-cache-miss: true
67+
- uses: actions/checkout@v4
68+
- name: Setup V
69+
run: ~/v/v symlink
70+
- name: Run comparison
71+
if: runner.os != 'Windows'
72+
run: |
73+
v -cc gcc -prod -o pc .github/workflows/performance_compare.v
74+
./pc -r 25 --single-host
75+
./pc -r 25 --single-host --use-vibe
76+
./pc -r 50 --single-host
77+
./pc -r 50 --single-host --use-vibe
78+
./pc -r 25
79+
./pc -r 25 --use-vibe
80+
./pc -r 50
81+
./pc -r 50 --use-vibe
82+
# `net.http` gets stuck for over 15 minutes when trying to perform 100 requests.
83+
# ./pc -r 100
84+
# ./pc -r 100 --use-vibe
85+
- name: Run comparison (Windows)
86+
if: runner.os == 'Windows'
87+
shell: cmd
88+
run: |
89+
set PATH=%PATH%;%USERPROFILE%\.vmodules\vibe\curl\libcurl\bin
90+
v -cc gcc -prod -o pc.exe .github\workflows\performance_compare.v
91+
pc.exe -r 25 --single-host
92+
pc.exe -r 25 --single-host --use-vibe
93+
pc.exe -r 50 --single-host
94+
pc.exe -r 50 --single-host --use-vibe
95+
pc.exe -r 25
96+
pc.exe -r 25 --use-vibe
97+
pc.exe -r 50
98+
pc.exe -r 50 --use-vibe
99+
# pc.exe -r 100
100+
# pc.exe -r 100 --use-vibe

0 commit comments

Comments
 (0)