diff --git a/.github/workflows/performance_compare.v b/.github/workflows/performance_compare.v new file mode 100644 index 0000000..fe4e3ac --- /dev/null +++ b/.github/workflows/performance_compare.v @@ -0,0 +1,133 @@ +import net.http +import sync.pool +import time +import vibe +import cli +import os + +pub struct GetResult { + error_kind ?GetError +} + +enum GetError { + request_err + status_err +} + +fn std_request(mut pp pool.PoolProcessor, idx int, wid int) &GetResult { + url := pp.get_item[string](idx) + + start := time.new_stopwatch() + defer { println('${url}: ${start.elapsed()}') } + + mut req := http.Request{ + url: 'https://${url}' + method: .get + } + req.add_header(.user_agent, 'net-vibe-perf-test/0.0.1') + + resp := req.do() or { + eprintln(' Failed to get: "${url}": ${err}') + return &GetResult{.request_err} + } + if resp.status_code != 200 { + eprintln(' Failed to get success response: "${resp.status}"') + return &GetResult{.status_err} + } + + // println(' Response content length: ${resp.body.len}') + + return &GetResult{} +} + +fn vibe_request(mut pp pool.PoolProcessor, idx int, wid int) &GetResult { + url := pp.get_item[string](idx) + + start := time.new_stopwatch() + defer { println('${url}: ${start.elapsed()}') } + + mut req := vibe.Request{ + headers: { + .user_agent: 'net-vibe-perf-test/0.0.1' + } + } + + resp := req.get('https://${url}') or { + eprintln(' Failed to get: "${url}": ${err}') + return &GetResult{.request_err} + } + if resp.status != 200 { + eprintln(' Failed to get success response: "${resp.status}"') + return &GetResult{.status_err} + } + // println(' Response content length: ${resp.body.len}') + + return &GetResult{} +} + +fn prep_urls(single_host bool, request_num int) ![]string { + return if single_host { + base := 'google.com/search?q=' + []string{len: request_num, init: base + index.str()} + } else { + urls := 'https://gist.githubusercontent.com/ttytm/b2c6e348dac6b3f0ffa150639ad94211/raw/0823f71f18d5567d231dabbafe4ad22d006f0e61/100-popular-urls.txt' + resp := http.get(urls)! + resp.body.split_into_lines()[..request_num] + } +} + +fn main() { + mut app := cli.Command{ + name: 'net-vibe-perf-test' + posix_mode: true + execute: run + flags: [ + cli.Flag{ + name: 'request-num' + flag: .int + abbrev: 'r' + default_value: ['100'] // Currently the maximium number is capped by the number of prepared urls. + }, + cli.Flag{ + name: 'single-host' + flag: .bool + abbrev: 's' + }, + cli.Flag{ + name: 'use-vibe' + flag: .bool + }, + ] + } + app.parse(os.args) +} + +fn run(cmd cli.Command) ! { + single_host := cmd.flags.get_bool('single_host')! + use_vibe := cmd.flags.get_bool('use_vibe')! + request_num := cmd.flags.get_int('request_num')! + + mut urls := prep_urls(single_host, if request_num < 100 { request_num } else { 100 })! + + start := time.new_stopwatch() + + mut pp := pool.new_pool_processor( + callback: if use_vibe { vibe_request } else { std_request } + ) + pp.work_on_items(urls) + + results := pp.get_results[GetResult]() + mut request_errs, mut status_errs := 0, 0 + for v in results { + err_kind := v.error_kind or { continue } + if err_kind == GetError.request_err { + request_errs++ + } else { + status_errs++ + } + } + + println('\nResults for "${if use_vibe { 'vibe' } else { 'net.http' }}"') + println('Requests: ${results.len}, Request errors: ${request_errs}, Status errors: ${status_errs}') + println('Total time: ${start.elapsed()}') +} diff --git a/.github/workflows/performance_compare.yml b/.github/workflows/performance_compare.yml new file mode 100644 index 0000000..410692e --- /dev/null +++ b/.github/workflows/performance_compare.yml @@ -0,0 +1,37 @@ +name: Performance comparison + +on: + workflow_dispatch: + +jobs: + test: + strategy: + matrix: + os: [ubuntu-latest] + fail-fast: false + runs-on: ${{ matrix.os }} + steps: + - name: Restore cache + uses: actions/cache/restore@v4 + with: + path: | + ~/v + ~/.vmodules + key: ${{ matrix.os }}-${{ github.sha }} + fail-on-cache-miss: true + - name: Setup V + run: ~/v/v symlink + - name: Run comparison + run: | + v -cc -gcc -prod -o pc .github/workflows/performance_compare.v + pc -r 25 --single-host + pc -r 25 --single-host --use-vibe + + pc -r 50 --single-host + pc -r 50 --single-host --use-vibe + + pc -r 25 + pc -r 25 --use-vibe + + pc -r 50 + pc -r 50 --use-vibe