blob: 342e56154af421a33ba382439d2abf8db8b173ca [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 Hughesb28e4902014-03-11 11:19:06 -070019
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080020#include <benchmark/Benchmark.h>
21
Elliott Hughesb28e4902014-03-11 11:19:06 -070022#define KB 1024
23#define MB 1024*KB
24
25#define AT_COMMON_SIZES \
26 Arg(1)->Arg(2)->Arg(3)->Arg(4)->Arg(8)->Arg(16)->Arg(32)->Arg(64)->Arg(512)-> \
27 Arg(1*KB)->Arg(4*KB)->Arg(8*KB)->Arg(16*KB)->Arg(64*KB)
28
Elliott Hughes47dc7c92014-12-01 13:12:18 -080029template <typename Fn>
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080030void ReadWriteTest(::testing::Benchmark* benchmark, int iters, int chunk_size, Fn f, bool buffered) {
31 benchmark->StopBenchmarkTiming();
Elliott Hughesb28e4902014-03-11 11:19:06 -070032 FILE* fp = fopen("/dev/zero", "rw");
Elliott Hughes8c4994b2015-01-20 18:09:05 -080033 __fsetlocking(fp, FSETLOCKING_BYCALLER);
Elliott Hughesb28e4902014-03-11 11:19:06 -070034 char* buf = new char[chunk_size];
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080035 benchmark->StartBenchmarkTiming();
Elliott Hughesb28e4902014-03-11 11:19:06 -070036
Elliott Hughes47dc7c92014-12-01 13:12:18 -080037 if (!buffered) {
38 setvbuf(fp, 0, _IONBF, 0);
39 }
40
Elliott Hughesb28e4902014-03-11 11:19:06 -070041 for (int i = 0; i < iters; ++i) {
Elliott Hughes47dc7c92014-12-01 13:12:18 -080042 f(buf, chunk_size, 1, fp);
Elliott Hughesb28e4902014-03-11 11:19:06 -070043 }
44
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080045 benchmark->StopBenchmarkTiming();
46 benchmark->SetBenchmarkBytesProcessed(int64_t(iters) * int64_t(chunk_size));
Elliott Hughesb28e4902014-03-11 11:19:06 -070047 delete[] buf;
48 fclose(fp);
49}
Elliott Hughes47dc7c92014-12-01 13:12:18 -080050
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080051BENCHMARK_WITH_ARG(BM_stdio_fread, int)->AT_COMMON_SIZES;
52void BM_stdio_fread::Run(int iters, int chunk_size) {
53 ReadWriteTest(this, iters, chunk_size, fread, true);
Elliott Hughes47dc7c92014-12-01 13:12:18 -080054}
Elliott Hughesb28e4902014-03-11 11:19:06 -070055
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080056BENCHMARK_WITH_ARG(BM_stdio_fwrite, int)->AT_COMMON_SIZES;
57void BM_stdio_fwrite::Run(int iters, int chunk_size) {
58 ReadWriteTest(this, iters, chunk_size, fwrite, true);
Elliott Hughesb28e4902014-03-11 11:19:06 -070059}
Elliott Hughes47dc7c92014-12-01 13:12:18 -080060
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080061BENCHMARK_WITH_ARG(BM_stdio_fread_unbuffered, int)->AT_COMMON_SIZES;
62void BM_stdio_fread_unbuffered::Run(int iters, int chunk_size) {
63 ReadWriteTest(this, iters, chunk_size, fread, false);
Elliott Hughes47dc7c92014-12-01 13:12:18 -080064}
Elliott Hughes47dc7c92014-12-01 13:12:18 -080065
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080066BENCHMARK_WITH_ARG(BM_stdio_fwrite_unbuffered, int)->AT_COMMON_SIZES;
67void BM_stdio_fwrite_unbuffered::Run(int iters, int chunk_size) {
68 ReadWriteTest(this, iters, chunk_size, fwrite, false);
Elliott Hughes47dc7c92014-12-01 13:12:18 -080069}
Elliott Hughes1cf32f82015-01-16 17:08:31 -080070
Elliott Hughes8c4994b2015-01-20 18:09:05 -080071static void FopenFgetsFclose(int iters, bool no_locking) {
Elliott Hughes1cf32f82015-01-16 17:08:31 -080072 char buf[1024];
73 for (int i = 0; i < iters; ++i) {
74 FILE* fp = fopen("/proc/version", "re");
Elliott Hughes8c4994b2015-01-20 18:09:05 -080075 if (no_locking) __fsetlocking(fp, FSETLOCKING_BYCALLER);
Elliott Hughes1cf32f82015-01-16 17:08:31 -080076 fgets(buf, sizeof(buf), fp);
77 fclose(fp);
78 }
79}
Elliott Hughes8c4994b2015-01-20 18:09:05 -080080
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080081BENCHMARK_NO_ARG(BM_stdio_fopen_fgets_fclose_locking);
82void BM_stdio_fopen_fgets_fclose_locking::Run(int iters) {
83 StartBenchmarkTiming();
Elliott Hughes8c4994b2015-01-20 18:09:05 -080084 FopenFgetsFclose(iters, false);
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080085 StopBenchmarkTiming();
Elliott Hughes8c4994b2015-01-20 18:09:05 -080086}
Elliott Hughes8c4994b2015-01-20 18:09:05 -080087
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080088BENCHMARK_NO_ARG(BM_stdio_fopen_fgets_fclose_no_locking);
89void BM_stdio_fopen_fgets_fclose_no_locking::Run(int iters) {
90 StartBenchmarkTiming();
Elliott Hughes8c4994b2015-01-20 18:09:05 -080091 FopenFgetsFclose(iters, true);
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080092 StopBenchmarkTiming();
Elliott Hughes8c4994b2015-01-20 18:09:05 -080093}