orderfile_instrumentation_perftest.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/android/orderfile/orderfile_instrumentation.h"
  5. #include <thread>
  6. #include "base/android/library_loader/anchor_functions.h"
  7. #include "base/strings/stringprintf.h"
  8. #include "base/time/time.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. #include "testing/perf/perf_test.h"
  11. namespace base {
  12. namespace android {
  13. namespace orderfile {
  14. namespace {
  15. // Records |addresses_count| distinct addresses |iterations| times, in
  16. // |threads|.
  17. void RunBenchmark(int iterations, int addresses_count, int threads) {
  18. ResetForTesting();
  19. auto iterate = [iterations, addresses_count]() {
  20. for (int i = 0; i < iterations; i++) {
  21. for (size_t addr = kStartOfTextForTesting;
  22. addr < static_cast<size_t>(addresses_count); addr += sizeof(int)) {
  23. RecordAddressForTesting(addr);
  24. }
  25. }
  26. };
  27. if (threads != 1) {
  28. for (int i = 0; i < threads - 1; ++i)
  29. std::thread(iterate).detach();
  30. }
  31. auto tick = base::TimeTicks::Now();
  32. iterate();
  33. auto tock = base::TimeTicks::Now();
  34. double nanos = static_cast<double>((tock - tick).InNanoseconds());
  35. auto ns_per_call =
  36. nanos / (iterations * static_cast<double>(addresses_count));
  37. auto modifier =
  38. base::StringPrintf("_%d_%d_%d", iterations, addresses_count, threads);
  39. perf_test::PrintResult("RecordAddressCostPerCall", modifier, "", ns_per_call,
  40. "ns", true);
  41. }
  42. } // namespace
  43. class OrderfileInstrumentationTest : public ::testing::Test {
  44. // Any tests need to run ResetForTesting() when they start. Because this
  45. // perftest is built with instrumentation enabled, all code including
  46. // ::testing::Test is instrumented. If ResetForTesting() is called earlier,
  47. // for example in setUp(), any test harness code between setUp() and the
  48. // actual test will change the instrumentation offset record in unpredictable
  49. // ways and make these tests unreliable.
  50. };
  51. TEST_F(OrderfileInstrumentationTest, RecordOffset) {
  52. ResetForTesting();
  53. size_t first = 1234, second = 1456;
  54. RecordAddressForTesting(first);
  55. RecordAddressForTesting(second);
  56. RecordAddressForTesting(first); // No duplicates.
  57. RecordAddressForTesting(first + 1); // 4 bytes granularity.
  58. Disable();
  59. auto reached = GetOrderedOffsetsForTesting();
  60. EXPECT_EQ(2UL, reached.size());
  61. EXPECT_EQ(first - kStartOfTextForTesting, reached[0]);
  62. EXPECT_EQ(second - kStartOfTextForTesting, reached[1]);
  63. }
  64. TEST_F(OrderfileInstrumentationTest, RecordingStops) {
  65. ResetForTesting();
  66. size_t first = 1234, second = 1456, third = 1789;
  67. RecordAddressForTesting(first);
  68. RecordAddressForTesting(second);
  69. Disable();
  70. RecordAddressForTesting(third);
  71. auto reached = GetOrderedOffsetsForTesting();
  72. ASSERT_EQ(2UL, reached.size());
  73. ASSERT_EQ(first - kStartOfTextForTesting, reached[0]);
  74. ASSERT_EQ(second - kStartOfTextForTesting, reached[1]);
  75. }
  76. TEST_F(OrderfileInstrumentationTest, OutOfBounds) {
  77. ResetForTesting();
  78. EXPECT_DEATH(RecordAddressForTesting(kEndOfTextForTesting + 100), "");
  79. EXPECT_DEATH(RecordAddressForTesting(kStartOfTextForTesting - 100), "");
  80. }
  81. TEST(OrderfileInstrumentationPerfTest, RecordAddress_10_10000) {
  82. RunBenchmark(10, 10000, 1);
  83. }
  84. TEST(OrderfileInstrumentationPerfTest, RecordAddress_100_10000) {
  85. RunBenchmark(100, 10000, 1);
  86. }
  87. TEST(OrderfileInstrumentationPerfTest, RecordAddress_10_100000) {
  88. RunBenchmark(10, 100000, 1);
  89. }
  90. TEST(OrderfileInstrumentationPerfTest, RecordAddress_100_100000) {
  91. RunBenchmark(100, 100000, 1);
  92. }
  93. TEST(OrderfileInstrumentationPerfTest, RecordAddress_1000_100000_2) {
  94. RunBenchmark(1000, 100000, 2);
  95. }
  96. TEST(OrderfileInstrumentationPerfTest, RecordAddress_1000_100000_3) {
  97. RunBenchmark(1000, 100000, 3);
  98. }
  99. TEST(OrderfileInstrumentationPerfTest, RecordAddress_1000_100000_4) {
  100. RunBenchmark(1000, 100000, 4);
  101. }
  102. TEST(OrderfileInstrumentationPerfTest, RecordAddress_1000_100000_6) {
  103. RunBenchmark(1000, 100000, 6);
  104. }
  105. } // namespace orderfile
  106. } // namespace android
  107. } // namespace base
  108. // Custom runner implementation since base's one requires JNI on Android.
  109. int main(int argc, char** argv) {
  110. testing::InitGoogleTest(&argc, argv);
  111. return RUN_ALL_TESTS();
  112. }