Skip to content

Instantly share code, notes, and snippets.

@sreimers
Last active January 27, 2023 13:22
Show Gist options
  • Save sreimers/337c9ee783cbe6298d7352b338ed8c3c to your computer and use it in GitHub Desktop.
Save sreimers/337c9ee783cbe6298d7352b338ed8c3c to your computer and use it in GitHub Desktop.
tcp_performance
#!/bin/bash
idle=$1
active=$2
port=$3
rounds=50
echo "" > /tmp/bench.csv
redis-benchmark -c ${idle} -p ${port} -I &
pid=$!
count=0
for i in `seq 1 ${rounds}`; do
redis-benchmark -c ${active} -t ping_inline -n 1000000 -P 1 -p ${port} --csv | grep -v test >> /tmp/bench.csv
sleep 1
sum=0
count=$((count + 1))
while read DOH; do
value=`echo $DOH | awk -F"," '{ print $2 }' | sed 's/"//g' | awk -F"." '{ print $1 }'`
echo $value
sum=$((sum + value))
done < <(cat /tmp/bench.csv)
echo rps = $(($sum / $count)) round = ${count}
done
kill ${pid}
/**
* @file tcp.c libre TCP performance testcode
*/
#include <string.h>
#include <re.h>
#include <re_dbg.h>
/* #define HTML */
#define REDIS_ECHO
static struct tcp_sock *ts;
struct conn {
struct tcp_conn *tc;
struct mbuf mb;
};
static void signal_handler(int sig)
{
(void)sig;
re_cancel();
}
static void tcp_server_recv_handler(struct mbuf *mb, void *arg)
{
struct conn *conn = arg;
int err;
#ifdef REDIS_ECHO
size_t size = mb->end;
int count = 0;
while (size--) {
const uint8_t c = mbuf_read_u8(mb);
if ('\n' == c)
++count;
}
mbuf_rewind(&conn->mb);
while (count--) {
mbuf_write_str(&conn->mb, "+PONG\r\n");
}
#endif
#ifdef HTML
mbuf_write_str(&conn->mb, "HTTP/1.0 200 OK\r\n");
mbuf_write_str(&conn->mb, "Content-Type: text/html\r\n");
mbuf_write_str(&conn->mb, "Content-Length: 1\r\n");
mbuf_write_str(&conn->mb, "\r\n");
mbuf_write_str(&conn->mb, "a");
#endif
conn->mb.pos = 0;
err = tcp_send(conn->tc, &conn->mb);
if (err)
re_printf("send error %m\n", err);
}
static void tcp_server_close_handler(int err, void *arg)
{
struct conn *conn = arg;
if (err)
re_printf("close error %m\n", err);
mem_deref(conn);
}
static void conn_destruct(void *data)
{
struct conn *conn = data;
mem_deref(conn->tc);
mbuf_reset(&conn->mb);
}
static void tcp_server_conn_handler(const struct sa *peer, void *arg)
{
struct conn *conn;
int err;
(void)arg;
(void)peer;
conn = mem_alloc(sizeof(struct conn), conn_destruct);
err = tcp_accept(&conn->tc, ts, NULL, tcp_server_recv_handler,
tcp_server_close_handler, conn);
if (err)
re_printf("accept error %m\n", err);
mbuf_init(&conn->mb);
}
int main(void)
{
struct sa srv;
int err;
libre_init();
fd_setsize(128);
poll_method_set(METHOD_SELECT);
/* poll_method_set(METHOD_POLL); */
/* poll_method_set(METHOD_EPOLL); */
err = sa_set_str(&srv, "127.0.0.1", 8888);
if (err)
goto out;
err = tcp_sock_alloc(&ts, &srv, tcp_server_conn_handler, NULL);
if (err)
goto out;
err = tcp_sock_bind(ts, &srv);
if (err)
goto out;
err = tcp_sock_listen(ts, 1000); /* backlog needs to be big enough,
limited by SOMAXCONN */
if (err)
goto out;
err = re_main(signal_handler);
if (err)
goto out;
re_printf("%H\n", re_debug);
mem_deref(ts);
out:
re_thread_async_close();
tmr_debug();
libre_close();
mem_debug();
return err;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment