summaryrefslogtreecommitdiff
path: root/c/bench.c
blob: 91f0c21f54f0d7cfd021019908028221927ea53a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#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; }


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_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_item(void* data, void* c, void* o) { }
static void* unpack_map_start(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 void* b, const void* p, size_t 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 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_string,
	    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 MB/s\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 MB/s\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 MB/s\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 MB/s\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_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_float,
		unpack_double,
		unpack_nil,
		unpack_true,
		unpack_false,
		unpack_array_start,
		unpack_array_item,
		unpack_map_start,
		unpack_map_item,
		unpack_raw,
	};
	msgpack_unpack_t* mupk = msgpack_unpack_new(NULL, &cb);


	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 MB/s\n", len / sec / 1024 / 1024);

	puts("----");
	puts("unpack integer");
	reset_timer();
	{
		size_t off = 0;
		int ret = msgpack_unpack_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 MB/s\n", len / sec / 1024 / 1024);


	pack_buffer_reset(&mpkbuf);
	msgpack_unpack_reset(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 MB/s\n", len / sec / 1024 / 1024);

	puts("----");
	puts("unpack string");
	reset_timer();
	{
		size_t off = 0;
		int ret = msgpack_unpack_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 MB/s\n", len / sec / 1024 / 1024);


	msgpack_unpack_free(mupk);
	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;
}