Home | History | Annotate | Download | only in platform
      1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
      2 
      3 Licensed under the Apache License, Version 2.0 (the "License");
      4 you may not use this file except in compliance with the License.
      5 You may obtain a copy of the License at
      6 
      7     http://www.apache.org/licenses/LICENSE-2.0
      8 
      9 Unless required by applicable law or agreed to in writing, software
     10 distributed under the License is distributed on an "AS IS" BASIS,
     11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 See the License for the specific language governing permissions and
     13 limitations under the License.
     14 ==============================================================================*/
     15 
     16 // Simple benchmarking facility.
     17 #ifndef TENSORFLOW_PLATFORM_TEST_BENCHMARK_H_
     18 #define TENSORFLOW_PLATFORM_TEST_BENCHMARK_H_
     19 
     20 #include <utility>
     21 #include <vector>
     22 #include "tensorflow/core/platform/macros.h"
     23 #include "tensorflow/core/platform/platform.h"
     24 #include "tensorflow/core/platform/types.h"
     25 
     26 #if defined(PLATFORM_GOOGLE)
     27 #include "tensorflow/core/platform/google/build_config/benchmark.h"
     28 
     29 #else
     30 #define BENCHMARK(n)                                            \
     31   static ::tensorflow::testing::Benchmark* TF_BENCHMARK_CONCAT( \
     32       __benchmark_, n, __LINE__) TF_ATTRIBUTE_UNUSED =          \
     33       (new ::tensorflow::testing::Benchmark(#n, (n)))
     34 #define TF_BENCHMARK_CONCAT(a, b, c) TF_BENCHMARK_CONCAT2(a, b, c)
     35 #define TF_BENCHMARK_CONCAT2(a, b, c) a##b##c
     36 
     37 #endif  // PLATFORM_GOOGLE
     38 
     39 namespace tensorflow {
     40 namespace testing {
     41 
     42 #if defined(PLATFORM_GOOGLE)
     43 
     44 using ::testing::Benchmark;
     45 
     46 #else
     47 
     48 class Benchmark {
     49  public:
     50   Benchmark(const char* name, void (*fn)(int));
     51   Benchmark(const char* name, void (*fn)(int, int));
     52   Benchmark(const char* name, void (*fn)(int, int, int));
     53 
     54   Benchmark* Arg(int x);
     55   Benchmark* ArgPair(int x, int y);
     56   Benchmark* Range(int lo, int hi);
     57   Benchmark* RangePair(int lo1, int hi1, int lo2, int hi2);
     58   static void Run(const char* pattern);
     59 
     60  private:
     61   string name_;
     62   int num_args_;
     63   std::vector<std::pair<int, int> > args_;
     64   void (*fn0_)(int) = nullptr;
     65   void (*fn1_)(int, int) = nullptr;
     66   void (*fn2_)(int, int, int) = nullptr;
     67 
     68   void Register();
     69   void Run(int arg1, int arg2, int* run_count, double* run_seconds);
     70 };
     71 #endif
     72 
     73 void RunBenchmarks();
     74 void SetLabel(const std::string& label);
     75 void BytesProcessed(int64);
     76 void ItemsProcessed(int64);
     77 void StartTiming();
     78 void StopTiming();
     79 void UseRealTime();
     80 
     81 }  // namespace testing
     82 }  // namespace tensorflow
     83 
     84 #endif  // TENSORFLOW_PLATFORM_TEST_BENCHMARK_H_
     85