blob: 69e01a99601ae623e9cbefd1720cbacdc92b264b [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
Elliott Hughesb28e4902014-03-11 11:19:06 -070017#include <stdio.h>
Elliott Hughes8c4994b2015-01-20 18:09:05 -080018#include <stdio_ext.h>
Elliott Hughesc2173732015-05-13 13:18:04 -070019#include <stdlib.h>
Elliott Hughesb28e4902014-03-11 11:19:06 -070020
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080021#include <benchmark/Benchmark.h>
22
Elliott Hughesb28e4902014-03-11 11:19:06 -070023#define KB 1024
24#define MB 1024*KB
25
26#define AT_COMMON_SIZES \
27 Arg(1)->Arg(2)->Arg(3)->Arg(4)->Arg(8)->Arg(16)->Arg(32)->Arg(64)->Arg(512)-> \
28 Arg(1*KB)->Arg(4*KB)->Arg(8*KB)->Arg(16*KB)->Arg(64*KB)
29
Elliott Hughes47dc7c92014-12-01 13:12:18 -080030template <typename Fn>
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080031void ReadWriteTest(::testing::Benchmark* benchmark, int iters, int chunk_size, Fn f, bool buffered) {
32 benchmark->StopBenchmarkTiming();
Elliott Hughesb28e4902014-03-11 11:19:06 -070033 FILE* fp = fopen("/dev/zero", "rw");
Elliott Hughes8c4994b2015-01-20 18:09:05 -080034 __fsetlocking(fp, FSETLOCKING_BYCALLER);
Elliott Hughesb28e4902014-03-11 11:19:06 -070035 char* buf = new char[chunk_size];
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080036 benchmark->StartBenchmarkTiming();
Elliott Hughesb28e4902014-03-11 11:19:06 -070037
Elliott Hughes47dc7c92014-12-01 13:12:18 -080038 if (!buffered) {
39 setvbuf(fp, 0, _IONBF, 0);
40 }
41
Elliott Hughesb28e4902014-03-11 11:19:06 -070042 for (int i = 0; i < iters; ++i) {
Elliott Hughes47dc7c92014-12-01 13:12:18 -080043 f(buf, chunk_size, 1, fp);
Elliott Hughesb28e4902014-03-11 11:19:06 -070044 }
45
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080046 benchmark->StopBenchmarkTiming();
47 benchmark->SetBenchmarkBytesProcessed(int64_t(iters) * int64_t(chunk_size));
Elliott Hughesb28e4902014-03-11 11:19:06 -070048 delete[] buf;
49 fclose(fp);
50}
Elliott Hughes47dc7c92014-12-01 13:12:18 -080051
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080052BENCHMARK_WITH_ARG(BM_stdio_fread, int)->AT_COMMON_SIZES;
53void BM_stdio_fread::Run(int iters, int chunk_size) {
54 ReadWriteTest(this, iters, chunk_size, fread, true);
Elliott Hughes47dc7c92014-12-01 13:12:18 -080055}
Elliott Hughesb28e4902014-03-11 11:19:06 -070056
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080057BENCHMARK_WITH_ARG(BM_stdio_fwrite, int)->AT_COMMON_SIZES;
58void BM_stdio_fwrite::Run(int iters, int chunk_size) {
59 ReadWriteTest(this, iters, chunk_size, fwrite, true);
Elliott Hughesb28e4902014-03-11 11:19:06 -070060}
Elliott Hughes47dc7c92014-12-01 13:12:18 -080061
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080062BENCHMARK_WITH_ARG(BM_stdio_fread_unbuffered, int)->AT_COMMON_SIZES;
63void BM_stdio_fread_unbuffered::Run(int iters, int chunk_size) {
64 ReadWriteTest(this, iters, chunk_size, fread, false);
Elliott Hughes47dc7c92014-12-01 13:12:18 -080065}
Elliott Hughes47dc7c92014-12-01 13:12:18 -080066
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080067BENCHMARK_WITH_ARG(BM_stdio_fwrite_unbuffered, int)->AT_COMMON_SIZES;
68void BM_stdio_fwrite_unbuffered::Run(int iters, int chunk_size) {
69 ReadWriteTest(this, iters, chunk_size, fwrite, false);
Elliott Hughes47dc7c92014-12-01 13:12:18 -080070}
Elliott Hughes1cf32f82015-01-16 17:08:31 -080071
Elliott Hughes8c4994b2015-01-20 18:09:05 -080072static void FopenFgetsFclose(int iters, bool no_locking) {
Elliott Hughes1cf32f82015-01-16 17:08:31 -080073 char buf[1024];
74 for (int i = 0; i < iters; ++i) {
75 FILE* fp = fopen("/proc/version", "re");
Elliott Hughes8c4994b2015-01-20 18:09:05 -080076 if (no_locking) __fsetlocking(fp, FSETLOCKING_BYCALLER);
Elliott Hughesc2173732015-05-13 13:18:04 -070077 if (fgets(buf, sizeof(buf), fp) == nullptr) abort();
Elliott Hughes1cf32f82015-01-16 17:08:31 -080078 fclose(fp);
79 }
80}
Elliott Hughes8c4994b2015-01-20 18:09:05 -080081
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080082BENCHMARK_NO_ARG(BM_stdio_fopen_fgets_fclose_locking);
83void BM_stdio_fopen_fgets_fclose_locking::Run(int iters) {
84 StartBenchmarkTiming();
Elliott Hughes8c4994b2015-01-20 18:09:05 -080085 FopenFgetsFclose(iters, false);
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080086 StopBenchmarkTiming();
Elliott Hughes8c4994b2015-01-20 18:09:05 -080087}
Elliott Hughes8c4994b2015-01-20 18:09:05 -080088
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080089BENCHMARK_NO_ARG(BM_stdio_fopen_fgets_fclose_no_locking);
90void BM_stdio_fopen_fgets_fclose_no_locking::Run(int iters) {
91 StartBenchmarkTiming();
Elliott Hughes8c4994b2015-01-20 18:09:05 -080092 FopenFgetsFclose(iters, true);
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080093 StopBenchmarkTiming();
Elliott Hughes8c4994b2015-01-20 18:09:05 -080094}