Skip to content

Instantly share code, notes, and snippets.

@jsimmons
Created October 12, 2010 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsimmons/622874 to your computer and use it in GitHub Desktop.
Save jsimmons/622874 to your computer and use it in GitHub Desktop.
lumberjack bench
#include <errno.h>
#include <unistd.h>
#include <glib.h>
#include <jansson.h>
#include <zmq.h>
#include <lumberjack.h>
#include "timer.h"
static gpointer lj_receiver(gpointer data)
{
void *sock = zmq_socket(data, ZMQ_SUB);
g_assert(sock != NULL);
int rc = zmq_setsockopt(sock, ZMQ_SUBSCRIBE, "", 0);
g_assert(rc == 0);
rc = zmq_connect(sock, "inproc://logs");
g_assert(rc == 0);
while(true)
{
zmq_msg_t message;
int rc = zmq_msg_init(&message);
g_assert(rc == 0);
rc = zmq_recv(sock, &message, 0);
g_assert(rc == 0);
// Zero length message is a log shutdown indicator.
if(zmq_msg_size(&message) == 0)
break;
json_t *log_msg_json = json_loads(zmq_msg_data(&message), 0, NULL);
g_assert(log_msg_json != NULL);
json_decref(log_msg_json);
zmq_msg_close(&message);
}
rc = zmq_close(sock);
g_assert(rc == 0);
return NULL;
}
static void setup_lj()
{
void *ctx = zmq_init(1);
lj_init(ctx);
lj_bind("inproc://logs");
// Receiver.
//GThread *thread = g_thread_create(lj_receiver, ctx, true, NULL);
//sleep(1);
}
static FILE *LOG_FILE;
static const char *json_format = "{\"level\":%d,\"errno\":\"%s\",\"file\":\"%s\",\"line\":%d,\"msg\":\"%s\"}\n";
static void log_to_file(int level, const char *errno_str, const char *file, int line, const char *format, ...)
{
va_list args;
va_start(args, format);
char *message = g_strdup_vprintf(format, args);
va_end(args);
char *data = g_strdup_printf(json_format, level, (errno_str == NULL) ? "null" : errno_str, (file == NULL) ? "null" : file, line, message);
fprintf(LOG_FILE, "%s", data);
g_free(message);
g_free(data);
}
#define file_log_fatal(format, ...) \
log_to_file(LJ_LOG_FATAL, clean_errno(), __FILE__, __LINE__, format, ##__VA_ARGS__)
static void setup_file()
{
LOG_FILE = fopen("dummy.log", "a+");
}
static const int PASSES = 10000;
int main(int argc, char **argv)
{
g_thread_init(NULL);
setup_file();
setup_lj();
Timer *timer = sgl_timer_new();
// Test file.
for(int i = 0; i < PASSES; i++)
{
sgl_timer_reset(timer);
for(int j = 0; j < 20; j++)
{
file_log_fatal("oh my gawd an emergency! %d", i);
}
unsigned long file_pass = sgl_timer_elapsed_microseconds(timer) / 20;
printf("%lu ", file_pass);
sgl_timer_reset(timer);
for(int j = 0; j < 20; j++)
{
lj_log_fatal("oh my gawd an emergency! %d", i);
}
unsigned long zmq_pass = sgl_timer_elapsed_microseconds(timer) / 20;
printf("%lu\n", zmq_pass);
sleep(0);
}
sgl_timer_free(timer);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment