Skip to content

Commit 2026acb

Browse files
committed
Adds support for exporting benchmarks in the old format.
1 parent d78865e commit 2026acb

3 files changed

Lines changed: 35 additions & 25 deletions

File tree

src/converter.cc

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -66,31 +66,26 @@ bool IncludeGaps(const Problem& problem) {
6666

6767
} // namespace
6868

69-
std::string ToCsv(const Problem& problem, Solution* solution) {
69+
std::string ToCsv(const Problem& problem, Solution* solution, bool old_format) {
7070
const bool include_alignment = IncludeAlignment(problem);
7171
const bool include_gaps = IncludeGaps(problem);
72+
const int addend = old_format ? -1 : 0;
7273
std::vector<std::string> header = {std::string(kId),
73-
std::string(kLower),
74-
std::string(kUpper),
74+
std::string(old_format ? kStart : kLower),
75+
std::string(old_format ? kEnd : kUpper),
7576
std::string(kSize)};
76-
if (include_alignment) {
77-
header.push_back(std::string(kAlignment));
78-
}
79-
if (include_gaps) {
80-
header.push_back(std::string(kGaps));
81-
}
82-
if (solution) {
83-
header.push_back(std::string(kOffset));
84-
}
77+
if (include_alignment) header.push_back(std::string(kAlignment));
78+
if (include_gaps) header.push_back(std::string(kGaps));
79+
if (solution) header.push_back(std::string(kOffset));
8580
std::vector<std::vector<std::string>> input = { header };
8681
for (auto buffer_idx = 0; buffer_idx < problem.buffers.size(); ++buffer_idx) {
8782
const Buffer& buffer = problem.buffers[buffer_idx];
8883
const auto& lifespan = buffer.lifespan;
8984
std::vector<std::string> gaps;
9085
gaps.reserve(buffer.gaps.size());
9186
for (const Gap& gap : buffer.gaps) {
92-
std::string gap_str =
93-
absl::StrCat(gap.lifespan.lower(), "-", gap.lifespan.upper());
87+
std::string gap_str = absl::StrCat(gap.lifespan.lower(), "-",
88+
gap.lifespan.upper() + addend);
9489
if (gap.window) {
9590
gap_str +=
9691
absl::StrCat("@", gap.window->lower(), ":", gap.window->upper());
@@ -99,17 +94,11 @@ std::string ToCsv(const Problem& problem, Solution* solution) {
9994
}
10095
std::vector<std::string> record = {absl::StrCat(buffer.id),
10196
absl::StrCat(lifespan.lower()),
102-
absl::StrCat(lifespan.upper()),
97+
absl::StrCat(lifespan.upper() + addend),
10398
absl::StrCat(buffer.size)};
104-
if (include_alignment) {
105-
record.push_back(absl::StrCat(buffer.alignment));
106-
}
107-
if (include_gaps) {
108-
record.push_back(absl::StrJoin(gaps, " "));
109-
}
110-
if (solution) {
111-
record.push_back(std::to_string(solution->offsets[buffer_idx]));
112-
}
99+
if (include_alignment) record.push_back(absl::StrCat(buffer.alignment));
100+
if (include_gaps) record.push_back(absl::StrJoin(gaps, " "));
101+
if (solution) record.push_back(absl::StrCat(solution->offsets[buffer_idx]));
113102
input.push_back(record);
114103
}
115104
std::ostringstream oss;

src/converter.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ namespace minimalloc {
3434
// 2,10,40,3,2
3535
//
3636
// If a solution is provided, an additional "offset" column will be created.
37-
std::string ToCsv(const Problem& problem, Solution* solution = nullptr);
37+
std::string ToCsv(const Problem& problem,
38+
Solution* solution = nullptr,
39+
bool old_format = false);
3840

3941
// Given a CSV like the one below (with buffers listed in any order), converts
4042
// it into a Problem instance or returns a status if the problem is malformed:

tests/converter_test.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,25 @@ TEST(ConverterTest, ToCsvStringIDs) {
139139
"Little,5,10,15,1,\nBig,6,12,18,2,7-8 9-10\n");
140140
}
141141

142+
TEST(ConverterTest, ToCsvOldFormat) {
143+
EXPECT_EQ(
144+
ToCsv({
145+
.buffers = {
146+
{.id = "Little", .lifespan = {5, 10}, .size = 15},
147+
{
148+
.id = "Big",
149+
.lifespan = {6, 12},
150+
.size = 18,
151+
.alignment = 2,
152+
.gaps = {{.lifespan = {7, 8}}, {.lifespan = {9, 10}}}
153+
},
154+
},
155+
.capacity = 40
156+
}, nullptr, true),
157+
"id,start,end,size,alignment,gaps\n"
158+
"Little,5,9,15,1,\nBig,6,11,18,2,7-7 9-9\n");
159+
}
160+
142161
TEST(ConverterTest, FromCsvProblemOnly) {
143162
EXPECT_EQ(
144163
*FromCsv("lower,size,id,upper\n6,18,1,12\n5,15,0,10\n"),

0 commit comments

Comments
 (0)