blob: 386ea04c9fc345834b56856f122ae5f315d5c042 [file] [log] [blame]
Elliott Hughesb28e4902014-03-11 11:19:06 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "benchmark.h"
18
19#include <stdio.h>
20
21#define KB 1024
22#define MB 1024*KB
23
24#define AT_COMMON_SIZES \
25 Arg(1)->Arg(2)->Arg(3)->Arg(4)->Arg(8)->Arg(16)->Arg(32)->Arg(64)->Arg(512)-> \
26 Arg(1*KB)->Arg(4*KB)->Arg(8*KB)->Arg(16*KB)->Arg(64*KB)
27
Elliott Hughes47dc7c92014-12-01 13:12:18 -080028template <typename Fn>
29static void ReadWriteTest(int iters, int chunk_size, Fn f, bool buffered) {
Elliott Hughesb28e4902014-03-11 11:19:06 -070030 StopBenchmarkTiming();
31 FILE* fp = fopen("/dev/zero", "rw");
32 char* buf = new char[chunk_size];
33 StartBenchmarkTiming();
34
Elliott Hughes47dc7c92014-12-01 13:12:18 -080035 if (!buffered) {
36 setvbuf(fp, 0, _IONBF, 0);
37 }
38
Elliott Hughesb28e4902014-03-11 11:19:06 -070039 for (int i = 0; i < iters; ++i) {
Elliott Hughes47dc7c92014-12-01 13:12:18 -080040 f(buf, chunk_size, 1, fp);
Elliott Hughesb28e4902014-03-11 11:19:06 -070041 }
42
43 StopBenchmarkTiming();
44 SetBenchmarkBytesProcessed(int64_t(iters) * int64_t(chunk_size));
45 delete[] buf;
46 fclose(fp);
47}
Elliott Hughes47dc7c92014-12-01 13:12:18 -080048
49static void BM_stdio_fread(int iters, int chunk_size) {
50 ReadWriteTest(iters, chunk_size, fread, true);
51}
Elliott Hughesb28e4902014-03-11 11:19:06 -070052BENCHMARK(BM_stdio_fread)->AT_COMMON_SIZES;
53
Elliott Hughesb28e4902014-03-11 11:19:06 -070054static void BM_stdio_fwrite(int iters, int chunk_size) {
Elliott Hughes47dc7c92014-12-01 13:12:18 -080055 ReadWriteTest(iters, chunk_size, fwrite, true);
Elliott Hughesb28e4902014-03-11 11:19:06 -070056}
57BENCHMARK(BM_stdio_fwrite)->AT_COMMON_SIZES;
Elliott Hughes47dc7c92014-12-01 13:12:18 -080058
59static void BM_stdio_fread_unbuffered(int iters, int chunk_size) {
60 ReadWriteTest(iters, chunk_size, fread, false);
61}
62BENCHMARK(BM_stdio_fread_unbuffered)->AT_COMMON_SIZES;
63
64static void BM_stdio_fwrite_unbuffered(int iters, int chunk_size) {
65 ReadWriteTest(iters, chunk_size, fwrite, false);
66}
67BENCHMARK(BM_stdio_fwrite_unbuffered)->AT_COMMON_SIZES;