Created
September 18, 2019 22:53
-
-
Save kristoff-it/bde02ab5d2c5313fd7d13122c3346f54 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const std = @import("std"); | |
const time = std.time; | |
const builtin = @import("builtin"); | |
const heyredis = @import("./src/heyredis.zig"); | |
const addr = if (builtin.os == .linux) "192.168.65.2" else "127.0.0.1"; | |
pub const io_mode = .evented; | |
pub fn main() !void { | |
if (std.io.is_async) { | |
const loop = std.event.Loop.instance.?; | |
try loop.initSingleThreaded(std.heap.direct_allocator); | |
defer loop.deinit(); | |
var result: @typeOf(asyncMain).ReturnType.ErrorSet!void = undefined; | |
var frame: @Frame(asyncMain) = undefined; | |
_ = @asyncCall(&frame, &result, asyncMain, loop); | |
loop.run(); | |
return result; | |
} else { | |
return allMain(); | |
} | |
} | |
async fn asyncMain(loop: *std.event.Loop) !void { | |
loop.beginOneEvent(); | |
defer loop.finishOneEvent(); | |
return allMain(); | |
} | |
fn allMain() !void { | |
var client: heyredis.Client = undefined; | |
try heyredis.Client.initIp4(&client, addr, 6379); | |
defer client.close(); | |
// Test 1 | |
{ | |
try client.send(void, "SET", "counter", "-1"); | |
var t = try time.Timer.start(); | |
var i: usize = 0; | |
const loops: usize = 1000; | |
while (i < loops) : (i += 1) { | |
const ctr = try client.send(usize, "INCR", "counter"); | |
if (ctr != i) @panic("Error!"); | |
} | |
var took = t.read(); | |
std.debug.warn("Counter test -- OK [{d}ms]\n\n", @intToFloat(f64, took) / time.millisecond); | |
} | |
// Test 2 | |
{ | |
try client.send(void, "SET", "counter", "-1"); | |
var t = try time.Timer.start(); | |
var i: usize = 0; | |
const loops: usize = 1000; | |
while (i < loops) : (i += 1) { | |
try client.send(void, "INCR", "counter"); | |
} | |
var took = t.read(); | |
std.debug.warn("Counter seq -- OK [{d}ms]\n\n", @intToFloat(f64, took) / time.millisecond); | |
} | |
// Test 3 | |
{ | |
try client.send(void, "SET", "counter", "-1"); | |
var t = try time.Timer.start(); | |
var i: usize = 0; | |
var f = async client.send(void, "INCR", "counter"); | |
try await f; | |
var took = t.read(); | |
std.debug.warn("Counter async -- OK [{d}ms]\n\n", @intToFloat(f64, took) / time.millisecond); | |
} | |
// Test 4 | |
{ | |
try client.send(void, "SET", "counter", "-1"); | |
var t = try time.Timer.start(); | |
var i: usize = 0; | |
const loops: usize = 1000; | |
comptime var FrameType = @typeOf(async client.send(void, "INCR", "counter")); | |
var frames: [loops]FrameType = undefined; | |
while (i < loops) : (i += 1) { | |
frames[i] = async client.send(void, "INCR", "counter"); | |
} | |
i = 0; | |
while (i < loops) : (i += 1) { | |
try await frames[i]; | |
} | |
var took = t.read(); | |
std.debug.warn("Counter pipe -- OK [{d}ms]\n\n", @intToFloat(f64, took) / time.millisecond); | |
} | |
// Test 5 | |
{ | |
const divine = | |
\\When half way through the journey of our life | |
\\I found that I was in a gloomy wood, | |
\\because the path which led aright was lost. | |
\\And ah, how hard it is to say just what | |
\\this wild and rough and stubborn woodland was, | |
\\the very thought of which renews my fear! | |
; | |
try client.send(void, "SET", "divine", divine); | |
const allocator = std.heap.direct_allocator; | |
var i: i64 = 0; | |
const loops: i64 = 1000; | |
while (i < loops) : (i += 1) { | |
var inferno = try client.send([255]u8, "GET", "divine"); | |
if (!std.mem.eql(u8, divine, inferno[0..])) @panic("Error - string\n"); | |
} | |
std.debug.warn("String -- OK\n\n"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment