void calculateMetric(CSelectionData *selectionData, const std::string &name, rapidjson::Document &results) { ITestSuiteMetricPlugin *metric = kernel.getTestSuiteMetricPluginManager().getPlugin(name); StringVector dependencies = metric->getDependency(); for (StringVector::iterator it = dependencies.begin(); it != dependencies.end(); it++) { if (metricsCalculated.find(*it) == metricsCalculated.end()) { calculateMetric(selectionData, *it, results); } } (std::cerr << "[INFO] Calculating metrics: " << metric->getName() << " ...").flush(); metric->init(selectionData, &clusterList, revision); metric->calculate(results); metricsCalculated.insert(name); (std::cerr << " done." << std::endl).flush(); }
int main(int argc, char* argv[]) { std::cout << "test-suite-metrics (SoDA tool)" << std::endl; options_description desc("Options"); desc.add_options() ("help,h", "Prints help message") ("create-json-file,j", "Creates a sample json file") ("list-cluster-algorithms,c", "Lists the cluster algorithms") ("list-metric-plugins,m", "Lists the metric plugins"); variables_map vm; store(parse_command_line(argc, argv, desc), vm); notify(vm); if (argc < 2) { std::cerr << "[ERROR] There are no arguments!" << std::endl; printHelp(); return 1; } if (vm.count("help")) { printHelp(); std::cout << desc << std::endl; return 0; } if (vm.count("list-cluster-algorithms")) { printPluginNames("cluster", kernel.getTestSuiteClusterPluginManager().getPluginNames()); return 0; } if (vm.count("list-metric-plugins")) { printPluginNames("metric", kernel.getTestSuiteMetricPluginManager().getPluginNames()); return 0; } if (vm.count("create-json-file")) { createJsonFile(); return 0; } return loadJsonFiles(String(argv[1])); }
void processJsonFiles(String path) { try { std::cout << "[INFO] Processing " << path << " configuration file." << std::endl; rapidjson::Document reader; { FILE *in = fopen (path.c_str(), "r"); char readBuffer[65536]; rapidjson::FileReadStream is(in, readBuffer, sizeof(readBuffer)); reader.ParseStream<0, rapidjson::UTF8<>, rapidjson::FileReadStream>(is); fclose(in); } boost::filesystem::path jsonPath(path); std::string clusterAlgorithmName = reader["cluster-algorithm"].GetString(); ITestSuiteClusterPlugin *clusterAlgorithm = kernel.getTestSuiteClusterPluginManager().getPlugin(clusterAlgorithmName); clusterAlgorithm->init(reader); CSelectionData *selectionData = new CSelectionData(); StringVector metrics; for (rapidjson::Value::ConstValueIterator itr = reader["metrics"].Begin(); itr != reader["metrics"].End(); ++itr) metrics.push_back(itr->GetString()); if (metrics.empty()) { std::cerr << "[ERROR] Missing metrics parameter in config file " << path << "." << std::endl; return; } else { for (StringVector::const_iterator it = metrics.begin(); it != metrics.end(); ++it) { try { kernel.getTestSuiteMetricPluginManager().getPlugin(*it); } catch (std::out_of_range &e) { std::cerr << "[ERROR] Invalid metric name(" << *it << ") in configuration file: " << path << "." << std::endl; return; } } } revision = reader["revision"].GetInt(); outputDir = reader["output-dir"].GetString(); if (outputDir.empty()) { std::cerr << "[ERROR] Missing output-dir parameter in config file " << path << "." << std::endl; return; } if (outputDir[0] == '.') outputDir = jsonPath.parent_path().string() + "/" + outputDir; if (!exists(outputDir)) boost::filesystem::create_directory(boost::filesystem::path(outputDir)); String covPath = reader["coverage-data"].GetString(); if (covPath[0] == '.') { covPath = jsonPath.parent_path().string() + "/" + covPath; } String resPath = reader["results-data"].GetString(); if (resPath[0] == '.') { resPath = jsonPath.parent_path().string() + "/" + resPath; } if (exists(covPath) && exists(resPath)) { (std::cerr << "[INFO] loading coverage from " << covPath << " ...").flush(); selectionData->loadCoverage(covPath); (std::cerr << " done\n[INFO] loading results from " << resPath << " ...").flush(); selectionData->loadResults(resPath); (std::cerr << " done" << std::endl).flush(); } else { std::cerr << "[ERROR] Missing or invalid input files in config file " << path << "." << std::endl; return; } if (reader["globalize"].GetBool()) { (std::cerr << "[INFO] Globalizing ...").flush(); selectionData->globalize(); (std::cerr << " done" << std::endl).flush(); } clusterList.clear(); metricsCalculated.clear(); (std::cerr << "[INFO] Running cluster algorithm: " << clusterAlgorithm->getName() << " ...").flush(); clusterAlgorithm->execute(*selectionData, clusterList); (std::cerr << " done" << std::endl).flush(); rapidjson::Document results; results.SetObject(); for (StringVector::iterator it = metrics.begin(); it != metrics.end(); it++) { if (metricsCalculated.find(*it) == metricsCalculated.end()) { calculateMetric(selectionData, *it, results); } } saveResults(results); delete selectionData; } catch (std::exception &e) { std::cerr << e.what() << std::endl; return; } catch (...) { std::cerr << "Exception of unknown type while processsing configuration file(" << path << ") arguments." << std::endl; return; } return; }