Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 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 | |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 17 | #include <stdio.h> |
Elliott Hughes | 8c4994b | 2015-01-20 18:09:05 -0800 | [diff] [blame] | 18 | #include <stdio_ext.h> |
Elliott Hughes | c217373 | 2015-05-13 13:18:04 -0700 | [diff] [blame] | 19 | #include <stdlib.h> |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 20 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 21 | #include <benchmark/Benchmark.h> |
| 22 | |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 23 | #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 Hughes | 47dc7c9 | 2014-12-01 13:12:18 -0800 | [diff] [blame] | 30 | template <typename Fn> |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 31 | void ReadWriteTest(::testing::Benchmark* benchmark, int iters, int chunk_size, Fn f, bool buffered) { |
| 32 | benchmark->StopBenchmarkTiming(); |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 33 | FILE* fp = fopen("/dev/zero", "rw"); |
Elliott Hughes | 8c4994b | 2015-01-20 18:09:05 -0800 | [diff] [blame] | 34 | __fsetlocking(fp, FSETLOCKING_BYCALLER); |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 35 | char* buf = new char[chunk_size]; |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 36 | benchmark->StartBenchmarkTiming(); |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 37 | |
Elliott Hughes | 47dc7c9 | 2014-12-01 13:12:18 -0800 | [diff] [blame] | 38 | if (!buffered) { |
| 39 | setvbuf(fp, 0, _IONBF, 0); |
| 40 | } |
| 41 | |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 42 | for (int i = 0; i < iters; ++i) { |
Elliott Hughes | 47dc7c9 | 2014-12-01 13:12:18 -0800 | [diff] [blame] | 43 | f(buf, chunk_size, 1, fp); |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 46 | benchmark->StopBenchmarkTiming(); |
| 47 | benchmark->SetBenchmarkBytesProcessed(int64_t(iters) * int64_t(chunk_size)); |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 48 | delete[] buf; |
| 49 | fclose(fp); |
| 50 | } |
Elliott Hughes | 47dc7c9 | 2014-12-01 13:12:18 -0800 | [diff] [blame] | 51 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 52 | BENCHMARK_WITH_ARG(BM_stdio_fread, int)->AT_COMMON_SIZES; |
| 53 | void BM_stdio_fread::Run(int iters, int chunk_size) { |
| 54 | ReadWriteTest(this, iters, chunk_size, fread, true); |
Elliott Hughes | 47dc7c9 | 2014-12-01 13:12:18 -0800 | [diff] [blame] | 55 | } |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 56 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 57 | BENCHMARK_WITH_ARG(BM_stdio_fwrite, int)->AT_COMMON_SIZES; |
| 58 | void BM_stdio_fwrite::Run(int iters, int chunk_size) { |
| 59 | ReadWriteTest(this, iters, chunk_size, fwrite, true); |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 60 | } |
Elliott Hughes | 47dc7c9 | 2014-12-01 13:12:18 -0800 | [diff] [blame] | 61 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 62 | BENCHMARK_WITH_ARG(BM_stdio_fread_unbuffered, int)->AT_COMMON_SIZES; |
| 63 | void BM_stdio_fread_unbuffered::Run(int iters, int chunk_size) { |
| 64 | ReadWriteTest(this, iters, chunk_size, fread, false); |
Elliott Hughes | 47dc7c9 | 2014-12-01 13:12:18 -0800 | [diff] [blame] | 65 | } |
Elliott Hughes | 47dc7c9 | 2014-12-01 13:12:18 -0800 | [diff] [blame] | 66 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 67 | BENCHMARK_WITH_ARG(BM_stdio_fwrite_unbuffered, int)->AT_COMMON_SIZES; |
| 68 | void BM_stdio_fwrite_unbuffered::Run(int iters, int chunk_size) { |
| 69 | ReadWriteTest(this, iters, chunk_size, fwrite, false); |
Elliott Hughes | 47dc7c9 | 2014-12-01 13:12:18 -0800 | [diff] [blame] | 70 | } |
Elliott Hughes | 1cf32f8 | 2015-01-16 17:08:31 -0800 | [diff] [blame] | 71 | |
Elliott Hughes | 8c4994b | 2015-01-20 18:09:05 -0800 | [diff] [blame] | 72 | static void FopenFgetsFclose(int iters, bool no_locking) { |
Elliott Hughes | 1cf32f8 | 2015-01-16 17:08:31 -0800 | [diff] [blame] | 73 | char buf[1024]; |
| 74 | for (int i = 0; i < iters; ++i) { |
| 75 | FILE* fp = fopen("/proc/version", "re"); |
Elliott Hughes | 8c4994b | 2015-01-20 18:09:05 -0800 | [diff] [blame] | 76 | if (no_locking) __fsetlocking(fp, FSETLOCKING_BYCALLER); |
Elliott Hughes | c217373 | 2015-05-13 13:18:04 -0700 | [diff] [blame] | 77 | if (fgets(buf, sizeof(buf), fp) == nullptr) abort(); |
Elliott Hughes | 1cf32f8 | 2015-01-16 17:08:31 -0800 | [diff] [blame] | 78 | fclose(fp); |
| 79 | } |
| 80 | } |
Elliott Hughes | 8c4994b | 2015-01-20 18:09:05 -0800 | [diff] [blame] | 81 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 82 | BENCHMARK_NO_ARG(BM_stdio_fopen_fgets_fclose_locking); |
| 83 | void BM_stdio_fopen_fgets_fclose_locking::Run(int iters) { |
| 84 | StartBenchmarkTiming(); |
Elliott Hughes | 8c4994b | 2015-01-20 18:09:05 -0800 | [diff] [blame] | 85 | FopenFgetsFclose(iters, false); |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 86 | StopBenchmarkTiming(); |
Elliott Hughes | 8c4994b | 2015-01-20 18:09:05 -0800 | [diff] [blame] | 87 | } |
Elliott Hughes | 8c4994b | 2015-01-20 18:09:05 -0800 | [diff] [blame] | 88 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 89 | BENCHMARK_NO_ARG(BM_stdio_fopen_fgets_fclose_no_locking); |
| 90 | void BM_stdio_fopen_fgets_fclose_no_locking::Run(int iters) { |
| 91 | StartBenchmarkTiming(); |
Elliott Hughes | 8c4994b | 2015-01-20 18:09:05 -0800 | [diff] [blame] | 92 | FopenFgetsFclose(iters, true); |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 93 | StopBenchmarkTiming(); |
Elliott Hughes | 8c4994b | 2015-01-20 18:09:05 -0800 | [diff] [blame] | 94 | } |