1b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes/*
2b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes * Copyright (C) 2014 The Android Open Source Project
3b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes *
4b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
5b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes * you may not use this file except in compliance with the License.
6b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes * You may obtain a copy of the License at
7b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes *
8b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
9b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes *
10b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes * Unless required by applicable law or agreed to in writing, software
11b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
12b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes * See the License for the specific language governing permissions and
14b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes * limitations under the License.
15b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes */
16b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes
17b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes#include "benchmark.h"
18b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes
19b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes#include <stdio.h>
20b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes
21b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes#define KB 1024
22b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes#define MB 1024*KB
23b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes
24b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes#define AT_COMMON_SIZES \
25b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes    Arg(1)->Arg(2)->Arg(3)->Arg(4)->Arg(8)->Arg(16)->Arg(32)->Arg(64)->Arg(512)-> \
26b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes    Arg(1*KB)->Arg(4*KB)->Arg(8*KB)->Arg(16*KB)->Arg(64*KB)
27b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes
28b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughesstatic void BM_stdio_fread(int iters, int chunk_size) {
29b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes  StopBenchmarkTiming();
30b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes  FILE* fp = fopen("/dev/zero", "rw");
31b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes  char* buf = new char[chunk_size];
32b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes  StartBenchmarkTiming();
33b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes
34b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes  for (int i = 0; i < iters; ++i) {
35b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes    fread(buf, chunk_size, 1, fp);
36b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes  }
37b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes
38b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes  StopBenchmarkTiming();
39b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes  SetBenchmarkBytesProcessed(int64_t(iters) * int64_t(chunk_size));
40b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes  delete[] buf;
41b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes  fclose(fp);
42b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes}
43b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott HughesBENCHMARK(BM_stdio_fread)->AT_COMMON_SIZES;
44b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes
45b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes
46b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughesstatic void BM_stdio_fwrite(int iters, int chunk_size) {
47b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes  StopBenchmarkTiming();
48b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes  FILE* fp = fopen("/dev/zero", "rw");
49b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes  char* buf = new char[chunk_size];
50b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes  StartBenchmarkTiming();
51b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes
52b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes  for (int i = 0; i < iters; ++i) {
53b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes      fwrite(buf, chunk_size, 1, fp);
54b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes  }
55b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes
56b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes  StopBenchmarkTiming();
57b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes  SetBenchmarkBytesProcessed(int64_t(iters) * int64_t(chunk_size));
58b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes  delete[] buf;
59b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes  fclose(fp);
60b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott Hughes}
61b28e490b7350b21c5ae9e5b3bb3e082d8357a1b0Elliott HughesBENCHMARK(BM_stdio_fwrite)->AT_COMMON_SIZES;
62