Update benchmarks to newer API

PiperOrigin-RevId: 356573157
Change-Id: Idc6a37be704b6b785e394be276311a1b85cd4c44
This commit is contained in:
A. Unique TensorFlower 2021-02-09 13:07:55 -08:00 committed by TensorFlower Gardener
parent b461505cb3
commit 3b0a3b7e8d
12 changed files with 130 additions and 116 deletions

View File

@ -49,14 +49,13 @@ TEST(BlockingCounterTest, TestMultipleThread) {
} // namespace
static void BM_BlockingCounter(int iters, int num_threads,
int shards_per_thread) {
testing::StopTiming();
static void BM_BlockingCounter(::testing::benchmark::State& state) {
int num_threads = state.range(0);
int shards_per_thread = state.range(1);
std::unique_ptr<thread::ThreadPool> thread_pool(
new thread::ThreadPool(Env::Default(), "test", num_threads));
const int num_shards = num_threads * shards_per_thread;
testing::StartTiming();
for (int i = 0; i < iters; ++i) {
for (auto s : state) {
BlockingCounter bc(num_shards);
for (int j = 0; j < num_threads; ++j) {
thread_pool->Schedule([&bc, shards_per_thread] {
@ -67,7 +66,6 @@ static void BM_BlockingCounter(int iters, int num_threads,
}
bc.Wait();
}
testing::StopTiming();
}
BENCHMARK(BM_BlockingCounter)->RangePair(1, 12, 1, 1000);

View File

@ -202,10 +202,11 @@ TEST(Status, ErasePayloadRemovesIt) {
ASSERT_EQ(s.GetPayload("Error key"), tensorflow::StringPiece());
}
static void BM_TF_CHECK_OK(int iters) {
tensorflow::Status s =
(iters < 0) ? errors::InvalidArgument("Invalid") : Status::OK();
for (int i = 0; i < iters; i++) {
static void BM_TF_CHECK_OK(::testing::benchmark::State& state) {
tensorflow::Status s = (state.max_iterations < 0)
? errors::InvalidArgument("Invalid")
: Status::OK();
for (auto i : state) {
TF_CHECK_OK(s);
}
}

View File

@ -267,46 +267,46 @@ volatile int i;
void Incr(volatile int* ip) { ++*ip; }
void Incr() { Incr(&i); }
void BM_Cleanup(int iters) {
while (iters--) {
void BM_Cleanup(::testing::benchmark::State& state) {
for (auto s : state) {
auto fin = gtl::MakeCleanup([] { Incr(); });
}
}
BENCHMARK(BM_Cleanup);
void BM_AnyCleanup(int iters) {
while (iters--) {
void BM_AnyCleanup(::testing::benchmark::State& state) {
for (auto s : state) {
AnyCleanup fin = gtl::MakeCleanup([] { Incr(); });
}
}
BENCHMARK(BM_AnyCleanup);
void BM_AnyCleanupNoFactory(int iters) {
while (iters--) {
void BM_AnyCleanupNoFactory(::testing::benchmark::State& state) {
for (auto s : state) {
AnyCleanup fin([] { Incr(); });
}
}
BENCHMARK(BM_AnyCleanupNoFactory);
void BM_CleanupBound(int iters) {
void BM_CleanupBound(::testing::benchmark::State& state) {
volatile int* ip = &i;
while (iters--) {
for (auto s : state) {
auto fin = gtl::MakeCleanup([ip] { Incr(ip); });
}
}
BENCHMARK(BM_CleanupBound);
void BM_AnyCleanupBound(int iters) {
void BM_AnyCleanupBound(::testing::benchmark::State& state) {
volatile int* ip = &i;
while (iters--) {
for (auto s : state) {
AnyCleanup fin = gtl::MakeCleanup([ip] { Incr(ip); });
}
}
BENCHMARK(BM_AnyCleanupBound);
void BM_AnyCleanupNoFactoryBound(int iters) {
void BM_AnyCleanupNoFactoryBound(::testing::benchmark::State& state) {
volatile int* ip = &i;
while (iters--) {
for (auto s : state) {
AnyCleanup fin([ip] { Incr(ip); });
}
}

View File

@ -109,7 +109,8 @@ TEST_F(LevenshteinDistanceTest, Vectors) {
6);
}
static void BM_EditDistanceHelper(int n, int len, bool completely_different) {
static void BM_EditDistanceHelper(::testing::benchmark::State& state, int len,
bool completely_different) {
string a =
"The quick brown fox jumped over the lazy dog and on and on and on"
" Every good boy deserves fudge. In fact, this is a very long sentence "
@ -123,18 +124,18 @@ static void BM_EditDistanceHelper(int n, int len, bool completely_different) {
b[i]++;
}
}
while (n-- > 0) {
for (auto s : state) {
LevenshteinDistance(gtl::ArraySlice<char>(a.data(), len),
gtl::ArraySlice<char>(b.data(), len),
std::equal_to<char>());
}
}
static void BM_EditDistanceSame(int n, int len) {
BM_EditDistanceHelper(n, len, false);
static void BM_EditDistanceSame(::testing::benchmark::State& state) {
BM_EditDistanceHelper(state, state.range(0), false);
}
static void BM_EditDistanceDiff(int n, int len) {
BM_EditDistanceHelper(n, len, true);
static void BM_EditDistanceDiff(::testing::benchmark::State& state) {
BM_EditDistanceHelper(state, state.range(0), true);
}
BENCHMARK(BM_EditDistanceSame)->Arg(5);

View File

@ -81,13 +81,14 @@ TEST(CRC, ExtendWithCord) {
}
#endif
static void BM_CRC(int iters, int len) {
static void BM_CRC(::testing::benchmark::State& state) {
int len = state.range(0);
std::string input(len, 'x');
uint32 h = 0;
for (int i = 0; i < iters; i++) {
for (auto s : state) {
h = Extend(h, input.data() + 1, len - 1);
}
testing::BytesProcessed(static_cast<int64>(iters) * len);
state.SetBytesProcessed(state.iterations() * len);
VLOG(1) << h;
}
BENCHMARK(BM_CRC)->Range(1, 256 * 1024);

View File

@ -72,13 +72,14 @@ TEST(Hash, HashPtrIsNotIdentityFunction) {
EXPECT_NE(hash<int*>()(ptr), size_t{0xcafe0000});
}
static void BM_Hash32(int iters, int len) {
static void BM_Hash32(::testing::benchmark::State& state) {
int len = state.range(0);
std::string input(len, 'x');
uint32 h = 0;
for (int i = 0; i < iters; i++) {
for (auto s : state) {
h = Hash32(input.data(), len, 1);
}
testing::BytesProcessed(static_cast<int64>(iters) * len);
state.SetBytesProcessed(state.iterations() * len);
VLOG(1) << h;
}
BENCHMARK(BM_Hash32)->Range(1, 1024);

View File

@ -82,8 +82,8 @@ TEST_F(DistributionSamplerTest, KnownDistribution) {
TestDistribution(kDist1, TF_ARRAYSIZE(kDist1));
}
static void BM_DistributionSampler(int iters, int n) {
testing::StopTiming();
static void BM_DistributionSampler(::testing::benchmark::State& state) {
const int n = state.range(0);
PhiloxRandom philox(173, 371);
SimplePhilox rand(&philox);
std::vector<float> weights(n, 0);
@ -91,9 +91,8 @@ static void BM_DistributionSampler(int iters, int n) {
weights[i] = rand.Uniform(100);
}
DistributionSampler picker(weights);
testing::StartTiming();
int r = 0;
for (int i = 0; i < iters; i++) {
for (auto s : state) {
r |= picker.Sample(&rand);
}
CHECK_NE(r, kint32max);

View File

@ -234,31 +234,34 @@ static void TestPickAt(int items, const int32* weights) {
EXPECT_EQ(weight_index, picker.total_weight());
}
static void BM_Create(int iters, int arg) {
while (--iters > 0) {
static void BM_Create(::testing::benchmark::State& state) {
int arg = state.range(0);
for (auto s : state) {
WeightedPicker p(arg);
}
}
BENCHMARK(BM_Create)->Range(1, 1024);
static void BM_CreateAndSetWeights(int iters, int arg) {
static void BM_CreateAndSetWeights(::testing::benchmark::State& state) {
int arg = state.range(0);
std::vector<int32> weights(arg);
for (int i = 0; i < arg; i++) {
weights[i] = i * 10;
}
while (--iters > 0) {
for (auto s : state) {
WeightedPicker p(arg);
p.SetWeightsFromArray(arg, &weights[0]);
}
}
BENCHMARK(BM_CreateAndSetWeights)->Range(1, 1024);
static void BM_Pick(int iters, int arg) {
static void BM_Pick(::testing::benchmark::State& state) {
int arg = state.range(0);
PhiloxRandom philox(301, 17);
SimplePhilox rnd(&philox);
WeightedPicker p(arg);
int result = 0;
while (--iters > 0) {
for (auto s : state) {
result += p.Pick(&rnd);
}
VLOG(4) << result; // Dummy use

View File

@ -376,18 +376,18 @@ uint64 NextBits(random::SimplePhilox* rnd, int bits) {
}
template <typename T>
void BM_WriteNum(int n, T multiplier) {
void BM_WriteNum(::testing::benchmark::State& state, T multiplier) {
constexpr int kValues = 64;
T values[kValues];
random::PhiloxRandom philox(301, 17);
random::SimplePhilox rnd(&philox);
// Use enough distinct values to confuse the branch predictor
for (int i = 0; i < kValues; i++) {
values[i] = NextBits(&rnd, n % 64) * multiplier;
values[i] = NextBits(&rnd, state.max_iterations % 64) * multiplier;
}
string result;
int index = 0;
while (n-- > 0) {
for (auto i : state) {
result.clear();
OCWriteToString<T>(&result, values[index % kValues]);
index++;
@ -395,7 +395,7 @@ void BM_WriteNum(int n, T multiplier) {
}
template <typename T>
void BM_ReadNum(int n, T multiplier) {
void BM_ReadNum(::testing::benchmark::State& state, T multiplier) {
random::PhiloxRandom philox(301, 17);
random::SimplePhilox rnd(&philox);
// Use enough distinct values to confuse the branch predictor
@ -406,7 +406,7 @@ void BM_ReadNum(int n, T multiplier) {
values[i] = OCWrite<T>(val);
}
uint32 index = 0;
while (n-- > 0) {
for (auto i : state) {
T val;
StringPiece s = values[index++ % kValues];
OCRead<T>(&s, &val);
@ -414,9 +414,13 @@ void BM_ReadNum(int n, T multiplier) {
}
#define BENCHMARK_NUM(name, T, multiplier) \
void BM_Write##name(int n) { BM_WriteNum<T>(n, multiplier); } \
void BM_Write##name(::testing::benchmark::State& state) { \
BM_WriteNum<T>(state, multiplier); \
} \
BENCHMARK(BM_Write##name); \
void BM_Read##name(int n) { BM_ReadNum<T>(n, multiplier); } \
void BM_Read##name(::testing::benchmark::State& state) { \
BM_ReadNum<T>(state, multiplier); \
} \
BENCHMARK(BM_Read##name)
BENCHMARK_NUM(NumIncreasing, uint64, 1);
@ -1209,8 +1213,7 @@ TEST(EncodingIsExpected, Signed) {
}
}
void BM_WriteString(int n, int len) {
testing::StopTiming();
void BM_WriteString(::testing::benchmark::State& state, int len) {
random::PhiloxRandom philox(301, 17);
random::SimplePhilox rnd(&philox);
string x;
@ -1219,16 +1222,14 @@ void BM_WriteString(int n, int len) {
}
string y;
testing::BytesProcessed(n * len);
testing::StartTiming();
while (n-- > 0) {
for (auto s : state) {
y.clear();
OCWriteToString<string>(&y, x);
}
state.SetBytesProcessed(state.iterations() * len);
}
void BM_ReadString(int n, int len) {
testing::StopTiming();
void BM_ReadString(::testing::benchmark::State& state, int len) {
random::PhiloxRandom philox(301, 17);
random::SimplePhilox rnd(&philox);
string x;
@ -1239,17 +1240,20 @@ void BM_ReadString(int n, int len) {
OCWriteToString<string>(&data, x);
string result;
testing::BytesProcessed(n * len);
testing::StartTiming();
while (n-- > 0) {
for (auto i : state) {
result.clear();
StringPiece s = data;
OCRead<string>(&s, &result);
}
state.SetBytesProcessed(state.iterations() * len);
}
void BM_WriteStringIncreasing(int n, int len) { BM_WriteString(n, len); }
void BM_ReadStringIncreasing(int n, int len) { BM_ReadString(n, len); }
void BM_WriteStringIncreasing(::testing::benchmark::State& state) {
BM_WriteString(state, state.range(0));
}
void BM_ReadStringIncreasing(::testing::benchmark::State& state) {
BM_ReadString(state, state.range(0));
}
BENCHMARK(BM_WriteStringIncreasing)->Range(0, 1024);
BENCHMARK(BM_ReadStringIncreasing)->Range(0, 1024);

View File

@ -16,6 +16,7 @@ limitations under the License.
#include "tensorflow/core/lib/strings/proto_serialization.h"
#include <string>
#include "absl/memory/memory.h"
#include "tensorflow/core/framework/attr_value.pb.h"
#include "tensorflow/core/framework/graph.pb.h"
@ -40,54 +41,55 @@ GraphDef MakeGraphDef(int num_nodes) {
}
} // namespace
static void BM_ProtoSerializationToString(int iters, int num_nodes) {
testing::StopTiming();
static void BM_ProtoSerializationToString(::testing::benchmark::State& state) {
int num_nodes = state.range(0);
GraphDef graph_def = MakeGraphDef(num_nodes);
testing::StartTiming();
for (int i = 0; i < iters; ++i) {
for (auto i : state) {
string serialized;
testing::DoNotOptimize(
SerializeToStringDeterministic(graph_def, &serialized));
}
testing::StopTiming();
}
BENCHMARK(BM_ProtoSerializationToString)->Range(1, 10000);
static void BM_ProtoSerializationToBuffer(int iters, int num_nodes) {
testing::StopTiming();
static void BM_ProtoSerializationToBuffer(::testing::benchmark::State& state) {
int num_nodes = state.range(0);
GraphDef graph_def = MakeGraphDef(num_nodes);
testing::StartTiming();
const size_t size = graph_def.ByteSizeLong();
for (int i = 0; i < iters; ++i) {
for (auto i : state) {
gtl::InlinedVector<char, 1024> buf(size);
testing::DoNotOptimize(
SerializeToBufferDeterministic(graph_def, buf.data(), size));
}
testing::StopTiming();
}
BENCHMARK(BM_ProtoSerializationToBuffer)->Range(1, 10000);
static void BM_DeterministicProtoHash64(int iters, int num_nodes) {
testing::StopTiming();
static void BM_DeterministicProtoHash64(::testing::benchmark::State& state) {
int num_nodes = state.range(0);
GraphDef graph_def = MakeGraphDef(num_nodes);
testing::StartTiming();
for (int i = 0; i < iters; ++i) {
for (auto i : state) {
testing::DoNotOptimize(DeterministicProtoHash64(graph_def));
}
testing::StopTiming();
}
BENCHMARK(BM_DeterministicProtoHash64)->Range(1, 10000);
static void BM_AreSerializedProtosEqual(int iters, int num_nodes) {
testing::StopTiming();
static void BM_AreSerializedProtosEqual(::testing::benchmark::State& state) {
int num_nodes = state.range(0);
GraphDef graph_def_a = MakeGraphDef(num_nodes);
GraphDef graph_def_b = MakeGraphDef(num_nodes);
graph_def_b.mutable_node(0)->mutable_name()[0] = 'l';
testing::StartTiming();
for (int i = 0; i < iters; ++i) {
for (auto i : state) {
testing::DoNotOptimize(AreSerializedProtosEqual(graph_def_a, graph_def_a));
}
testing::StopTiming();
}
BENCHMARK(BM_AreSerializedProtosEqual)->Range(1, 10000);

View File

@ -744,7 +744,9 @@ TEST(SparseTensorTest, Dim0SparseTensorToDenseTensor) {
EXPECT_EQ(dense.scalar<int32>()(), 5);
}
static void BM_SparseReorderFloat(int iters, int N32, int NDIM32) {
static void BM_SparseReorderFloat(::testing::benchmark::State& state) {
int N32 = state.range(0);
int NDIM32 = state.range(1);
random::PhiloxRandom philox(301, 17);
random::SimplePhilox rnd(&philox);
const int64 NDIM = static_cast<int64>(NDIM32);
@ -764,10 +766,9 @@ static void BM_SparseReorderFloat(int iters, int N32, int NDIM32) {
reorder.push_back(d);
}
auto ix_t = ix.matrix<int64>();
testing::UseRealTime();
while (--iters) {
testing::StopTiming();
for (auto s : state) {
state.PauseTiming();
for (int64 i = 0; i < N; ++i) {
for (int d = 0; d < NDIM32; ++d) {
ix_t(i, d) = rnd.Rand64() % 1000;
@ -776,12 +777,14 @@ static void BM_SparseReorderFloat(int iters, int N32, int NDIM32) {
SparseTensor st;
TF_ASSERT_OK(SparseTensor::Create(ix, vals, shape, order, &st));
testing::StartTiming();
state.ResumeTiming();
st.Reorder<float>(reorder);
}
}
static void BM_SparseReorderString(int iters, int N32, int NDIM32) {
static void BM_SparseReorderString(::testing::benchmark::State& state) {
int N32 = state.range(0);
int NDIM32 = state.range(1);
random::PhiloxRandom philox(301, 17);
random::SimplePhilox rnd(&philox);
const int64 NDIM = static_cast<int64>(NDIM32);
@ -806,10 +809,9 @@ static void BM_SparseReorderString(int iters, int N32, int NDIM32) {
for (int d = 2; d < NDIM32; ++d) {
reorder.push_back(d);
}
testing::UseRealTime();
while (--iters) {
testing::StopTiming();
for (auto s : state) {
state.PauseTiming();
for (int64 i = 0; i < N; ++i) {
for (int d = 0; d < NDIM32; ++d) {
ix_t(i, d) = rnd.Rand64() % 1000;
@ -818,30 +820,30 @@ static void BM_SparseReorderString(int iters, int N32, int NDIM32) {
SparseTensor st;
TF_ASSERT_OK(SparseTensor::Create(ix, vals, shape, order, &st));
testing::StartTiming();
state.ResumeTiming();
st.Reorder<tstring>(reorder);
}
}
BENCHMARK(BM_SparseReorderFloat)->ArgPair(10, 2);
BENCHMARK(BM_SparseReorderFloat)->ArgPair(100, 2);
BENCHMARK(BM_SparseReorderFloat)->ArgPair(1000, 2);
BENCHMARK(BM_SparseReorderFloat)->ArgPair(10000, 2);
BENCHMARK(BM_SparseReorderFloat)->ArgPair(100000, 2);
BENCHMARK(BM_SparseReorderFloat)->ArgPair(10, 3);
BENCHMARK(BM_SparseReorderFloat)->ArgPair(100, 3);
BENCHMARK(BM_SparseReorderFloat)->ArgPair(1000, 3);
BENCHMARK(BM_SparseReorderFloat)->ArgPair(10000, 3);
BENCHMARK(BM_SparseReorderFloat)->ArgPair(100000, 3);
BENCHMARK(BM_SparseReorderFloat)->UseRealTime()->ArgPair(10, 2);
BENCHMARK(BM_SparseReorderFloat)->UseRealTime()->ArgPair(100, 2);
BENCHMARK(BM_SparseReorderFloat)->UseRealTime()->ArgPair(1000, 2);
BENCHMARK(BM_SparseReorderFloat)->UseRealTime()->ArgPair(10000, 2);
BENCHMARK(BM_SparseReorderFloat)->UseRealTime()->ArgPair(100000, 2);
BENCHMARK(BM_SparseReorderFloat)->UseRealTime()->ArgPair(10, 3);
BENCHMARK(BM_SparseReorderFloat)->UseRealTime()->ArgPair(100, 3);
BENCHMARK(BM_SparseReorderFloat)->UseRealTime()->ArgPair(1000, 3);
BENCHMARK(BM_SparseReorderFloat)->UseRealTime()->ArgPair(10000, 3);
BENCHMARK(BM_SparseReorderFloat)->UseRealTime()->ArgPair(100000, 3);
BENCHMARK(BM_SparseReorderString)->ArgPair(10, 2);
BENCHMARK(BM_SparseReorderString)->ArgPair(100, 2);
BENCHMARK(BM_SparseReorderString)->ArgPair(1000, 2);
BENCHMARK(BM_SparseReorderString)->ArgPair(10000, 2);
BENCHMARK(BM_SparseReorderString)->ArgPair(10, 3);
BENCHMARK(BM_SparseReorderString)->ArgPair(100, 3);
BENCHMARK(BM_SparseReorderString)->ArgPair(1000, 3);
BENCHMARK(BM_SparseReorderString)->ArgPair(10000, 3);
BENCHMARK(BM_SparseReorderString)->UseRealTime()->ArgPair(10, 2);
BENCHMARK(BM_SparseReorderString)->UseRealTime()->ArgPair(100, 2);
BENCHMARK(BM_SparseReorderString)->UseRealTime()->ArgPair(1000, 2);
BENCHMARK(BM_SparseReorderString)->UseRealTime()->ArgPair(10000, 2);
BENCHMARK(BM_SparseReorderString)->UseRealTime()->ArgPair(10, 3);
BENCHMARK(BM_SparseReorderString)->UseRealTime()->ArgPair(100, 3);
BENCHMARK(BM_SparseReorderString)->UseRealTime()->ArgPair(1000, 3);
BENCHMARK(BM_SparseReorderString)->UseRealTime()->ArgPair(10000, 3);
} // namespace
} // namespace sparse

View File

@ -144,12 +144,14 @@ TEST(TensorSliceSetTest, QueryMetaTwoD) {
}
}
static void BM_RegisterOneByOne(int parts) {
TensorShape shape({parts, 41});
static void BM_RegisterOneByOne(::testing::benchmark::State& state) {
TensorShape shape({static_cast<int>(state.max_iterations), 41});
TensorSliceSet slice_set(shape, DT_INT32);
for (int i = 0; i < parts; ++i) {
int i = 0;
for (auto s : state) {
TensorSlice part({{i, 1}, {0, -1}});
TF_CHECK_OK(slice_set.Register(part, part.DebugString()));
++i;
}
}