| 1 | #include "impedance.hh" | 
|---|
| 2 | #include "log.hh" | 
|---|
| 3 | #include <iostream> | 
|---|
| 4 |  | 
|---|
| 5 | using namespace std; | 
|---|
| 6 |  | 
|---|
| 7 | static map<const char *, long> occurances_in_failed_query; | 
|---|
| 8 | static map<const char *, long> occurances_in_ok_query; | 
|---|
| 9 | static map<const char *, long> retries; | 
|---|
| 10 | static map<const char *, long> limited; | 
|---|
| 11 | static map<const char *, long> failed; | 
|---|
| 12 |  | 
|---|
| 13 | impedance_visitor::impedance_visitor(map<const char *, long> &occured) : _occured(occured) { | 
|---|
| 14 | } | 
|---|
| 15 |  | 
|---|
| 16 | void impedance_visitor::visit(struct prod *p) { | 
|---|
| 17 | found[typeid(*p).name()] = true; | 
|---|
| 18 | } | 
|---|
| 19 |  | 
|---|
| 20 | impedance_visitor::~impedance_visitor() { | 
|---|
| 21 | for (auto pair : found) | 
|---|
| 22 | _occured[pair.first]++; | 
|---|
| 23 | } | 
|---|
| 24 |  | 
|---|
| 25 | void impedance_feedback::executed(prod &query) { | 
|---|
| 26 | impedance_visitor v(occurances_in_ok_query); | 
|---|
| 27 | query.accept(&v); | 
|---|
| 28 | } | 
|---|
| 29 |  | 
|---|
| 30 | void impedance_feedback::error(prod &query, const dut::failure &e) { | 
|---|
| 31 | (void)e; | 
|---|
| 32 | impedance_visitor v(occurances_in_failed_query); | 
|---|
| 33 | query.accept(&v); | 
|---|
| 34 | } | 
|---|
| 35 |  | 
|---|
| 36 | namespace impedance { | 
|---|
| 37 |  | 
|---|
| 38 | bool matched(const char *name) { | 
|---|
| 39 | if (100 > occurances_in_failed_query[name]) | 
|---|
| 40 | return true; | 
|---|
| 41 | double error_rate = | 
|---|
| 42 | (double)occurances_in_failed_query[name] / (occurances_in_failed_query[name] + occurances_in_ok_query[name]); | 
|---|
| 43 | if (error_rate > 0.99) | 
|---|
| 44 | return false; | 
|---|
| 45 | return true; | 
|---|
| 46 | } | 
|---|
| 47 |  | 
|---|
| 48 | void report() { | 
|---|
| 49 | cerr << "impedance report: "<< endl; | 
|---|
| 50 | for (auto pair : occurances_in_failed_query) { | 
|---|
| 51 | cerr << "  "<< pretty_type(pair.first) << ": "<< pair.second << "/"<< occurances_in_ok_query[pair.first] | 
|---|
| 52 | << " (bad/ok)"; | 
|---|
| 53 | if (!matched(pair.first)) | 
|---|
| 54 | cerr << " -> BLACKLISTED"; | 
|---|
| 55 | cerr << endl; | 
|---|
| 56 | } | 
|---|
| 57 | } | 
|---|
| 58 |  | 
|---|
| 59 | void report(std::ostream &out) { | 
|---|
| 60 | out << "{\"impedance\": [ "<< endl; | 
|---|
| 61 |  | 
|---|
| 62 | for (auto pair = occurances_in_failed_query.begin(); pair != occurances_in_failed_query.end(); ++pair) { | 
|---|
| 63 | out << "{\"prod\": \""<< pretty_type(pair->first) << "\"," | 
|---|
| 64 | << "\"bad\": "<< pair->second << ", " | 
|---|
| 65 | << "\"ok\": "<< occurances_in_ok_query[pair->first] << ", " | 
|---|
| 66 | << "\"limited\": "<< limited[pair->first] << ", " | 
|---|
| 67 | << "\"failed\": "<< failed[pair->first] << ", " | 
|---|
| 68 | << "\"retries\": "<< retries[pair->first] << "} "; | 
|---|
| 69 |  | 
|---|
| 70 | if (next(pair) != occurances_in_failed_query.end()) | 
|---|
| 71 | out << ","<< endl; | 
|---|
| 72 | } | 
|---|
| 73 | out << "]}"<< endl; | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 | void retry(const char *p) { | 
|---|
| 77 | retries[p]++; | 
|---|
| 78 | } | 
|---|
| 79 |  | 
|---|
| 80 | void limit(const char *p) { | 
|---|
| 81 | limited[p]++; | 
|---|
| 82 | } | 
|---|
| 83 |  | 
|---|
| 84 | void fail(const char *p) { | 
|---|
| 85 | failed[p]++; | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|
| 88 | } // namespace impedance | 
|---|
| 89 |  | 
|---|