Skip to content

Instantly share code, notes, and snippets.

@Xe

Xe/httptest.zig Secret

Created October 12, 2019 15:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xe/7fcde4a6aa0e54c2543d6b5f923054bf to your computer and use it in GitHub Desktop.
Save Xe/7fcde4a6aa0e54c2543d6b5f923054bf to your computer and use it in GitHub Desktop.
const olin = @import("./olin/olin.zig");
const log = olin.log;
const resource = olin.resource;
const assert = @import("std").debug.assert;
const mem = @import("std").mem;
const fmt = @import("std").fmt;
const heap = @import("std").heap;
const Headers = @import("std").http.Headers;
const userAgent = "Olin+Zig@master";
export fn cwa_main() i32 {
log.info("making request to http://127.0.0.1:3000/foo.txt");
doRequest(heap.wasm_allocator) catch return 1;
return 0;
}
fn doRequest(alloc: *mem.Allocator) !void {
const fout = try resource.open("http://127.0.0.1:3000");
var buf: []u8 = undefined;
buf = try alloc.alloc(u8, 256);
defer alloc.free(buf);
var h = Headers.init(alloc);
defer h.deinit();
try h.append("User-Agent", userAgent, null);
try h.append("Host", "127.0.0.1:3000", null);
var res = try fmt.bufPrint(buf[0..], "GET /foo.txt HTTP/1.1\n{}\n\n", h);
const n = try fout.write(res.ptr, res.len);
log.info(res);
try fout.flush();
var resp: []u8 = undefined;
resp = try alloc.alloc(u8, 2048);
defer alloc.free(resp);
const nresp = try fout.read(resp.ptr, resp.len);
log.info(resp[0..nresp]);
}
unoptimized build
httptest.wasm: 2019/10/12 11:27:37 info: making request to http://127.0.0.1:3000/foo.txt
httptest.wasm: 2019/10/12 11:27:37 info: GET /foo.txt HTTP/1.1
User-Agent: Olin+Zig@master
Host: 127.0.0.1:3000
2019/10/12 11:27:37 http://127.0.0.1:3000: 207 bytes waiting in resp
httptest.wasm: 2019/10/12 11:27:37 info: HTTP/1.1 200 OK
Content-Length: 22
Accept-Ranges: bytes
Content-Type: text/plain; charset=utf-8
Date: Sat, 12 Oct 2019 15:27:37 GMT
Last-Modified: Sat, 12 Oct 2019 14:50:12 GMT
Hello this is a test!
2019/10/12 11:27:37 reading file time: 176.21µs
2019/10/12 11:27:37 vm init time: 42.639344ms
2019/10/12 11:27:37 vm gas limit: 4194304
2019/10/12 11:27:37 vm gas used: 294649
2019/10/12 11:27:37 vm gas percentage: 7.024979591369629
2019/10/12 11:27:37 vm syscalls: 7
2019/10/12 11:27:37 execution time: 49.13221ms
2019/10/12 11:27:37 memory pages: 3
optimized build (--release-fast)
httptest.wasm: 2019/10/12 11:28:54 info: making request to http://127.0.0.1:3000/foo.txt
httptest.wasm: 2019/10/12 11:28:54 info: GET /foo.txt HTTP/1.1
User-Agent: Olin+Zig@master
Host: 127.0.0.1:3000
2019/10/12 11:28:54 http://127.0.0.1:3000: 207 bytes waiting in resp
httptest.wasm: 2019/10/12 11:28:54 info: HTTP/1.1 200 OK
Content-Length: 22
Accept-Ranges: bytes
Content-Type: text/plain; charset=utf-8
Date: Sat, 12 Oct 2019 15:28:54 GMT
Last-Modified: Sat, 12 Oct 2019 14:50:12 GMT
Hello this is a test!
2019/10/12 11:28:54 reading file time: 123.688µs
2019/10/12 11:28:54 vm init time: 10.769802ms
2019/10/12 11:28:54 vm gas limit: 4194304
2019/10/12 11:28:54 vm gas used: 59120
2019/10/12 11:28:54 vm gas percentage: 1.4095306396484375
2019/10/12 11:28:54 vm syscalls: 7
2019/10/12 11:28:54 execution time: 13.301229ms
2019/10/12 11:28:54 memory pages: 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment