File: speedtest_msg.c

package info (click to toggle)
stringencoders 3.10.3%2Bgit20180306-1.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,132 kB
  • sloc: ansic: 17,159; javascript: 1,067; cpp: 407; python: 319; makefile: 180; sh: 34
file content (78 lines) | stat: -rw-r--r-- 1,991 bytes parent folder | download | duplicates (3)
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
#include "modp_json.h"

#include <time.h>
#ifndef CLOCKS_PER_SEC
#ifdef CLK_TCK
#define CLOCKS_PER_SEC (CLK_TCK)
#endif
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

size_t test_json_encode(char* dest)
{
    modp_json_ctx ctx;
    modp_json_init(&ctx, dest);
    modp_json_map_open(&ctx);

    modp_json_add_cstring(&ctx, "start_ms");
    modp_json_add_uint32(&ctx, 123456789);

    modp_json_add_cstring(&ctx, "remote_ip");
    modp_json_add_cstring(&ctx, "123.123.123.13");

    modp_json_add_cstring(&ctx, "request");
    modp_json_add_cstring(&ctx, "GET /foobar HTTP/1.1");

    modp_json_add_cstring(&ctx, "headers_in");
    modp_json_ary_open(&ctx);

    modp_json_ary_open(&ctx);
    modp_json_add_cstring(&ctx, "Accept");
    modp_json_add_cstring(&ctx, "*/*");
    modp_json_ary_close(&ctx);

    modp_json_ary_open(&ctx);
    modp_json_add_cstring(&ctx, "Content-type");
    modp_json_add_cstring(&ctx, "text/plain");
    modp_json_ary_close(&ctx);

    modp_json_ary_open(&ctx);
    modp_json_add_cstring(&ctx, "Connection");
    modp_json_add_cstring(&ctx, "close");
    modp_json_ary_close(&ctx);

    modp_json_ary_open(&ctx);
    modp_json_add_cstring(&ctx, "User-agent");
    modp_json_add_cstring(&ctx, "Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405");
    modp_json_ary_close(&ctx);

    modp_json_ary_close(&ctx);
    modp_json_map_close(&ctx);
    return modp_json_end(&ctx);
}

int main(void)
{

    const int imax = 1000000;
    clock_t t0, t1;
    int i;
    double s1;
    size_t len;
    char buf2[512];

    printf("ALG\tEncodes/Sec\tBYTES\n");
    fflush(stdout);
    t0 = clock();
    for (i = 0; i < imax; ++i) {
        len = test_json_encode(NULL);
        len = test_json_encode(buf2);
    }
    t1 = clock();
    s1 = (double)(t1 - t0) * (1.0 / (double)CLOCKS_PER_SEC);
    printf("%s\t%8.0f\t%u\n", "JSON", imax / s1, (unsigned)len);
    fflush(stdout);
    return 0;
}