summaryrefslogtreecommitdiff
path: root/simpleperf/record_test.cpp
diff options
context:
space:
mode:
authorYabin Cui <yabinc@google.com>2016-08-04 10:22:17 -0700
committerYabin Cui <yabinc@google.com>2016-08-04 12:25:10 -0700
commit190a848fb2d4f502372b2528c55ca1f520e90609 (patch)
tree0f91a18d54c12e089e6454313577de959ff5095c /simpleperf/record_test.cpp
parent1f282c61fa7ba844688f370b64b3d547ca85d1ad (diff)
downloadextras-190a848fb2d4f502372b2528c55ca1f520e90609.tar.gz
simpleperf: keep binary in class Record.
It removes memory copy and heap allocation/deallocation in Record::BinaryFormat(), and is a preparation to remove memory copy and heap allocation in Record constructor. Bug: 30649868 Test: run simpleperf_unit_test. Change-Id: Ic8dd80e43f7b547a9beaf896d726b56aeb5d55a2
Diffstat (limited to 'simpleperf/record_test.cpp')
-rw-r--r--simpleperf/record_test.cpp82
1 files changed, 39 insertions, 43 deletions
diff --git a/simpleperf/record_test.cpp b/simpleperf/record_test.cpp
index 8c984e9d..da5a3ce3 100644
--- a/simpleperf/record_test.cpp
+++ b/simpleperf/record_test.cpp
@@ -29,29 +29,25 @@ class RecordTest : public ::testing::Test {
event_attr = CreateDefaultPerfEventAttr(*type);
}
- template <class RecordType>
- void CheckRecordMatchBinary(const RecordType& record);
+ void CheckRecordMatchBinary(const Record& record) {
+ const char* p = record.Binary();
+ std::vector<std::unique_ptr<Record>> records =
+ ReadRecordsFromBuffer(event_attr, p, record.size());
+ ASSERT_EQ(1u, records.size());
+ CheckRecordEqual(record, *records[0]);
+ }
perf_event_attr event_attr;
};
-template <class RecordType>
-void RecordTest::CheckRecordMatchBinary(const RecordType& record) {
- std::vector<char> binary = record.BinaryFormat();
- std::vector<std::unique_ptr<Record>> records =
- ReadRecordsFromBuffer(event_attr, binary.data(), binary.size());
- ASSERT_EQ(1u, records.size());
- CheckRecordEqual(record, *records[0]);
-}
-
TEST_F(RecordTest, MmapRecordMatchBinary) {
- MmapRecord record =
- MmapRecord::Create(event_attr, true, 1, 2, 0x1000, 0x2000, 0x3000, "MmapRecord", 0);
+ MmapRecord record(event_attr, true, 1, 2, 0x1000, 0x2000, 0x3000,
+ "MmapRecord", 0);
CheckRecordMatchBinary(record);
}
TEST_F(RecordTest, CommRecordMatchBinary) {
- CommRecord record = CommRecord::Create(event_attr, 1, 2, "CommRecord", 0);
+ CommRecord record(event_attr, 1, 2, "CommRecord", 0);
CheckRecordMatchBinary(record);
}
@@ -59,58 +55,58 @@ TEST_F(RecordTest, RecordCache_smoke) {
event_attr.sample_id_all = 1;
event_attr.sample_type |= PERF_SAMPLE_TIME;
RecordCache cache(true, 2, 2);
- MmapRecord r1 = MmapRecord::Create(event_attr, true, 1, 1, 0x100, 0x200, 0x300, "mmap_record1", 0);
- MmapRecord r2 = r1;
- MmapRecord r3 = r1;
- MmapRecord r4 = r1;
- r1.sample_id.time_data.time = 3;
- r2.sample_id.time_data.time = 1;
- r3.sample_id.time_data.time = 4;
- r4.sample_id.time_data.time = 6;
+ MmapRecord* r1 = new MmapRecord(event_attr, true, 1, 1, 0x100, 0x200, 0x300,
+ "mmap_record1", 0, 3);
+ MmapRecord* r2 = new MmapRecord(event_attr, true, 1, 1, 0x100, 0x200, 0x300,
+ "mmap_record1", 0, 1);
+ MmapRecord* r3 = new MmapRecord(event_attr, true, 1, 1, 0x100, 0x200, 0x300,
+ "mmap_record1", 0, 4);
+ MmapRecord* r4 = new MmapRecord(event_attr, true, 1, 1, 0x100, 0x200, 0x300,
+ "mmap_record1", 0, 6);
// Push r1.
- cache.Push(std::unique_ptr<Record>(new MmapRecord(r1)));
+ cache.Push(std::unique_ptr<Record>(r1));
ASSERT_EQ(nullptr, cache.Pop());
// Push r2.
- cache.Push(std::unique_ptr<Record>(new MmapRecord(r2)));
+ cache.Push(std::unique_ptr<Record>(r2));
// Pop r2.
std::unique_ptr<Record> popped_r = cache.Pop();
ASSERT_TRUE(popped_r != nullptr);
- CheckRecordEqual(r2, *popped_r);
+ ASSERT_EQ(r2, popped_r.get());
ASSERT_EQ(nullptr, cache.Pop());
// Push r3.
- cache.Push(std::unique_ptr<Record>(new MmapRecord(r3)));
+ cache.Push(std::unique_ptr<Record>(r3));
ASSERT_EQ(nullptr, cache.Pop());
// Push r4.
- cache.Push(std::unique_ptr<Record>(new MmapRecord(r4)));
+ cache.Push(std::unique_ptr<Record>(r4));
// Pop r1.
popped_r = cache.Pop();
ASSERT_TRUE(popped_r != nullptr);
- CheckRecordEqual(r1, *popped_r);
+ ASSERT_EQ(r1, popped_r.get());
// Pop r3.
popped_r = cache.Pop();
ASSERT_TRUE(popped_r != nullptr);
- CheckRecordEqual(r3, *popped_r);
+ ASSERT_EQ(r3, popped_r.get());
ASSERT_EQ(nullptr, cache.Pop());
// Pop r4.
std::vector<std::unique_ptr<Record>> last_records = cache.PopAll();
ASSERT_EQ(1u, last_records.size());
- CheckRecordEqual(r4, *last_records[0]);
+ ASSERT_EQ(r4, last_records[0].get());
}
TEST_F(RecordTest, RecordCache_FIFO) {
event_attr.sample_id_all = 1;
event_attr.sample_type |= PERF_SAMPLE_TIME;
RecordCache cache(true, 2, 2);
- std::vector<MmapRecord> records;
+ std::vector<MmapRecord*> records;
for (size_t i = 0; i < 10; ++i) {
- MmapRecord r = MmapRecord::Create(event_attr, true, 1, i, 0x100, 0x200, 0x300, "mmap_record1", 0);
- records.push_back(r);
- cache.Push(std::unique_ptr<Record>(new MmapRecord(r)));
+ records.push_back(new MmapRecord(event_attr, true, 1, i, 0x100, 0x200,
+ 0x300, "mmap_record1", 0));
+ cache.Push(std::unique_ptr<Record>(records.back()));
}
std::vector<std::unique_ptr<Record>> out_records = cache.PopAll();
ASSERT_EQ(records.size(), out_records.size());
for (size_t i = 0; i < records.size(); ++i) {
- CheckRecordEqual(records[i], *out_records[i]);
+ ASSERT_EQ(records[i], out_records[i].get());
}
}
@@ -118,18 +114,18 @@ TEST_F(RecordTest, RecordCache_PushRecordVector) {
event_attr.sample_id_all = 1;
event_attr.sample_type |= PERF_SAMPLE_TIME;
RecordCache cache(true, 2, 2);
- MmapRecord r1 = MmapRecord::Create(event_attr, true, 1, 1, 0x100, 0x200, 0x300, "mmap_record1", 0);
- MmapRecord r2 = r1;
- r1.sample_id.time_data.time = 1;
- r2.sample_id.time_data.time = 3;
+ MmapRecord* r1 = new MmapRecord(event_attr, true, 1, 1, 0x100, 0x200, 0x300,
+ "mmap_record1", 0, 1);
+ MmapRecord* r2 = new MmapRecord(event_attr, true, 1, 1, 0x100, 0x200, 0x300,
+ "mmap_record1", 0, 3);
std::vector<std::unique_ptr<Record>> records;
- records.push_back(std::unique_ptr<Record>(new MmapRecord(r1)));
- records.push_back(std::unique_ptr<Record>(new MmapRecord(r2)));
+ records.push_back(std::unique_ptr<Record>(r1));
+ records.push_back(std::unique_ptr<Record>(r2));
cache.Push(std::move(records));
std::unique_ptr<Record> popped_r = cache.Pop();
ASSERT_TRUE(popped_r != nullptr);
- CheckRecordEqual(r1, *popped_r);
+ ASSERT_EQ(r1, popped_r.get());
std::vector<std::unique_ptr<Record>> last_records = cache.PopAll();
ASSERT_EQ(1u, last_records.size());
- CheckRecordEqual(r2, *last_records[0]);
+ ASSERT_EQ(r2, last_records[0].get());
}