Skip to content

Commit 4b2e11a

Browse files
committed
ci: add performance compare
1 parent d7603c7 commit 4b2e11a

File tree

2 files changed

+253
-0
lines changed

2 files changed

+253
-0
lines changed
+152
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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+
133+
res := '
134+
┌–––––––––––––––––––––––––––––––––––––––––––––––––––––––––┐
135+
Results for "${mod}"${host_details}
136+
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––
137+
Requests: ${results.len}, Request errors: ${request_errs}, Status errors: ${status_errs}
138+
Total time: ${start.elapsed()}
139+
└–––––––––––––––––––––––––––––––––––––––––––––––––––––––––┘
140+
'
141+
println(res)
142+
if os.getenv('GITHUB_JOB') != '' {
143+
for l in res.split_into_lines() {
144+
summary_cmd := "echo '${l}' >> \$GITHUB_STEP_SUMMARY"
145+
$if windows {
146+
os.system('wsl ${summary_cmd}')
147+
} $else {
148+
os.system(summary_cmd)
149+
}
150+
}
151+
}
152+
}
+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
name: Performance comparison
2+
3+
on:
4+
workflow_dispatch:
5+
# TODO: remove
6+
push:
7+
8+
jobs:
9+
setup:
10+
if: github.ref_name != 'main'
11+
runs-on: ${{ matrix.os }}
12+
strategy:
13+
matrix:
14+
include:
15+
- os: macos-latest
16+
v_archive: v_macos_arm64.zip
17+
- os: ubuntu-latest
18+
v_archive: v_linux.zip
19+
- os: windows-latest
20+
v_archive: v_windows.zip
21+
fail-fast: false
22+
defaults:
23+
run:
24+
shell: bash
25+
steps:
26+
- name: Setup V
27+
run: |
28+
curl -LO https://github.com/vlang/v/releases/latest/download/${{ matrix.v_archive }}
29+
if [[ $RUNNER_OS == "Windows" ]]; then
30+
7z x ${{ matrix.v_archive }}
31+
else
32+
unzip -o ${{ matrix.v_archive }}
33+
fi
34+
mv v ~/v
35+
~/v/v symlink
36+
- run: v -showcc self && v doctor
37+
- uses: actions/checkout@v4
38+
with:
39+
path: vibe
40+
- name: Setup V module
41+
run: |
42+
mv vibe ~/.vmodules/vibe
43+
~/.vmodules/vibe/curl/setup.vsh --silent
44+
- name: Save cache
45+
uses: actions/cache/save@v4
46+
with:
47+
path: |
48+
~/v
49+
~/.vmodules
50+
key: ${{ matrix.os }}-${{ github.sha }}
51+
52+
compare:
53+
needs: setup
54+
if: ${{ !cancelled() }}
55+
runs-on: ${{ matrix.os }}
56+
strategy:
57+
matrix:
58+
os: [ubuntu-latest, windows-latest, macos-latest]
59+
fail-fast: false
60+
steps:
61+
- name: Restore cache
62+
uses: actions/cache/restore@v4
63+
with:
64+
path: |
65+
~/v
66+
~/.vmodules
67+
key: ${{ matrix.os }}-${{ github.sha }}
68+
fail-on-cache-miss: true
69+
- uses: actions/checkout@v4
70+
- name: Setup V
71+
run: ~/v/v symlink
72+
- name: Run comparison
73+
if: runner.os != 'Windows'
74+
run: |
75+
v -cc gcc -prod -o pc .github/workflows/performance_compare.v
76+
./pc -r 25 --single-host
77+
./pc -r 25 --single-host --use-vibe
78+
./pc -r 50 --single-host
79+
./pc -r 50 --single-host --use-vibe
80+
./pc -r 25
81+
./pc -r 25 --use-vibe
82+
./pc -r 50
83+
./pc -r 50 --use-vibe
84+
./pc -r 100
85+
./pc -r 100 --use-vibe
86+
- name: Run comparison (Windows)
87+
if: runner.os == 'Windows'
88+
shell: cmd
89+
run: |
90+
set PATH=%PATH%;%USERPROFILE%\.vmodules\vibe\curl\libcurl\bin
91+
v -cc gcc -prod -o pc.exe .github\workflows\performance_compare.v
92+
pc.exe -r 25 --single-host
93+
pc.exe -r 25 --single-host --use-vibe
94+
pc.exe -r 50 --single-host
95+
pc.exe -r 50 --single-host --use-vibe
96+
pc.exe -r 25
97+
pc.exe -r 25 --use-vibe
98+
pc.exe -r 50
99+
pc.exe -r 50 --use-vibe
100+
pc.exe -r 100
101+
pc.exe -r 100 --use-vibe

0 commit comments

Comments
 (0)