SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
CSVWriter.h
Go to the documentation of this file.
1 #pragma once
2 
3 
8 
9 #include <fstream>
10 #include <map>
11 #include <string>
12 
13 namespace benchmax {
14 
15 /**
16  * Writes results to a csv file.
17  */
18 class CSVWriter {
19 private:
20  std::ofstream mFile;
21 
22  void replace(std::string& s, const std::string& pattern, const std::string& replacement) const {
23  std::size_t pos = 0;
24  while ((pos = s.find(pattern, pos)) != std::string::npos) {
25  s.replace(pos, pattern.length(), replacement);
26  pos += replacement.length();
27  }
28  }
29  std::string sanitize(const std::string& s, bool eliminateSlashes = false) const {
30  std::string res(s);
31  //replace(res, "<", "&lt;");
32  //replace(res, ">", "&gt;");
33  if (eliminateSlashes) replace(res, "/", ".");
34  return res;
35  }
36  std::string sanitizeTool(const std::unique_ptr<Tool>& tool) const {
37  return sanitize(remove_prefix(tool->binary(), settings_tools().tools_common_prefix), false);
38  }
39  std::string sanitizeFile(const fs::path& file) const {
40  return sanitize(remove_prefix(file, settings_benchmarks().input_directories_common_prefix), false);
41  }
42  template<typename Results>
43  std::vector<std::string> collectStatistics(const Jobs& jobs, const Results& results, Tool* tool) const {
44  std::vector<std::string> res;
45  for (const auto& filename: jobs.files()) { // for (const auto& run: results.data())
46  const auto& run = results.get(tool, filename);
47  if (run) {
48  run->get().restore();
49  for (const auto& stat: run->get().additional) {
50  res.emplace_back(stat.first);
51  }
52  run->get().store();
53  }
54  }
55  std::sort(res.begin(), res.end());
56  res.erase(
57  std::unique(res.begin(), res.end()),
58  res.end()
59  );
60  return res;
61  }
62 public:
63  /// Open file for writing.
64  CSVWriter(const std::string& filename): mFile(filename) {
65  }
66 
67  /// Write results to file.
68  template<typename Results>
69  void write(const Jobs& jobs, const Results& results) {
70  std::map<Tool*,std::vector<std::string>> tool_stats;
71  {
72  std::stringstream second_row;
73  // mFile << ",";
74  // second_row << ",";
75  for (const auto& tool: jobs.tools()) {
76  mFile << "," << sanitizeTool(tool) << "," << sanitizeTool(tool) << "," << sanitizeTool(tool);
77  second_row << ",answer,exitcode,runtime";
78  tool_stats.emplace(tool.get(), collectStatistics(jobs, results, tool.get()));
79  for (const auto& s: tool_stats.at(tool.get())) {
80  mFile << "," << sanitizeTool(tool);
81  second_row << "," << sanitize(s);
82  }
83  }
84  mFile << std::endl << second_row.rdbuf() << std::endl;
85  }
86  for (const auto& filename: jobs.files()) {
87  mFile << "\"" << sanitizeFile(filename) << "\"";
88  for (const auto& tool: jobs.tools()) {
89  const auto& result = results.get(tool.get(), filename);
90  if (!result) {
91  mFile << ",,,";
92  for (std::size_t i = 0; i < tool_stats.at(tool.get()).size(); i++) {
93  mFile << ",";
94  }
95  } else {
96  const auto& res = result->get();
97  res.restore();
98  mFile << "," << res.answer << "," << res.exitCode << "," << std::chrono::milliseconds(res.time).count();
99  for (const auto& stat : tool_stats.at(tool.get())) {
100  mFile << ",";
101  if (res.additional.find(stat) != res.additional.end()) {
102  mFile << "\"" << res.additional.at(stat) << "\"";
103  }
104  }
105  res.forget();
106  }
107  }
108  mFile << std::endl;
109  }
110  }
111 };
112 
113 }
Writes results to a csv file.
Definition: CSVWriter.h:18
std::string sanitizeFile(const fs::path &file) const
Definition: CSVWriter.h:39
void replace(std::string &s, const std::string &pattern, const std::string &replacement) const
Definition: CSVWriter.h:22
CSVWriter(const std::string &filename)
Open file for writing.
Definition: CSVWriter.h:64
std::string sanitize(const std::string &s, bool eliminateSlashes=false) const
Definition: CSVWriter.h:29
std::vector< std::string > collectStatistics(const Jobs &jobs, const Results &results, Tool *tool) const
Definition: CSVWriter.h:43
void write(const Jobs &jobs, const Results &results)
Write results to file.
Definition: CSVWriter.h:69
std::string sanitizeTool(const std::unique_ptr< Tool > &tool) const
Definition: CSVWriter.h:36
std::ofstream mFile
Definition: CSVWriter.h:20
Represents a set of jobs, constructed as the cartesian product of a set of tools and a set of benchma...
Definition: Jobs.h:70
const auto & files() const
Returns the set of files.
Definition: Jobs.h:92
const auto & tools() const
Returns the set of tools.
Definition: Jobs.h:88
Stores results for for whole benchmax run.
Definition: Results.h:33
std::optional< std::reference_wrapper< const BenchmarkResult > > get(const Tool *tool, const fs::path &file) const
Definition: Results.h:41
Base class for any tool.
Definition: Tool.h:38
void sort(T *array, int size, LessThan lt)
Definition: Sort.h:67
const auto & settings_benchmarks()
Return the benchmark settings.
Definition: benchmarks.h:41
std::filesystem::path remove_prefix(const std::filesystem::path &s, const std::filesystem::path &prefix)
Remove a prefix from a path.
Definition: filesystem.h:54
const auto & settings_tools()
Returns the tool settings.
Definition: Tools.h:59