[render-test] Add instruction for verifying file size

For a given absolute or relative path, measure the size of the file.

This is useful for measuring if the cache has increased as expected
after moving a camera or decreased after cleaning up.

In a more hackish manner, could be used for collecting binary size
statistics of shared libraries.
This commit is contained in:
Thiago Marcos P. Santos
2019-10-09 16:12:02 +03:00
parent 026e15fe3d
commit a9f06f3b52
3 changed files with 98 additions and 1 deletions

View File

@@ -31,6 +31,14 @@ struct TestPaths {
}
};
struct FileSizeProbe {
FileSizeProbe() = default;
FileSizeProbe(std::string path_, uintmax_t size_) : path(std::move(path_)), size(size_) {}
std::string path;
uintmax_t size;
};
struct MemoryProbe {
MemoryProbe() = default;
MemoryProbe(size_t peak_, size_t allocations_)
@@ -43,7 +51,8 @@ struct MemoryProbe {
class TestMetrics {
public:
bool isEmpty() const { return memory.empty(); }
bool isEmpty() const { return fileSize.empty() && memory.empty(); }
std::map<std::string, FileSizeProbe> fileSize;
std::map<std::string, MemoryProbe> memory;
};

View File

@@ -273,6 +273,21 @@ std::string serializeMetrics(const TestMetrics& metrics) {
rapidjson::Writer<rapidjson::StringBuffer> writer(s);
writer.StartObject();
// Start fileSize section.
writer.Key("fileSize");
writer.StartArray();
for (const auto& fileSizeProbe : metrics.fileSize) {
assert(!fileSizeProbe.first.empty());
writer.StartArray();
writer.String(fileSizeProbe.first.c_str());
writer.String(fileSizeProbe.second.path);
writer.Uint64(fileSizeProbe.second.size);
writer.EndArray();
}
// End fileSize section.
writer.EndArray();
// Start memory section.
writer.Key("memory");
writer.StartArray();
@@ -286,6 +301,7 @@ std::string serializeMetrics(const TestMetrics& metrics) {
}
// End memory section.
writer.EndArray();
writer.EndObject();
return s.GetString();
@@ -439,6 +455,29 @@ TestMetrics readExpectedMetrics(const mbgl::filesystem::path& path) {
}
const auto& document = maybeJson.get<mbgl::JSDocument>();
if (document.HasMember("fileSize")) {
const mbgl::JSValue& fileSizeValue = document["fileSize"];
assert(fileSizeValue.IsArray());
for (auto& probeValue : fileSizeValue.GetArray()) {
assert(probeValue.IsArray());
assert(probeValue.Size() >= 3u);
assert(probeValue[0].IsString());
assert(probeValue[1].IsString());
assert(probeValue[2].IsNumber());
std::string mark{probeValue[0].GetString(), probeValue[0].GetStringLength()};
assert(!mark.empty());
std::string filePath{probeValue[1].GetString(), probeValue[1].GetStringLength()};
assert(!filePath.empty());
result.fileSize.emplace(std::piecewise_construct,
std::forward_as_tuple(std::move(mark)),
std::forward_as_tuple(std::move(filePath), probeValue[2].GetUint64()));
}
}
if (document.HasMember("memory")) {
const mbgl::JSValue& memoryValue = document["memory"];
assert(memoryValue.IsArray());

View File

@@ -242,6 +242,31 @@ bool TestRunner::checkRenderTestResults(mbgl::PremultipliedImage&& actualImage,
break;
}
}
// Check file size metrics.
for (const auto& expected : metadata.expectedMetrics.fileSize) {
auto actual = metadata.metrics.fileSize.find(expected.first);
if (actual == metadata.metrics.fileSize.end()) {
metadata.errorMessage = "Failed to find fileSize probe: " + expected.first;
return false;
}
if (actual->second.path != expected.second.path) {
std::stringstream ss;
ss << "Comparing different files at probe \"" << expected.first << "\": " << actual->second.path
<< ", expected is " << expected.second.path << ".";
metadata.errorMessage = ss.str();
return false;
}
if (actual->second.size != expected.second.size) {
std::stringstream ss;
ss << "File size does not match at probe \"" << expected.first << "\": " << actual->second.size
<< ", expected is " << expected.second.size << ".";
metadata.errorMessage = ss.str();
return false;
}
}
// Check memory metrics.
for (const auto& expected : metadata.expectedMetrics.memory) {
auto actual = metadata.metrics.memory.find(expected.first);
@@ -313,6 +338,7 @@ bool TestRunner::runOperations(const std::string& key, TestMetadata& metadata) {
static const std::string removeSourceOp("removeSource");
static const std::string setPaintPropertyOp("setPaintProperty");
static const std::string setLayoutPropertyOp("setLayoutProperty");
static const std::string fileSizeProbeOp("probeFileSize");
static const std::string memoryProbeOp("probeMemory");
static const std::string memoryProbeStartOp("probeMemoryStart");
static const std::string memoryProbeEndOp("probeMemoryEnd");
@@ -562,6 +588,29 @@ bool TestRunner::runOperations(const std::string& key, TestMetadata& metadata) {
const mbgl::JSValue* propertyValue = &operationArray[3];
layer->setLayoutProperty(propertyName, propertyValue);
}
// probeFileSize
} else if (operationArray[0].GetString() == fileSizeProbeOp) {
assert(operationArray.Size() >= 3u);
assert(operationArray[1].IsString());
assert(operationArray[2].IsString());
std::string mark = std::string(operationArray[1].GetString(), operationArray[1].GetStringLength());
mbgl::filesystem::path path = std::string(operationArray[2].GetString(), operationArray[2].GetStringLength());
assert(!path.empty());
if (!path.is_absolute()) {
path = metadata.paths.defaultExpectations() / path;
}
if (mbgl::filesystem::exists(path)) {
auto size = mbgl::filesystem::file_size(path);
metadata.metrics.fileSize.emplace(std::piecewise_construct,
std::forward_as_tuple(std::move(mark)),
std::forward_as_tuple(std::move(path), size));
} else {
metadata.errorMessage = std::string("File not found: ") + path.string();
return false;
}
// probeMemoryStart
} else if (operationArray[0].GetString() == memoryProbeStartOp) {
assert(!AllocationIndex::isActive());