profiler_ut.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include "profiler.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. namespace NAllocProfiler {
  4. ////////////////////////////////////////////////////////////////////////////////
  5. Y_UNIT_TEST_SUITE(Profiler) {
  6. Y_UNIT_TEST(StackCollection)
  7. {
  8. TStringStream str;
  9. NAllocProfiler::StartAllocationSampling(true);
  10. TVector<TAutoPtr<int>> test;
  11. // Do many allocations and no deallocations
  12. for (int i = 0; i < 10000; ++i) {
  13. test.push_back(new int);
  14. }
  15. NAllocProfiler::StopAllocationSampling(str);
  16. //Cout << str.Str() << Endl;
  17. #if !defined(ARCH_AARCH64)
  18. /* Check that output resembles this:
  19. STACK #2: 0 Allocs: 10 Frees: 0 CurrentSize: 40
  20. 0000000000492353 ??
  21. 000000000048781F operator new(unsigned long) +1807
  22. 00000000003733FA NAllocProfiler::NTestSuiteProfiler::TTestCaseStackCollection::Execute_(NUnitTest::TTestContext&) +218
  23. 00000000004A1938 NUnitTest::TTestBase::Run(std::__y1::function<void ()>, TString, char const*, bool) +120
  24. 0000000000375656 NAllocProfiler::NTestSuiteProfiler::TCurrentTest::Execute() +342
  25. 00000000004A20CF NUnitTest::TTestFactory::Execute() +847
  26. 000000000049922D NUnitTest::RunMain(int, char**) +1965
  27. 00007FF665778F45 __libc_start_main +245
  28. */
  29. UNIT_ASSERT_STRING_CONTAINS(str.Str(), "StackCollection");
  30. UNIT_ASSERT_STRING_CONTAINS(str.Str(), "NUnitTest::TTestBase::Run");
  31. UNIT_ASSERT_STRING_CONTAINS(str.Str(), "NAllocProfiler::NTestSuiteProfiler::TCurrentTest::Execute");
  32. UNIT_ASSERT_STRING_CONTAINS(str.Str(), "NUnitTest::TTestFactory::Execute");
  33. UNIT_ASSERT_STRING_CONTAINS(str.Str(), "NUnitTest::RunMain");
  34. #endif
  35. }
  36. class TAllocDumper : public NAllocProfiler::TAllocationStatsDumper {
  37. public:
  38. explicit TAllocDumper(IOutputStream& out) : NAllocProfiler::TAllocationStatsDumper(out) {}
  39. TString FormatTag(int tag) override {
  40. UNIT_ASSERT_VALUES_EQUAL(tag, 42);
  41. return "TAG_NAME_42";
  42. }
  43. };
  44. Y_UNIT_TEST(TagNames)
  45. {
  46. TStringStream str;
  47. NAllocProfiler::StartAllocationSampling(true);
  48. TVector<TAutoPtr<int>> test;
  49. NAllocProfiler::TProfilingScope scope(42);
  50. // Do many allocations and no deallocations
  51. for (int i = 0; i < 10000; ++i) {
  52. test.push_back(new int);
  53. }
  54. TAllocDumper dumper(str);
  55. NAllocProfiler::StopAllocationSampling(dumper);
  56. #if !defined(ARCH_AARCH64)
  57. UNIT_ASSERT_STRING_CONTAINS(str.Str(), "TAG_NAME_42");
  58. #endif
  59. }
  60. }
  61. }