• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 <stdio.h>
18 #include <stdio_ext.h>
19 
20 #include <benchmark/Benchmark.h>
21 
22 #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 
29 template <typename Fn>
ReadWriteTest(::testing::Benchmark * benchmark,int iters,int chunk_size,Fn f,bool buffered)30 void ReadWriteTest(::testing::Benchmark* benchmark, int iters, int chunk_size, Fn f, bool buffered) {
31   benchmark->StopBenchmarkTiming();
32   FILE* fp = fopen("/dev/zero", "rw");
33   __fsetlocking(fp, FSETLOCKING_BYCALLER);
34   char* buf = new char[chunk_size];
35   benchmark->StartBenchmarkTiming();
36 
37   if (!buffered) {
38     setvbuf(fp, 0, _IONBF, 0);
39   }
40 
41   for (int i = 0; i < iters; ++i) {
42     f(buf, chunk_size, 1, fp);
43   }
44 
45   benchmark->StopBenchmarkTiming();
46   benchmark->SetBenchmarkBytesProcessed(int64_t(iters) * int64_t(chunk_size));
47   delete[] buf;
48   fclose(fp);
49 }
50 
51 BENCHMARK_WITH_ARG(BM_stdio_fread, int)->AT_COMMON_SIZES;
Run(int iters,int chunk_size)52 void BM_stdio_fread::Run(int iters, int chunk_size) {
53   ReadWriteTest(this, iters, chunk_size, fread, true);
54 }
55 
56 BENCHMARK_WITH_ARG(BM_stdio_fwrite, int)->AT_COMMON_SIZES;
Run(int iters,int chunk_size)57 void BM_stdio_fwrite::Run(int iters, int chunk_size) {
58   ReadWriteTest(this, iters, chunk_size, fwrite, true);
59 }
60 
61 BENCHMARK_WITH_ARG(BM_stdio_fread_unbuffered, int)->AT_COMMON_SIZES;
Run(int iters,int chunk_size)62 void BM_stdio_fread_unbuffered::Run(int iters, int chunk_size) {
63   ReadWriteTest(this, iters, chunk_size, fread, false);
64 }
65 
66 BENCHMARK_WITH_ARG(BM_stdio_fwrite_unbuffered, int)->AT_COMMON_SIZES;
Run(int iters,int chunk_size)67 void BM_stdio_fwrite_unbuffered::Run(int iters, int chunk_size) {
68   ReadWriteTest(this, iters, chunk_size, fwrite, false);
69 }
70 
FopenFgetsFclose(int iters,bool no_locking)71 static void FopenFgetsFclose(int iters, bool no_locking) {
72   char buf[1024];
73   for (int i = 0; i < iters; ++i) {
74     FILE* fp = fopen("/proc/version", "re");
75     if (no_locking) __fsetlocking(fp, FSETLOCKING_BYCALLER);
76     fgets(buf, sizeof(buf), fp);
77     fclose(fp);
78   }
79 }
80 
81 BENCHMARK_NO_ARG(BM_stdio_fopen_fgets_fclose_locking);
Run(int iters)82 void BM_stdio_fopen_fgets_fclose_locking::Run(int iters) {
83   StartBenchmarkTiming();
84   FopenFgetsFclose(iters, false);
85   StopBenchmarkTiming();
86 }
87 
88 BENCHMARK_NO_ARG(BM_stdio_fopen_fgets_fclose_no_locking);
Run(int iters)89 void BM_stdio_fopen_fgets_fclose_no_locking::Run(int iters) {
90   StartBenchmarkTiming();
91   FopenFgetsFclose(iters, true);
92   StopBenchmarkTiming();
93 }
94