Skip to content

Commit

Permalink
ResultsFile: Support storing results in an XML file
Browse files Browse the repository at this point in the history
  • Loading branch information
afrantzis committed Jan 4, 2023
1 parent 710025c commit 08ea092
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
2 changes: 1 addition & 1 deletion doc/glmark2.1.in
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Run in fullscreen mode (equivalent to --size -1x-1)
The types of results to report for each benchmark, as a ':' separated list [fps,cpu,shader]
.TP
\fB\-\-results-file\fR RESULTS-FILE
The file to save the results to, in the format determined by the file extension [csv]
The file to save the results to, in the format determined by the file extension [csv,xml]
.TP
\fB\-\-winsys-options\fR OPTS
A list of 'opt=value' pairs for window system specific options, separated by ':'
Expand Down
2 changes: 1 addition & 1 deletion src/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ Options::print_help()
" --results RESULTS The types of results to report for each benchmark,\n"
" as a ':' separated list [fps,cpu,shader]\n"
" --results-file F The file to save the results to, in the format determined\n"
" by the file extension [csv]\n"
" by the file extension [csv,xml]\n"
" --winsys-options O A list of 'opt=value' pairs for window system specific\n"
" options, separated by ':'\n"
" -l, --list-scenes Display information about the available scenes\n"
Expand Down
70 changes: 70 additions & 0 deletions src/results-file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,72 @@ class CSVResultsFile : public ResultsFile
bool first_field = true;
};

std::string xml_text_escape(const std::string &str)
{
std::stringstream ss;

for (auto c : str)
{
switch (c)
{
case '<': ss << "&lt;"; break;
case '>': ss << "&gt;"; break;
case '&': ss << "&amp;"; break;
default: ss << c; break;
}
}

return ss.str();
}

class XMLResultsFile : public ResultsFile
{
public:
XMLResultsFile(std::ofstream &&fs) : fs{std::move(fs)} {}

std::string type() override { return "XML"; }

void begin() override
{
fs << "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>" << std::endl;
fs << "<glmark2>" << std::endl;
}

void end() override
{
fs << "</glmark2>" << std::endl;
}

void begin_info() override
{
fs << " <info>" << std::endl;
}

void end_info() override
{
fs << " </info>" << std::endl;
}

void begin_benchmark() override
{
fs << " <benchmark>" << std::endl;
}

void end_benchmark() override
{
fs << " </benchmark>" << std::endl;
}

void add_field(const std::string &name, const std::string &value) override
{
std::string escaped = xml_text_escape(value);
fs << " <" << name << ">" << escaped << "</" << name << ">" << std::endl;
}

private:
std::ofstream fs;
};

std::string get_file_extension(const std::string &str)
{
auto i = str.rfind('.');
Expand Down Expand Up @@ -144,6 +210,10 @@ bool ResultsFile::init(const std::string &file)
{
ResultsFile::singleton = std::make_unique<CSVResultsFile>(std::move(fs));
}
else if (ext == ".xml")
{
ResultsFile::singleton = std::make_unique<XMLResultsFile>(std::move(fs));
}
else
{
Log::error("Results file type %s is not supported\n", file.c_str());
Expand Down

0 comments on commit 08ea092

Please sign in to comment.