[NFC][Coroutines] Use structured binding with llvm::enumerate in CoroSplit (#116879)
[llvm-project.git] / lldb / unittests / Target / SummaryStatisticsTest.cpp
blobf4033752845408a0865a8e832e5fc8d778286fb0
1 #include "lldb/DataFormatters/TypeSummary.h"
2 #include "lldb/Target/Statistics.h"
3 #include "lldb/Utility/Stream.h"
4 #include "lldb/lldb-forward.h"
5 #include "lldb/lldb-private-enumerations.h"
6 #include "lldb/lldb-private.h"
7 #include "llvm/Testing/Support/Error.h"
8 #include "gtest/gtest.h"
9 #include <thread>
11 using namespace lldb_private;
12 using Duration = std::chrono::duration<double>;
14 class DummySummaryImpl : public lldb_private::TypeSummaryImpl {
15 public:
16 DummySummaryImpl()
17 : TypeSummaryImpl(TypeSummaryImpl::Kind::eSummaryString,
18 TypeSummaryImpl::Flags()) {}
20 std::string GetName() override { return "DummySummary"; }
22 std::string GetSummaryKindName() override { return "dummy"; }
24 std::string GetDescription() override { return ""; }
26 bool FormatObject(ValueObject *valobj, std::string &dest,
27 const TypeSummaryOptions &options) override {
28 return false;
32 TEST(MultithreadFormatting, Multithread) {
33 SummaryStatisticsCache statistics_cache;
34 DummySummaryImpl summary;
35 std::vector<std::thread> threads;
36 for (int i = 0; i < 10; ++i) {
37 threads.emplace_back(std::thread([&statistics_cache, &summary]() {
38 auto sp = statistics_cache.GetSummaryStatisticsForProvider(summary);
40 SummaryStatistics::SummaryInvocation invocation(sp);
41 std::this_thread::sleep_for(Duration(1));
43 }));
46 for (auto &thread : threads)
47 thread.join();
49 auto sp = statistics_cache.GetSummaryStatisticsForProvider(summary);
50 ASSERT_TRUE(sp->GetDurationReference().get().count() > 10);
51 ASSERT_TRUE(sp->GetSummaryCount() == 10);
53 std::string stats_as_json;
54 llvm::raw_string_ostream ss(stats_as_json);
55 ss << sp->ToJSON();
56 ASSERT_THAT(stats_as_json, ::testing::HasSubstr("\"name\":\"DummySummary\""));
57 ASSERT_THAT(stats_as_json, ::testing::HasSubstr("\"type\":\"dummy\""));