Skip to content

Instantly share code, notes, and snippets.

@itn3000
Created May 20, 2017 16:53
Show Gist options
  • Save itn3000/9465e1617a5bfd29c085ad2dbdc3b590 to your computer and use it in GitHub Desktop.
Save itn3000/9465e1617a5bfd29c085ad2dbdc3b590 to your computer and use it in GitHub Desktop.
nats bench sources
using System;
using NATS.Client;
namespace natstest
{
class Program
{
static void i32tobytes(int val, ref byte[] buf)
{
buf[0] = (byte)(val & 0xff);
buf[1] = (byte)((val >> 8) & 0xff);
buf[2] = (byte)((val >> 16) & 0xff);
buf[3] = (byte)((val >> 24) & 0xff);
}
static int bytestoi32(byte[] dat)
{
return (int)(dat[0])
+ (int)(dat[1]) << 8
+ (int)(dat[2]) << 16
+ (int)(dat[3]) << 24
;
}
static void Main(string[] args)
{
var cf = new ConnectionFactory();
var sw = new System.Diagnostics.Stopwatch();
sw.Start();
using(var consumer = cf.CreateConnection())
using(var producer = cf.CreateConnection())
{
using(var sub = consumer.SubscribeAsync("csnatstest", (sender, ev) => {
if(!string.IsNullOrEmpty(ev.Message.Reply))
{
consumer.Publish(ev.Message.Reply, ev.Message.Data);
}
}))
{
var dat = new byte[4];
for(int i = 0;i<10000;i++)
{
i32tobytes(i, ref dat);
var rep = producer.Request("csnatstest", dat);
if(i % 1000 == 999)
{
Console.WriteLine($"{bytestoi32(rep.Data)}");
}
}
}
}
Console.WriteLine($"{sw.Elapsed}");
}
}
}
package main
import "github.com/nats-io/go-nats"
import "time"
import "fmt"
func main() {
consumer, _ := nats.Connect(nats.DefaultURL)
producer, _ := nats.Connect(nats.DefaultURL)
consumer.Subscribe("gonatstest", func(m *nats.Msg) {
consumer.Publish(m.Reply, m.Data)
})
start := time.Now()
for i := 0; i < 100000; i++ {
msg, err := producer.Request("gonatstest", []byte(fmt.Sprintf("request%d", i)), 100*time.Millisecond)
if i%10000 == 9999 {
fmt.Printf("%s, %s\n", msg.Data, err)
}
}
fmt.Printf("%s\n", time.Now().Sub(start))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment