Пример #1
0
int encode(flatcc_builder_t *B, void *buffer, size_t *size)
{
    int i, veclen = 3;
    void *buffer_ok;

    flatcc_builder_reset(B);

    C(start_as_root(B));
    C(list_start(B, 0));
    for (i = 0; i < veclen; ++i) {
        /*
         * By using push_start instead of push_create we can construct
         * the sibling field (of Bar type) in-place on the stack,
         * otherwise we would need to create a temporary Bar struct.
         */
        C(list_push_start(B));
        FooBar(sibling_create(B,
                0xABADCAFEABADCAFE + i, 10000 + i, '@' + i, 1000000 + i,
                123456 + i, 3.14159f + i, 10000 + i));
        FooBar(name_create_str(B, "Hello, World!"));
        FooBar(rating_add(B, 3.1415432432445543543 + i));
        FooBar(postfix_add(B, '!' + i));
        C(list_push_end(B));
    }
    C(list_end(B));
    C(location_create_str(B, "https://www.example.com/myurl/"));
    C(fruit_add(B, Enum(Bananas)));
    C(initialized_add(B, True));
    C(end_as_root(B));

    /*
     * This only works with the default emitter and only if the buffer
     * is larger enough. Otherwise use whatever custom operation the
     * emitter provides.
     */
    buffer_ok = flatcc_builder_copy_buffer(B, buffer, *size);
    *size = flatcc_builder_get_buffer_size(B);
    return !buffer_ok;
}
Пример #2
0
// TODO:
// when running benchmark with the wrong size argument (output size
// instead of input size), the warmup loop iterates indefinitely in the
// first iteration. This suggests there is an end check missing somwhere
// and this needs to be debugged. The input size as of this writing is 701
// bytes, and the output size is 288 bytes.
int test_parse()
{
#if FLATCC_BENCHMARK
    double t1, t2;
    int i;
    int rep = 1000000;
    int warmup_rep = 1000000;
#endif

    const char *buf;
    void *flatbuffer = 0;
    size_t in_size, out_size;
    flatcc_json_parser_t ctx;
    flatcc_builder_t builder;
    flatcc_builder_t *B = &builder;
    int ret = -1;
    int flags = 0;
    const char *filename = "monsterdata_test.golden";

    flatcc_builder_init(B);

    buf = readfile(filename, FILE_SIZE_MAX, &in_size);
    if (!buf) {
        fprintf(stderr, "%s: could not read input json file\n", filename);
        return -1;
    }

    if (monster_test_parse_json(B, &ctx, buf, in_size, flags)) {
        goto failed;
    }
    fprintf(stderr, "%s: successfully parsed %d lines\n", filename, ctx.line);
    flatbuffer = flatcc_builder_finalize_buffer(B, &out_size);
    hexdump("parsed monsterdata_test.golden", flatbuffer, out_size, stdout);
    fprintf(stderr, "input size: %lu, output size: %lu\n",
            (unsigned long)in_size, (unsigned long)out_size);
    verify_parse(flatbuffer);

    flatcc_builder_reset(B);
#if FLATCC_BENCHMARK
    fprintf(stderr, "Now warming up\n");
    for (i = 0; i < warmup_rep; ++i) {
        if (monster_test_parse_json(B, &ctx, buf, in_size, flags)) {
            goto failed;
        }
        flatcc_builder_reset(B);
    }

    fprintf(stderr, "Now benchmarking\n");
    t1 = elapsed_realtime();
    for (i = 0; i < rep; ++i) {
        if (monster_test_parse_json(B, &ctx, buf, in_size, flags)) {
            goto failed;
        }
        flatcc_builder_reset(B);
    }
    t2 = elapsed_realtime();

    printf("----\n");
    show_benchmark(BENCH_TITLE " C generated JSON parse " COMPILE_TYPE, t1, t2, in_size, rep, "1M");
#endif
    ret = 0;

done:
    if (flatbuffer) {
        free(flatbuffer);
    }
    if (buf) {
        free((void *)buf);
    }
    flatcc_builder_clear(B);
    return ret;

failed:
    fprintf(stderr, "%s:%d:%d: %s\n",
            filename, (int)ctx.line, (int)(ctx.error_loc - ctx.line_start + 1),
            flatcc_json_parser_error_string(ctx.error));
    goto done;
}