summaryrefslogtreecommitdiff
path: root/tests/bionic/libc/common/bench_stdio.c
blob: 4c19bf1e9aaec4b88717d9ec3851c482b9607e79 (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
/*
 * Copyright (C) 2011 The Android Open Source Project
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/* This program tries to benchmark stdio operations like fread() and
 * fwrite() with various chunk sizes. We always read/write from /dev/zero
 * to ensure that disk speed and caching don't change our results.
 *
 * We really do this to measure improvements in the low-level stdio
 * features.
 */
 
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <errno.h>

static char buffer[1024*1024];

/* Return current time in milli-seconds, as a double */
static double
now_ms(void)
{
    struct timespec  ts;
    clock_gettime(CLOCK_MONOTONIC, &ts);
    return ts.tv_sec*1000. + ts.tv_nsec*1e-6;
}

void read_file(FILE* fp, int chunkSize)
{
    int totalSize = sizeof(buffer);
    for ( ; totalSize > 0; totalSize -= chunkSize) {
        fread(buffer, 1, chunkSize, fp);
    }
}

void write_file(FILE* fp, int chunkSize)
{
    int totalSize = sizeof(buffer);
    for ( ; totalSize > 0; totalSize -= chunkSize) {
        fwrite(buffer, 1, chunkSize, fp);
    }
}

#define  BENCH(op,...) \
    do { \
	double  time_ms = now_ms(); \
	op ; \
	time_ms = now_ms() - time_ms; \
	double  bandwidth = sizeof(buffer)*1000./1024./time_ms; \
	printf("bench %-30s %8.2f ms  (%.1f KB/s) \n", #op, time_ms, bandwidth ); \
    } while (0)

int main(void)
{
    FILE* fp = fopen("/dev/zero", "rw");

    if (fp == NULL) {
	fprintf(stderr,"Could not open /dev/zero: %s\n", strerror(errno));
	return 1;
    }

    BENCH(read_file(fp,1));
    BENCH(read_file(fp,2));
    BENCH(read_file(fp,3));
    BENCH(read_file(fp,4));
    BENCH(read_file(fp,8));
    BENCH(read_file(fp,16));
    BENCH(read_file(fp,32));
    BENCH(read_file(fp,64));
    BENCH(read_file(fp,256));
    BENCH(read_file(fp,1024));
    BENCH(read_file(fp,4096));
    BENCH(read_file(fp,16384));
    BENCH(read_file(fp,65536));

    BENCH(write_file(fp,1));
    BENCH(write_file(fp,2));
    BENCH(write_file(fp,3));
    BENCH(write_file(fp,4));
    BENCH(write_file(fp,8));
    BENCH(write_file(fp,16));
    BENCH(write_file(fp,32));
    BENCH(write_file(fp,64));
    BENCH(write_file(fp,256));
    BENCH(write_file(fp,1024));
    BENCH(write_file(fp,4096));
    BENCH(write_file(fp,16384));
    BENCH(write_file(fp,65536));

    fclose(fp);
    return 0;
}