mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-10-16 18:56:54 +02:00
lang/c/msgpack: c-macro based template
git-svn-id: file:///Users/frsyuki/project/msgpack-git/svn/x@66 5a5092ae-2292-43ba-b2d5-dcab9c1a2731
This commit is contained in:
@@ -2,17 +2,12 @@ lib_LTLIBRARIES = libmsgpackc.la
|
||||
|
||||
libmsgpackc_la_SOURCES = \
|
||||
pack.c \
|
||||
unpack.c \
|
||||
unpack_inline.c
|
||||
unpack.c
|
||||
|
||||
nobase_include_HEADERS = \
|
||||
msgpack.h \
|
||||
msgpack/pack.h \
|
||||
msgpack/unpack.h
|
||||
|
||||
noinst_HEADERS = \
|
||||
pack_inline.h \
|
||||
unpack_context.h
|
||||
|
||||
libmsgpackc_la_LDFLAGS = -version-info 0:0:0
|
||||
|
||||
|
86
c/bench.c
86
c/bench.c
@@ -16,14 +16,15 @@ void reset_timer()
|
||||
gettimeofday(&g_timer, NULL);
|
||||
}
|
||||
|
||||
double show_timer()
|
||||
void show_timer(size_t bufsz)
|
||||
{
|
||||
struct timeval endtime;
|
||||
gettimeofday(&endtime, NULL);
|
||||
double sec = (endtime.tv_sec - g_timer.tv_sec)
|
||||
+ (double)(endtime.tv_usec - g_timer.tv_usec) / 1000 / 1000;
|
||||
printf("%f sec\n", sec);
|
||||
return sec;
|
||||
printf("%f MB\n", ((double)bufsz)/1024/1024);
|
||||
printf("%f Mbps\n", ((double)bufsz)*8/sec/1000/1000);
|
||||
}
|
||||
|
||||
|
||||
@@ -38,32 +39,33 @@ static int reformat_start_array(void * ctx) { return 1; }
|
||||
static int reformat_end_array(void * ctx) { return 1; }
|
||||
|
||||
|
||||
static void* unpack_unsigned_int_8(void* data, uint8_t d) { return NULL; }
|
||||
static void* unpack_unsigned_int_16(void* data, uint16_t d) { return NULL; }
|
||||
static void* unpack_unsigned_int_32(void* data, uint32_t d) { return NULL; }
|
||||
static void* unpack_unsigned_int_64(void* data, uint64_t d) { return NULL; }
|
||||
static void* unpack_signed_int_8(void* data, int8_t d) { return NULL; }
|
||||
static void* unpack_signed_int_16(void* data, int16_t d) { return NULL; }
|
||||
static void* unpack_signed_int_32(void* data, int32_t d) { return NULL; }
|
||||
static void* unpack_signed_int_64(void* data, int64_t d) { return NULL; }
|
||||
static void* unpack_uint8(void* data, uint8_t d) { return NULL; }
|
||||
static void* unpack_uint16(void* data, uint16_t d) { return NULL; }
|
||||
static void* unpack_uint32(void* data, uint32_t d) { return NULL; }
|
||||
static void* unpack_uint64(void* data, uint64_t d) { return NULL; }
|
||||
static void* unpack_int8(void* data, int8_t d) { return NULL; }
|
||||
static void* unpack_int16(void* data, int16_t d) { return NULL; }
|
||||
static void* unpack_int32(void* data, int32_t d) { return NULL; }
|
||||
static void* unpack_int64(void* data, int64_t d) { return NULL; }
|
||||
static void* unpack_float(void* data, float d) { return NULL; }
|
||||
static void* unpack_double(void* data, double d) { return NULL; }
|
||||
static void* unpack_nil(void* data) { return NULL; }
|
||||
static void* unpack_true(void* data) { return NULL; }
|
||||
static void* unpack_false(void* data) { return NULL; }
|
||||
static void* unpack_array_start(void* data, unsigned int n) { return NULL; }
|
||||
static void* unpack_array(void* data, unsigned int n) { return NULL; }
|
||||
static void unpack_array_item(void* data, void* c, void* o) { }
|
||||
static void* unpack_map_start(void* data, unsigned int n) { return NULL; }
|
||||
static void* unpack_map(void* data, unsigned int n) { return NULL; }
|
||||
static void unpack_map_item(void* data, void* c, void* k, void* v) { }
|
||||
static void* unpack_raw(void* data, const char* b, const char* p, unsigned int l) { /*printf("unpack raw %p %lu\n",p,l);*/ return NULL; }
|
||||
|
||||
|
||||
typedef struct {
|
||||
size_t allocated;
|
||||
size_t length;
|
||||
char* buffer;
|
||||
} pack_buffer;
|
||||
|
||||
static const size_t PACK_INITIAL_BUFFER_SIZE = 512;
|
||||
static const size_t PACK_INITIAL_BUFFER_SIZE = 32*1024;
|
||||
|
||||
static void pack_buffer_init(pack_buffer* data)
|
||||
{
|
||||
@@ -84,7 +86,7 @@ static void pack_buffer_free(pack_buffer* data)
|
||||
free(data->buffer);
|
||||
}
|
||||
|
||||
static void pack_append_buffer(void* user, const unsigned char* b, unsigned int l)
|
||||
static void pack_append_buffer(void* user, const char* b, unsigned int l)
|
||||
{
|
||||
pack_buffer* data = (pack_buffer*)user;
|
||||
if(data->allocated - data->length < l) {
|
||||
@@ -128,7 +130,6 @@ void bench_json(void)
|
||||
yajl_handle h = yajl_alloc(&callbacks, &hcfg, NULL);
|
||||
|
||||
|
||||
double sec;
|
||||
const unsigned char * buf;
|
||||
unsigned int len;
|
||||
|
||||
@@ -143,11 +144,9 @@ void bench_json(void)
|
||||
}
|
||||
yajl_gen_array_close(g);
|
||||
}
|
||||
sec = show_timer();
|
||||
show_timer(len);
|
||||
|
||||
yajl_gen_get_buf(g, &buf, &len);
|
||||
printf("%u KB\n", len / 1024);
|
||||
printf("%f MB/s\n", len / sec / 1024 / 1024);
|
||||
|
||||
puts("----");
|
||||
puts("parse integer");
|
||||
@@ -159,9 +158,7 @@ void bench_json(void)
|
||||
fprintf(stderr, (const char *) str);
|
||||
}
|
||||
}
|
||||
sec = show_timer();
|
||||
|
||||
printf("%f MB/s\n", len / sec / 1024 / 1024);
|
||||
show_timer(len);
|
||||
|
||||
|
||||
//yajl_gen_clear(g);
|
||||
@@ -182,11 +179,9 @@ void bench_json(void)
|
||||
}
|
||||
yajl_gen_array_close(g);
|
||||
}
|
||||
sec = show_timer();
|
||||
show_timer(len);
|
||||
|
||||
yajl_gen_get_buf(g, &buf, &len);
|
||||
printf("%u KB\n", len / 1024);
|
||||
printf("%f MB/s\n", len / sec / 1024 / 1024);
|
||||
|
||||
puts("----");
|
||||
puts("parse string");
|
||||
@@ -198,15 +193,14 @@ void bench_json(void)
|
||||
fprintf(stderr, (const char *) str);
|
||||
}
|
||||
}
|
||||
sec = show_timer();
|
||||
|
||||
printf("%f MB/s\n", len / sec / 1024 / 1024);
|
||||
show_timer(len);
|
||||
|
||||
|
||||
yajl_gen_free(g);
|
||||
yajl_free(h);
|
||||
}
|
||||
|
||||
|
||||
void bench_msgpack(void)
|
||||
{
|
||||
puts("== MessagePack ==");
|
||||
@@ -214,33 +208,33 @@ void bench_msgpack(void)
|
||||
|
||||
pack_buffer mpkbuf;
|
||||
pack_buffer_init(&mpkbuf);
|
||||
|
||||
msgpack_pack_t* mpk = msgpack_pack_new(
|
||||
&mpkbuf, pack_append_buffer);
|
||||
|
||||
msgpack_unpack_callback cb = {
|
||||
unpack_unsigned_int_8,
|
||||
unpack_unsigned_int_16,
|
||||
unpack_unsigned_int_32,
|
||||
unpack_unsigned_int_64,
|
||||
unpack_signed_int_8,
|
||||
unpack_signed_int_16,
|
||||
unpack_signed_int_32,
|
||||
unpack_signed_int_64,
|
||||
unpack_uint8,
|
||||
unpack_uint16,
|
||||
unpack_uint32,
|
||||
unpack_uint64,
|
||||
unpack_int8,
|
||||
unpack_int16,
|
||||
unpack_int32,
|
||||
unpack_int64,
|
||||
unpack_float,
|
||||
unpack_double,
|
||||
unpack_nil,
|
||||
unpack_true,
|
||||
unpack_false,
|
||||
unpack_array_start,
|
||||
unpack_array,
|
||||
unpack_array_item,
|
||||
unpack_map_start,
|
||||
unpack_map,
|
||||
unpack_map_item,
|
||||
unpack_raw,
|
||||
};
|
||||
msgpack_unpack_t* mupk = msgpack_unpack_new(NULL, &cb);
|
||||
|
||||
|
||||
double sec;
|
||||
size_t len;
|
||||
const char* buf;
|
||||
|
||||
@@ -254,12 +248,10 @@ void bench_msgpack(void)
|
||||
msgpack_pack_unsigned_int(mpk, i);
|
||||
}
|
||||
}
|
||||
sec = show_timer();
|
||||
show_timer(mpkbuf.length);
|
||||
|
||||
len = mpkbuf.length;
|
||||
buf = mpkbuf.buffer;
|
||||
printf("%lu KB\n", len / 1024);
|
||||
printf("%f MB/s\n", len / sec / 1024 / 1024);
|
||||
|
||||
puts("----");
|
||||
puts("unpack integer");
|
||||
@@ -273,9 +265,7 @@ void bench_msgpack(void)
|
||||
fprintf(stderr, "Not finished.\n");
|
||||
}
|
||||
}
|
||||
sec = show_timer();
|
||||
|
||||
printf("%f MB/s\n", len / sec / 1024 / 1024);
|
||||
show_timer(mpkbuf.length);
|
||||
|
||||
|
||||
pack_buffer_reset(&mpkbuf);
|
||||
@@ -292,12 +282,10 @@ void bench_msgpack(void)
|
||||
msgpack_pack_raw(mpk, TASK_STR_PTR, i);
|
||||
}
|
||||
}
|
||||
sec = show_timer();
|
||||
show_timer(mpkbuf.length);
|
||||
|
||||
len = mpkbuf.length;
|
||||
buf = mpkbuf.buffer;
|
||||
printf("%lu KB\n", len / 1024);
|
||||
printf("%f MB/s\n", len / sec / 1024 / 1024);
|
||||
|
||||
puts("----");
|
||||
puts("unpack string");
|
||||
@@ -311,9 +299,7 @@ void bench_msgpack(void)
|
||||
fprintf(stderr, "Not finished.\n");
|
||||
}
|
||||
}
|
||||
sec = show_timer();
|
||||
|
||||
printf("%f MB/s\n", len / sec / 1024 / 1024);
|
||||
show_timer(mpkbuf.length);
|
||||
|
||||
|
||||
msgpack_unpack_free(mupk);
|
||||
|
9
c/bench.mk
Normal file
9
c/bench.mk
Normal file
@@ -0,0 +1,9 @@
|
||||
|
||||
CFLAGS += -Wall -g -I. -I.. -O4
|
||||
LDFLAGS += -lyajl
|
||||
|
||||
all: bench
|
||||
|
||||
bench: bench.o pack.o unpack.o pack.h unpack.h
|
||||
$(CC) bench.o pack.o unpack.o $(CFLAGS) $(LDFLAGS) -o $@
|
||||
|
325
c/bench_inline.c
325
c/bench_inline.c
@@ -1,325 +0,0 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#include <msgpack/pack.h>
|
||||
#include <msgpack/unpack.h>
|
||||
#include <yajl/yajl_parse.h>
|
||||
#include <yajl/yajl_gen.h>
|
||||
|
||||
|
||||
static struct timeval g_timer;
|
||||
|
||||
void reset_timer()
|
||||
{
|
||||
gettimeofday(&g_timer, NULL);
|
||||
}
|
||||
|
||||
double show_timer()
|
||||
{
|
||||
struct timeval endtime;
|
||||
gettimeofday(&endtime, NULL);
|
||||
double sec = (endtime.tv_sec - g_timer.tv_sec)
|
||||
+ (double)(endtime.tv_usec - g_timer.tv_usec) / 1000 / 1000;
|
||||
printf("%f sec\n", sec);
|
||||
return sec;
|
||||
}
|
||||
|
||||
|
||||
static int reformat_null(void * ctx) { return 1; }
|
||||
static int reformat_boolean(void * ctx, int boolean) { return 1; }
|
||||
static int reformat_number(void * ctx, const char * s, unsigned int l) { return 1; }
|
||||
static int reformat_string(void * ctx, const unsigned char * stringVal, unsigned int stringLen) { return 1; }
|
||||
static int reformat_map_key(void * ctx, const unsigned char * stringVal, unsigned int stringLen) { return 1; }
|
||||
static int reformat_start_map(void * ctx) { return 1; }
|
||||
static int reformat_end_map(void * ctx) { return 1; }
|
||||
static int reformat_start_array(void * ctx) { return 1; }
|
||||
static int reformat_end_array(void * ctx) { return 1; }
|
||||
|
||||
|
||||
typedef void* msgpack_object;
|
||||
|
||||
typedef struct {
|
||||
} msgpack_unpack_context;
|
||||
|
||||
#include "msgpack/unpack/inline_context.h"
|
||||
|
||||
static inline void* msgpack_unpack_init(msgpack_unpack_context* x) { return NULL; }
|
||||
static inline void* msgpack_unpack_unsigned_int_8(msgpack_unpack_context* x, uint8_t d) { return NULL; }
|
||||
static inline void* msgpack_unpack_unsigned_int_16(msgpack_unpack_context* x, uint16_t d) { return NULL; }
|
||||
static inline void* msgpack_unpack_unsigned_int_32(msgpack_unpack_context* x, uint32_t d) { return NULL; }
|
||||
static inline void* msgpack_unpack_unsigned_int_64(msgpack_unpack_context* x, uint64_t d) { return NULL; }
|
||||
static inline void* msgpack_unpack_signed_int_8(msgpack_unpack_context* x, int8_t d) { return NULL; }
|
||||
static inline void* msgpack_unpack_signed_int_16(msgpack_unpack_context* x, int16_t d) { return NULL; }
|
||||
static inline void* msgpack_unpack_signed_int_32(msgpack_unpack_context* x, int32_t d) { return NULL; }
|
||||
static inline void* msgpack_unpack_signed_int_64(msgpack_unpack_context* x, int64_t d) { return NULL; }
|
||||
static inline void* msgpack_unpack_float(msgpack_unpack_context* x, float d) { return NULL; }
|
||||
static inline void* msgpack_unpack_double(msgpack_unpack_context* x, double d) { return NULL; }
|
||||
static inline void* msgpack_unpack_nil(msgpack_unpack_context* x) { return NULL; }
|
||||
static inline void* msgpack_unpack_true(msgpack_unpack_context* x) { return NULL; }
|
||||
static inline void* msgpack_unpack_false(msgpack_unpack_context* x) { return NULL; }
|
||||
static inline void* msgpack_unpack_array_start(msgpack_unpack_context* x, unsigned int n) { return NULL; }
|
||||
static inline void msgpack_unpack_array_item(msgpack_unpack_context* x, void* c, void* o) { }
|
||||
static inline void* msgpack_unpack_map_start(msgpack_unpack_context* x, unsigned int n) { return NULL; }
|
||||
static inline void msgpack_unpack_map_item(msgpack_unpack_context* x, void* c, void* k, void* v) { }
|
||||
static inline void* msgpack_unpack_raw(msgpack_unpack_context* x, const void* b, const void* p, size_t l) { return NULL; }
|
||||
|
||||
#include "msgpack/unpack/inline_impl.h"
|
||||
|
||||
typedef struct {
|
||||
size_t allocated;
|
||||
size_t length;
|
||||
char* buffer;
|
||||
} pack_buffer;
|
||||
|
||||
static const size_t PACK_INITIAL_BUFFER_SIZE = 512;
|
||||
|
||||
static void pack_buffer_init(pack_buffer* data)
|
||||
{
|
||||
data->buffer = malloc(PACK_INITIAL_BUFFER_SIZE);
|
||||
data->length = 0;
|
||||
data->allocated = PACK_INITIAL_BUFFER_SIZE;
|
||||
}
|
||||
|
||||
static void pack_buffer_reset(pack_buffer* data)
|
||||
{
|
||||
data->buffer = realloc(data->buffer, PACK_INITIAL_BUFFER_SIZE);
|
||||
data->allocated = PACK_INITIAL_BUFFER_SIZE;
|
||||
data->length = 0;
|
||||
}
|
||||
|
||||
static void pack_buffer_free(pack_buffer* data)
|
||||
{
|
||||
free(data->buffer);
|
||||
}
|
||||
|
||||
static void pack_append_buffer(void* user, const unsigned char* b, unsigned int l)
|
||||
{
|
||||
pack_buffer* data = (pack_buffer*)user;
|
||||
if(data->allocated - data->length < l) {
|
||||
data->buffer = realloc(data->buffer, data->allocated*2);
|
||||
data->allocated *= 2;
|
||||
}
|
||||
memcpy(data->buffer + data->length, b, l);
|
||||
data->length += l;
|
||||
}
|
||||
|
||||
|
||||
static const unsigned int TASK_INT_NUM = 1<<24;
|
||||
static const unsigned int TASK_STR_LEN = 1<<15;
|
||||
//static const unsigned int TASK_INT_NUM = 1<<20;
|
||||
//static const unsigned int TASK_STR_LEN = 1<<12;
|
||||
static const char* TASK_STR_PTR;
|
||||
|
||||
|
||||
void bench_json(void)
|
||||
{
|
||||
puts("== JSON ==");
|
||||
|
||||
|
||||
yajl_gen_config gcfg = {0, NULL};
|
||||
yajl_gen g = yajl_gen_alloc(&gcfg);
|
||||
|
||||
yajl_parser_config hcfg = { 0, 0 };
|
||||
yajl_callbacks callbacks = {
|
||||
reformat_null,
|
||||
reformat_boolean,
|
||||
NULL,
|
||||
NULL,
|
||||
reformat_number,
|
||||
reformat_start_map,
|
||||
reformat_map_key,
|
||||
reformat_end_map,
|
||||
reformat_start_array,
|
||||
reformat_end_array
|
||||
};
|
||||
yajl_handle h = yajl_alloc(&callbacks, &hcfg, NULL);
|
||||
|
||||
|
||||
double sec;
|
||||
const unsigned char * buf;
|
||||
unsigned int len;
|
||||
|
||||
|
||||
puts("generate integer");
|
||||
reset_timer();
|
||||
{
|
||||
unsigned int i;
|
||||
yajl_gen_array_open(g);
|
||||
for(i=0; i < TASK_INT_NUM; ++i) {
|
||||
yajl_gen_integer(g, i);
|
||||
}
|
||||
yajl_gen_array_close(g);
|
||||
}
|
||||
sec = show_timer();
|
||||
|
||||
yajl_gen_get_buf(g, &buf, &len);
|
||||
printf("%u KB\n", len / 1024);
|
||||
printf("%f Mbps\n", len / sec / 1024 / 1024);
|
||||
|
||||
puts("----");
|
||||
puts("parse integer");
|
||||
reset_timer();
|
||||
{
|
||||
yajl_status stat = yajl_parse(h, buf, len);
|
||||
if (stat != yajl_status_ok && stat != yajl_status_insufficient_data) {
|
||||
unsigned char * str = yajl_get_error(h, 1, buf, len);
|
||||
fprintf(stderr, (const char *) str);
|
||||
}
|
||||
}
|
||||
sec = show_timer();
|
||||
|
||||
printf("%f Mbps\n", len / sec / 1024 / 1024);
|
||||
|
||||
|
||||
//yajl_gen_clear(g);
|
||||
yajl_gen_free(g);
|
||||
g = yajl_gen_alloc(&gcfg);
|
||||
yajl_free(h);
|
||||
h = yajl_alloc(&callbacks, &hcfg, NULL);
|
||||
|
||||
|
||||
puts("----");
|
||||
puts("generate string");
|
||||
reset_timer();
|
||||
{
|
||||
unsigned int i;
|
||||
yajl_gen_array_open(g);
|
||||
for(i=0; i < TASK_STR_LEN; ++i) {
|
||||
yajl_gen_string(g, (const unsigned char*)TASK_STR_PTR, i);
|
||||
}
|
||||
yajl_gen_array_close(g);
|
||||
}
|
||||
sec = show_timer();
|
||||
|
||||
yajl_gen_get_buf(g, &buf, &len);
|
||||
printf("%u KB\n", len / 1024);
|
||||
printf("%f Mbps\n", len / sec / 1024 / 1024);
|
||||
|
||||
puts("----");
|
||||
puts("parse string");
|
||||
reset_timer();
|
||||
{
|
||||
yajl_status stat = yajl_parse(h, buf, len);
|
||||
if (stat != yajl_status_ok && stat != yajl_status_insufficient_data) {
|
||||
unsigned char * str = yajl_get_error(h, 1, buf, len);
|
||||
fprintf(stderr, (const char *) str);
|
||||
}
|
||||
}
|
||||
sec = show_timer();
|
||||
|
||||
printf("%f Mbps\n", len / sec / 1024 / 1024);
|
||||
|
||||
|
||||
yajl_gen_free(g);
|
||||
yajl_free(h);
|
||||
}
|
||||
|
||||
void bench_msgpack(void)
|
||||
{
|
||||
puts("== MessagePack ==");
|
||||
|
||||
|
||||
pack_buffer mpkbuf;
|
||||
pack_buffer_init(&mpkbuf);
|
||||
msgpack_pack_t* mpk = msgpack_pack_new(
|
||||
&mpkbuf, pack_append_buffer);
|
||||
|
||||
msgpack_unpacker mupk;
|
||||
msgpack_unpacker_init(&mupk);
|
||||
|
||||
double sec;
|
||||
size_t len;
|
||||
const char* buf;
|
||||
|
||||
|
||||
puts("pack integer");
|
||||
reset_timer();
|
||||
{
|
||||
unsigned int i;
|
||||
msgpack_pack_array(mpk, TASK_INT_NUM);
|
||||
for(i=0; i < TASK_INT_NUM; ++i) {
|
||||
msgpack_pack_unsigned_int(mpk, i);
|
||||
}
|
||||
}
|
||||
sec = show_timer();
|
||||
|
||||
len = mpkbuf.length;
|
||||
buf = mpkbuf.buffer;
|
||||
printf("%lu KB\n", len / 1024);
|
||||
printf("%f Mbps\n", len / sec / 1024 / 1024);
|
||||
|
||||
puts("----");
|
||||
puts("unpack integer");
|
||||
reset_timer();
|
||||
{
|
||||
size_t off = 0;
|
||||
int ret = msgpack_unpacker_execute(&mupk, buf, len, &off);
|
||||
if(ret < 0) {
|
||||
fprintf(stderr, "Parse error.\n");
|
||||
} else if(ret == 0) {
|
||||
fprintf(stderr, "Not finished.\n");
|
||||
}
|
||||
}
|
||||
sec = show_timer();
|
||||
|
||||
printf("%f Mbps\n", len / sec / 1024 / 1024);
|
||||
|
||||
|
||||
pack_buffer_reset(&mpkbuf);
|
||||
msgpack_unpacker_init(&mupk);
|
||||
|
||||
|
||||
puts("----");
|
||||
puts("pack string");
|
||||
reset_timer();
|
||||
{
|
||||
unsigned int i;
|
||||
msgpack_pack_array(mpk, TASK_STR_LEN);
|
||||
for(i=0; i < TASK_STR_LEN; ++i) {
|
||||
msgpack_pack_raw(mpk, TASK_STR_PTR, i);
|
||||
}
|
||||
}
|
||||
sec = show_timer();
|
||||
|
||||
len = mpkbuf.length;
|
||||
buf = mpkbuf.buffer;
|
||||
printf("%lu KB\n", len / 1024);
|
||||
printf("%f Mbps\n", len / sec / 1024 / 1024);
|
||||
|
||||
puts("----");
|
||||
puts("unpack string");
|
||||
reset_timer();
|
||||
{
|
||||
size_t off = 0;
|
||||
int ret = msgpack_unpacker_execute(&mupk, buf, len, &off);
|
||||
if(ret < 0) {
|
||||
fprintf(stderr, "Parse error.\n");
|
||||
} else if(ret == 0) {
|
||||
fprintf(stderr, "Not finished.\n");
|
||||
}
|
||||
}
|
||||
sec = show_timer();
|
||||
|
||||
printf("%f Mbps\n", len / sec / 1024 / 1024);
|
||||
|
||||
|
||||
msgpack_pack_free(mpk);
|
||||
pack_buffer_free(&mpkbuf);
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
char* str = malloc(TASK_STR_LEN);
|
||||
memset(str, 'a', TASK_STR_LEN);
|
||||
TASK_STR_PTR = str;
|
||||
|
||||
bench_msgpack();
|
||||
//bench_json();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
21
c/pack.c
21
c/pack.c
@@ -15,11 +15,21 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include "pack_inline.h"
|
||||
#include "msgpack/pack.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
msgpack_pack_t* msgpack_pack_new(void* data, msgpack_pack_callback_t callback)
|
||||
|
||||
#define msgpack_pack_inline_func(name) \
|
||||
void msgpack_pack_##name
|
||||
|
||||
#define msgpack_pack_user msgpack_pack_t*
|
||||
|
||||
#define msgpack_pack_append_buffer(user, buf, len) \
|
||||
(*(user)->callback)((user)->data, (const char*)buf, len)
|
||||
|
||||
#include "msgpack/pack_template.h"
|
||||
|
||||
msgpack_pack_t* msgpack_pack_new(void* data, msgpack_pack_append_buffer_t callback)
|
||||
{
|
||||
msgpack_pack_t* ctx = calloc(1, sizeof(msgpack_pack_t));
|
||||
if(!ctx) { return NULL; }
|
||||
@@ -33,8 +43,3 @@ void msgpack_pack_free(msgpack_pack_t* ctx)
|
||||
free(ctx);
|
||||
}
|
||||
|
||||
static inline void msgpack_pack_append_buffer(msgpack_pack_t* ctx, const unsigned char* b, unsigned int l)
|
||||
{
|
||||
ctx->callback(ctx->data, b, l);
|
||||
}
|
||||
|
||||
|
33
c/pack.h
33
c/pack.h
@@ -21,26 +21,32 @@
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
typedef void (*msgpack_pack_callback_t)(void* data, const unsigned char* b, unsigned int i);
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
typedef void (*msgpack_pack_append_buffer_t)(void* data, const char* b, unsigned int i);
|
||||
|
||||
typedef struct {
|
||||
void* data;
|
||||
msgpack_pack_callback_t callback;
|
||||
msgpack_pack_append_buffer_t callback;
|
||||
} msgpack_pack_t;
|
||||
|
||||
msgpack_pack_t* msgpack_pack_new(void* data, msgpack_pack_callback_t callback);
|
||||
msgpack_pack_t* msgpack_pack_new(void* data, msgpack_pack_append_buffer_t callback);
|
||||
|
||||
void msgpack_pack_free(msgpack_pack_t* ctx);
|
||||
|
||||
void msgpack_pack_int(msgpack_pack_t* ctx, int d);
|
||||
void msgpack_pack_unsigned_int(msgpack_pack_t* ctx, unsigned int d);
|
||||
void msgpack_pack_unsigned_int_8(msgpack_pack_t* ctx, uint8_t d);
|
||||
void msgpack_pack_unsigned_int_16(msgpack_pack_t* ctx, uint16_t d);
|
||||
void msgpack_pack_unsigned_int_32(msgpack_pack_t* ctx, uint32_t d);
|
||||
void msgpack_pack_unsigned_int_64(msgpack_pack_t* ctx, uint64_t d);
|
||||
void msgpack_pack_signed_int_8(msgpack_pack_t* ctx, int8_t d);
|
||||
void msgpack_pack_signed_int_16(msgpack_pack_t* ctx, int16_t d);
|
||||
void msgpack_pack_signed_int_32(msgpack_pack_t* ctx, int32_t d);
|
||||
void msgpack_pack_signed_int_64(msgpack_pack_t* ctx, int64_t d);
|
||||
void msgpack_pack_uint8(msgpack_pack_t* ctx, uint8_t d);
|
||||
void msgpack_pack_uint16(msgpack_pack_t* ctx, uint16_t d);
|
||||
void msgpack_pack_uint32(msgpack_pack_t* ctx, uint32_t d);
|
||||
void msgpack_pack_uint64(msgpack_pack_t* ctx, uint64_t d);
|
||||
void msgpack_pack_int8(msgpack_pack_t* ctx, int8_t d);
|
||||
void msgpack_pack_int16(msgpack_pack_t* ctx, int16_t d);
|
||||
void msgpack_pack_int32(msgpack_pack_t* ctx, int32_t d);
|
||||
void msgpack_pack_int64(msgpack_pack_t* ctx, int64_t d);
|
||||
void msgpack_pack_float(msgpack_pack_t* ctx, float d);
|
||||
void msgpack_pack_double(msgpack_pack_t* ctx, double d);
|
||||
void msgpack_pack_nil(msgpack_pack_t* ctx);
|
||||
@@ -51,5 +57,10 @@ void msgpack_pack_map(msgpack_pack_t* ctx, unsigned int n);
|
||||
void msgpack_pack_string(msgpack_pack_t* ctx, const char* b);
|
||||
void msgpack_pack_raw(msgpack_pack_t* ctx, const void* b, size_t l);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* msgpack/pack.h */
|
||||
|
||||
|
@@ -1,32 +0,0 @@
|
||||
/*
|
||||
* MessagePack packing routine for C
|
||||
*
|
||||
* Copyright (C) 2008 FURUHASHI Sadayuki
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef PACK_INLINE_H__
|
||||
#define PACK_INLINE_H__
|
||||
|
||||
#include "msgpack/pack.h"
|
||||
|
||||
typedef msgpack_pack_t* msgpack_pack_context;
|
||||
|
||||
static inline void msgpack_pack_append_buffer(msgpack_pack_t* x, const unsigned char* b, unsigned int l);
|
||||
|
||||
#include <string.h>
|
||||
#include <arpa/inet.h> /* __BYTE_ORDER */
|
||||
#include "msgpack/pack/inline_impl.h"
|
||||
|
||||
#endif /* pack_inline.h */
|
||||
|
112
c/unpack.c
112
c/unpack.c
@@ -16,15 +16,101 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include "msgpack/unpack.h"
|
||||
#include "unpack_context.h"
|
||||
#include "msgpack/unpack_define.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
#define msgpack_unpack_struct(name) \
|
||||
struct template_##name
|
||||
|
||||
#define msgpack_unpack_func(ret, name) \
|
||||
ret template_func_##name
|
||||
|
||||
#define msgpack_unpack_callback(name) \
|
||||
template_callback_##name
|
||||
|
||||
#define msgpack_unpack_object void*
|
||||
|
||||
#define msgpack_unpack_user msgpack_unpack_t
|
||||
|
||||
|
||||
struct template_context;
|
||||
|
||||
static void template_func_init(struct template_context* ctx);
|
||||
|
||||
static void* template_func_data(struct template_context* ctx);
|
||||
|
||||
static int template_func_execute(struct template_context* ctx,
|
||||
const char* data, size_t len, size_t* off);
|
||||
|
||||
|
||||
static inline void* template_callback_init(msgpack_unpack_t* x)
|
||||
{ return NULL; }
|
||||
|
||||
static inline void* template_callback_uint8(msgpack_unpack_t* x, uint8_t d)
|
||||
{ return x->callback.unpack_uint8(x->data, d); }
|
||||
|
||||
static inline void* template_callback_uint16(msgpack_unpack_t* x, uint16_t d)
|
||||
{ return x->callback.unpack_uint16(x->data, d); }
|
||||
|
||||
static inline void* template_callback_uint32(msgpack_unpack_t* x, uint32_t d)
|
||||
{ return x->callback.unpack_uint32(x->data, d); }
|
||||
|
||||
static inline void* template_callback_uint64(msgpack_unpack_t* x, uint64_t d)
|
||||
{ return x->callback.unpack_uint64(x->data, d); }
|
||||
|
||||
static inline void* template_callback_int8(msgpack_unpack_t* x, int8_t d)
|
||||
{ return x->callback.unpack_int8(x->data, d); }
|
||||
|
||||
static inline void* template_callback_int16(msgpack_unpack_t* x, int16_t d)
|
||||
{ return x->callback.unpack_int16(x->data, d); }
|
||||
|
||||
static inline void* template_callback_int32(msgpack_unpack_t* x, int32_t d)
|
||||
{ return x->callback.unpack_int32(x->data, d); }
|
||||
|
||||
static inline void* template_callback_int64(msgpack_unpack_t* x, int64_t d)
|
||||
{ return x->callback.unpack_int64(x->data, d); }
|
||||
|
||||
static inline void* template_callback_float(msgpack_unpack_t* x, float d)
|
||||
{ return x->callback.unpack_float(x->data, d); }
|
||||
|
||||
static inline void* template_callback_double(msgpack_unpack_t* x, double d)
|
||||
{ return x->callback.unpack_double(x->data, d); }
|
||||
|
||||
static inline void* template_callback_nil(msgpack_unpack_t* x)
|
||||
{ return x->callback.unpack_nil(x->data); }
|
||||
|
||||
static inline void* template_callback_true(msgpack_unpack_t* x)
|
||||
{ return x->callback.unpack_true(x->data); }
|
||||
|
||||
static inline void* template_callback_false(msgpack_unpack_t* x)
|
||||
{ return x->callback.unpack_false(x->data); }
|
||||
|
||||
static inline void* template_callback_array(msgpack_unpack_t* x, unsigned int n)
|
||||
{ return x->callback.unpack_array(x->data, n); }
|
||||
|
||||
static inline void template_callback_array_item(msgpack_unpack_t* x, void* c, void* o)
|
||||
{ x->callback.unpack_array_item(x->data, c, o); }
|
||||
|
||||
static inline void* template_callback_map(msgpack_unpack_t* x, unsigned int n)
|
||||
{ return x->callback.unpack_map(x->data, n); }
|
||||
|
||||
static inline void template_callback_map_item(msgpack_unpack_t* x, void* c, void* k, void* v)
|
||||
{ x->callback.unpack_map_item(x->data, c, k, v); }
|
||||
|
||||
static inline void* template_callback_raw(msgpack_unpack_t* x, const char* b, const char* p, unsigned int l)
|
||||
{ return x->callback.unpack_raw(x->data, b, p, l); }
|
||||
|
||||
|
||||
#include "msgpack/unpack_template.h"
|
||||
|
||||
|
||||
msgpack_unpack_t* msgpack_unpack_new(void* data, msgpack_unpack_callback* callback)
|
||||
{
|
||||
msgpack_unpacker* ctx;
|
||||
ctx = (msgpack_unpacker*)calloc(1, sizeof(msgpack_unpacker));
|
||||
struct template_context* ctx;
|
||||
ctx = (struct template_context*)calloc(1, sizeof(struct template_context));
|
||||
if(ctx == NULL) { return NULL; }
|
||||
msgpack_unpacker_init(ctx);
|
||||
template_func_init(ctx);
|
||||
((msgpack_unpack_t*)ctx)->data = data;
|
||||
((msgpack_unpack_t*)ctx)->callback = *callback;
|
||||
return (msgpack_unpack_t*)ctx;
|
||||
@@ -32,25 +118,27 @@ msgpack_unpack_t* msgpack_unpack_new(void* data, msgpack_unpack_callback* callba
|
||||
|
||||
void msgpack_unpack_free(msgpack_unpack_t* ctx)
|
||||
{
|
||||
free((msgpack_unpacker*)ctx);
|
||||
free((struct template_context*)ctx);
|
||||
}
|
||||
|
||||
void* msgpack_unpack_data(msgpack_unpack_t* ctx)
|
||||
{
|
||||
return msgpack_unpacker_data((msgpack_unpacker*)ctx);
|
||||
return template_func_data((struct template_context*)ctx);
|
||||
}
|
||||
|
||||
void msgpack_unpack_reset(msgpack_unpack_t* ctx)
|
||||
{
|
||||
msgpack_unpack_t x = ((msgpack_unpacker*)ctx)->user;
|
||||
msgpack_unpacker_init((msgpack_unpacker*)ctx);
|
||||
((msgpack_unpacker*)ctx)->user = x;
|
||||
msgpack_unpack_t x = ((struct template_context*)ctx)->user;
|
||||
template_func_init((struct template_context*)ctx);
|
||||
((struct template_context*)ctx)->user = x;
|
||||
}
|
||||
|
||||
int msgpack_unpack_execute(msgpack_unpack_t* ctx, const char* data, size_t len, size_t* off)
|
||||
int msgpack_unpack_execute(msgpack_unpack_t* ctx,
|
||||
const char* data, size_t len, size_t* off)
|
||||
{
|
||||
return msgpack_unpacker_execute(
|
||||
(msgpack_unpacker*)ctx,
|
||||
return template_func_execute(
|
||||
(struct template_context*)ctx,
|
||||
data, len, off);
|
||||
}
|
||||
|
||||
|
||||
|
35
c/unpack.h
35
c/unpack.h
@@ -21,24 +21,29 @@
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
typedef struct {
|
||||
void* (*unpack_unsigned_int_8)(void* data, uint8_t d);
|
||||
void* (*unpack_unsigned_int_16)(void* data, uint16_t d);
|
||||
void* (*unpack_unsigned_int_32)(void* data, uint32_t d);
|
||||
void* (*unpack_unsigned_int_64)(void* data, uint64_t d);
|
||||
void* (*unpack_signed_int_8)(void* data, int8_t d);
|
||||
void* (*unpack_signed_int_16)(void* data, int16_t d);
|
||||
void* (*unpack_signed_int_32)(void* data, int32_t d);
|
||||
void* (*unpack_signed_int_64)(void* data, int64_t d);
|
||||
void* (*unpack_uint8)(void* data, uint8_t d);
|
||||
void* (*unpack_uint16)(void* data, uint16_t d);
|
||||
void* (*unpack_uint32)(void* data, uint32_t d);
|
||||
void* (*unpack_uint64)(void* data, uint64_t d);
|
||||
void* (*unpack_int8)(void* data, int8_t d);
|
||||
void* (*unpack_int16)(void* data, int16_t d);
|
||||
void* (*unpack_int32)(void* data, int32_t d);
|
||||
void* (*unpack_int64)(void* data, int64_t d);
|
||||
void* (*unpack_float)(void* data, float d);
|
||||
void* (*unpack_double)(void* data, double d);
|
||||
void* (*unpack_nil)(void* data);
|
||||
void* (*unpack_true)(void* data);
|
||||
void* (*unpack_false)(void* data);
|
||||
void* (*unpack_array_start)(void* data, unsigned int n);
|
||||
void* (*unpack_array)(void* data, unsigned int n);
|
||||
void (*unpack_array_item)(void* data, void* c, void* o);
|
||||
void* (*unpack_map_start)(void* data, unsigned int n);
|
||||
void (*unpack_map_item)(void* data, void* c, void* k, void* v);
|
||||
void* (*unpack_map)(void* data, unsigned int n);
|
||||
void (*unpack_map_item)(void* data, void* c, void* k, void* v);
|
||||
void* (*unpack_raw)(void* data, const char* b, const char* p, unsigned int l);
|
||||
} msgpack_unpack_callback;
|
||||
|
||||
@@ -51,8 +56,14 @@ msgpack_unpack_t* msgpack_unpack_new(void* data, msgpack_unpack_callback* callba
|
||||
void msgpack_unpack_free(msgpack_unpack_t* ctx);
|
||||
void msgpack_unpack_reset(msgpack_unpack_t* ctx);
|
||||
|
||||
int msgpack_unpack_execute(msgpack_unpack_t* ctx, const char* data, size_t len, size_t* off);
|
||||
int msgpack_unpack_execute(msgpack_unpack_t* ctx,
|
||||
const char* data, size_t len, size_t* off);
|
||||
void* msgpack_unpack_data(msgpack_unpack_t* ctx);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* msgpack/unpack.h */
|
||||
|
||||
|
@@ -1,30 +0,0 @@
|
||||
/*
|
||||
* MessagePack unpacking routine for C
|
||||
*
|
||||
* Copyright (C) 2008 FURUHASHI Sadayuki
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef UNPACK_CONTEXT_H__
|
||||
#define UNPACK_CONTEXT_H__
|
||||
|
||||
#include "msgpack/unpack.h"
|
||||
|
||||
typedef void* msgpack_object;
|
||||
|
||||
typedef msgpack_unpack_t msgpack_unpack_context;
|
||||
|
||||
#include "msgpack/unpack/inline_context.h"
|
||||
|
||||
#endif /* unpack_context.h */
|
||||
|
@@ -1,79 +0,0 @@
|
||||
/*
|
||||
* MessagePack unpacking routine for C
|
||||
*
|
||||
* Copyright (C) 2008 FURUHASHI Sadayuki
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include "unpack_context.h"
|
||||
|
||||
static inline void* msgpack_unpack_init(msgpack_unpack_t* x)
|
||||
{ return NULL; }
|
||||
|
||||
static inline void* msgpack_unpack_unsigned_int_8(msgpack_unpack_t* x, uint8_t d)
|
||||
{ return x->callback.unpack_unsigned_int_8(x->data, d); }
|
||||
|
||||
static inline void* msgpack_unpack_unsigned_int_16(msgpack_unpack_t* x, uint16_t d)
|
||||
{ return x->callback.unpack_unsigned_int_16(x->data, d); }
|
||||
|
||||
static inline void* msgpack_unpack_unsigned_int_32(msgpack_unpack_t* x, uint32_t d)
|
||||
{ return x->callback.unpack_unsigned_int_32(x->data, d); }
|
||||
|
||||
static inline void* msgpack_unpack_unsigned_int_64(msgpack_unpack_t* x, uint64_t d)
|
||||
{ return x->callback.unpack_unsigned_int_64(x->data, d); }
|
||||
|
||||
static inline void* msgpack_unpack_signed_int_8(msgpack_unpack_t* x, int8_t d)
|
||||
{ return x->callback.unpack_signed_int_8(x->data, d); }
|
||||
|
||||
static inline void* msgpack_unpack_signed_int_16(msgpack_unpack_t* x, int16_t d)
|
||||
{ return x->callback.unpack_signed_int_16(x->data, d); }
|
||||
|
||||
static inline void* msgpack_unpack_signed_int_32(msgpack_unpack_t* x, int32_t d)
|
||||
{ return x->callback.unpack_signed_int_32(x->data, d); }
|
||||
|
||||
static inline void* msgpack_unpack_signed_int_64(msgpack_unpack_t* x, int64_t d)
|
||||
{ return x->callback.unpack_signed_int_64(x->data, d); }
|
||||
|
||||
static inline void* msgpack_unpack_float(msgpack_unpack_t* x, float d)
|
||||
{ return x->callback.unpack_float(x->data, d); }
|
||||
|
||||
static inline void* msgpack_unpack_double(msgpack_unpack_t* x, double d)
|
||||
{ return x->callback.unpack_double(x->data, d); }
|
||||
|
||||
static inline void* msgpack_unpack_nil(msgpack_unpack_t* x)
|
||||
{ return x->callback.unpack_nil(x->data); }
|
||||
|
||||
static inline void* msgpack_unpack_true(msgpack_unpack_t* x)
|
||||
{ return x->callback.unpack_true(x->data); }
|
||||
|
||||
static inline void* msgpack_unpack_false(msgpack_unpack_t* x)
|
||||
{ return x->callback.unpack_false(x->data); }
|
||||
|
||||
static inline void* msgpack_unpack_array_start(msgpack_unpack_t* x, unsigned int n)
|
||||
{ return x->callback.unpack_array_start(x->data, n); }
|
||||
|
||||
static inline void msgpack_unpack_array_item(msgpack_unpack_t* x, void* c, void* o)
|
||||
{ x->callback.unpack_array_item(x->data, c, o); }
|
||||
|
||||
static inline void* msgpack_unpack_map_start(msgpack_unpack_t* x, unsigned int n)
|
||||
{ return x->callback.unpack_map_start(x->data, n); }
|
||||
|
||||
static inline void msgpack_unpack_map_item(msgpack_unpack_t* x, void* c, void* k, void* v)
|
||||
{ x->callback.unpack_map_item(x->data, c, k, v); }
|
||||
|
||||
static inline void* msgpack_unpack_raw(msgpack_unpack_t* x, const char* b, const char* p, unsigned int l)
|
||||
{ return x->callback.unpack_raw(x->data, b, p, l); }
|
||||
|
||||
|
||||
#include "msgpack/unpack/inline_impl.h"
|
||||
|
Reference in New Issue
Block a user