| 1 | module main |
| 2 | |
| 3 | import os |
| 4 | import veb |
| 5 | import time |
| 6 | |
| 7 | struct Context { |
| 8 | veb.Context |
| 9 | } |
| 10 | |
| 11 | struct App {} |
| 12 | |
| 13 | fn exit_after_timeout(timeout_in_ms int) { |
| 14 | time.sleep(timeout_in_ms * time.millisecond) |
| 15 | exit(0) |
| 16 | } |
| 17 | |
| 18 | fn main() { |
| 19 | if os.args.len != 3 { |
| 20 | panic('Usage: memory_leak_test_server.exe PORT TIMEOUT_IN_MILLISECONDS') |
| 21 | } |
| 22 | http_port := os.args[1].int() |
| 23 | assert http_port > 0 |
| 24 | timeout := os.args[2].int() |
| 25 | assert timeout > 0 |
| 26 | spawn exit_after_timeout(timeout) |
| 27 | |
| 28 | mut app := &App{} |
| 29 | veb.run_at[App, Context](mut app, |
| 30 | host: '127.0.0.1' |
| 31 | port: http_port |
| 32 | family: .ip |
| 33 | timeout_in_seconds: 10 |
| 34 | )! |
| 35 | } |
| 36 | |
| 37 | pub fn (mut app App) index(mut ctx Context) veb.Result { |
| 38 | return ctx.text('OK') |
| 39 | } |
| 40 | |
| 41 | @['/heap'] |
| 42 | pub fn (mut app App) heap(mut ctx Context) veb.Result { |
| 43 | usage := gc_heap_usage() |
| 44 | return ctx.text('${usage.heap_size}') |
| 45 | } |
| 46 | |
| 47 | @['/gc'] |
| 48 | pub fn (mut app App) gc_collect(mut ctx Context) veb.Result { |
| 49 | gc_collect() |
| 50 | return ctx.text('GC collected') |
| 51 | } |
| 52 | |
| 53 | @['/large'] |
| 54 | pub fn (mut app App) large(mut ctx Context) veb.Result { |
| 55 | data := 'X'.repeat(100 * 1024) |
| 56 | return ctx.text(data) |
| 57 | } |
| 58 | |
| 59 | @['/upload'; post] |
| 60 | pub fn (mut app App) upload(mut ctx Context) veb.Result { |
| 61 | if 'file' !in ctx.files || ctx.files['file'].len == 0 { |
| 62 | ctx.res.set_status(.bad_request) |
| 63 | return ctx.text('no file') |
| 64 | } |
| 65 | return ctx.text(ctx.files['file'][0].data.len.str()) |
| 66 | } |
| 67 | |