1 /* Copyright 2017 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 #include "tensorflow/core/common_runtime/kernel_benchmark_testlib.h"
17 #include "tensorflow/core/framework/tensor.h"
18 #include "tensorflow/core/platform/test.h"
19 #include "tensorflow/core/platform/test_benchmark.h"
20
21 namespace tensorflow {
22
23 namespace {
24
25 // Implement a trivial version of the Rendezvous interface, to avoid
26 // clouding the benchmark results with the time spent in the various
27 // implementations, and to avoid the duplicate-send or duplicate-recv
28 // errors that would arise from running either benchmark in a loop.
29 class DummyRendezvous : public Rendezvous {
Send(const ParsedKey & key,const Args & args,const Tensor & val,const bool is_dead)30 Status Send(const ParsedKey& key, const Args& args, const Tensor& val,
31 const bool is_dead) override {
32 return Status::OK();
33 }
RecvAsync(const ParsedKey & key,const Args & args,DoneCallback done)34 void RecvAsync(const ParsedKey& key, const Args& args,
35 DoneCallback done) override {
36 static Tensor* t = new Tensor(DT_FLOAT, TensorShape({0}));
37 done(Status::OK(), args, args, *t, false);
38 }
StartAbort(const Status & status)39 void StartAbort(const Status& status) override {}
40 };
41
Send()42 static Graph* Send() {
43 Graph* g = new Graph(OpRegistry::Global());
44 Tensor in0(DT_FLOAT, TensorShape({0}));
45 test::graph::Send(g, test::graph::Constant(g, in0), "T", "/cpu:0", 1,
46 "/cpu:0");
47 test::graph::Recv(g, "T", "float", "/cpu:0", 1, "/cpu:0");
48 return g;
49 }
50
Recv()51 static Graph* Recv() {
52 Graph* g = new Graph(OpRegistry::Global());
53 test::graph::Recv(g, "T", "float", "/cpu:0", 1, "/cpu:0");
54 return g;
55 }
56
BM_Send(::testing::benchmark::State & state)57 void BM_Send(::testing::benchmark::State& state) {
58 test::Benchmark("cpu", Send(), nullptr, nullptr, new DummyRendezvous, "",
59 /*old_benchmark_api*/ false)
60 .Run(state);
61 state.SetItemsProcessed(static_cast<int64>(state.iterations()));
62 }
63 BENCHMARK(BM_Send)->UseRealTime();
64
BM_Recv(::testing::benchmark::State & state)65 void BM_Recv(::testing::benchmark::State& state) {
66 test::Benchmark("cpu", Recv(), nullptr, nullptr, new DummyRendezvous, "",
67 /*old_benchmark_api*/ false)
68 .Run(state);
69 state.SetItemsProcessed(static_cast<int64>(state.iterations()));
70 }
71 BENCHMARK(BM_Recv)->UseRealTime();
72
73 } // namespace
74 } // namespace tensorflow
75