]> git.saurik.com Git - apple/libc.git/blame - tests/open_memstream.c
Libc-1439.100.3.tar.gz
[apple/libc.git] / tests / open_memstream.c
CommitLineData
70ad1dc8
A
1#include <stdio.h>
2#include <stdlib.h>
3
4#include <darwintest.h>
5#include <darwintest_perf.h>
6
7static void
8perf_fixed_size(size_t size_per_write)
9{
10 dt_stat_time_t latency = dt_stat_time_create("write_latency",
11 "adding %zu bytes to a memstream", size_per_write);
12 dt_stat_set_variable_d(latency, "bytes", (double)size_per_write);
13 char *src = calloc(1, size_per_write);
14 T_QUIET; T_ASSERT_NOTNULL(src, "allocated source buffer");
15
16 while (!dt_stat_stable(latency)) {
17 char *buf = NULL;
18 size_t size = 0;
19
20 FILE *victim = open_memstream(&buf, &size);
21 T_QUIET; T_WITH_ERRNO; T_ASSERT_NOTNULL(victim, "opened memstream");
22
23 T_STAT_MEASURE_BATCH(latency) {
24 (void)fwrite(src, size_per_write, 1, victim);
25 }
26
27 fclose(victim);
28 T_QUIET; T_ASSERT_NOTNULL(buf, "buffer was set by open_memstream");
29 T_QUIET; T_ASSERT_GE(size, size_per_write,
30 "memstream added non-zero bytes");
31
32 free(buf);
33 }
34
35 free(src);
36 dt_stat_finalize(latency);
37}
38
39T_DECL(perf_open_memstream, "measure the performance of open_memstream")
40{
41 size_t sizes[] = { 1, 8, 16, 64, 1024, 2048, 4096, 16 * 1024 };
42 for (size_t i = 0; i < sizeof(sizes) / sizeof(sizes[0]); i++) {
43 perf_fixed_size(sizes[i]);
44 }
45}