SQLsmith  v1.2.1-5-gfacd7a8
A random SQL query generator
impedance.cc
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)
14  : _occured(occured)
15 { }
16 
17 void impedance_visitor::visit(struct prod *p)
18 {
19  found[typeid(*p).name()] = true;
20 }
21 
22 impedance_visitor::~impedance_visitor()
23 {
24  for(auto pair : found)
25  _occured[pair.first]++;
26 }
27 
28 void impedance_feedback::executed(prod &query)
29 {
30  impedance_visitor v(occurances_in_ok_query);
31  query.accept(&v);
32 }
33 
34 void impedance_feedback::error(prod &query, const dut::failure &e)
35 {
36  (void)e;
37  impedance_visitor v(occurances_in_failed_query);
38  query.accept(&v);
39 }
40 
41 namespace impedance {
42 
43 bool matched(const char *name)
44 {
45  if (100 > occurances_in_failed_query[name])
46  return true;
47  double error_rate = (double)occurances_in_failed_query[name]
48  / (occurances_in_failed_query[name] + occurances_in_ok_query[name]);
49  if (error_rate > 0.99)
50  return false;
51  return true;
52 }
53 
54 void report()
55 {
56  cerr << "impedance report: " << endl;
57  for (auto pair : occurances_in_failed_query) {
58  cerr << " " << pretty_type(pair.first) << ": " <<
59  pair.second << "/" << occurances_in_ok_query[pair.first]
60  << " (bad/ok)";
61  if (!matched(pair.first))
62  cerr << " -> BLACKLISTED";
63  cerr << endl;
64  }
65 }
66 
67 void report(std::ostream &out)
68 {
69  out << "{\"impedance\": [ " << endl;
70 
71  for (auto pair = occurances_in_failed_query.begin();
72  pair != occurances_in_failed_query.end();
73  ++pair) {
74  out << "{\"prod\": \"" << pretty_type(pair->first) << "\","
75  << "\"bad\": " << pair->second << ", "
76  << "\"ok\": " << occurances_in_ok_query[pair->first] << ", "
77  << "\"limited\": " << limited[pair->first] << ", "
78  << "\"failed\": " << failed[pair->first] << ", "
79  << "\"retries\": " << retries[pair->first] << "} ";
80 
81  if (next(pair) != occurances_in_failed_query.end())
82  out << "," << endl;
83  }
84  out << "]}" << endl;
85 }
86 
87 void retry(const char *p)
88 {
89  retries[p]++;
90 }
91 
92 void limit(const char *p)
93 {
94  limited[p]++;
95 }
96 
97 void fail(const char *p)
98 {
99  failed[p]++;
100 }
101 
102 }
feedback to the grammar about failed productions
logging
Base class for AST nodes.
Definition: prod.hh:17
virtual void accept(prod_visitor *v)
Visitor pattern for walking the AST.
Definition: prod.hh:41