summaryrefslogtreecommitdiffstats
path: root/scripts/getstats1.py
blob: 8ed1cbca2a7f3f21925de6d0fedd07f95ab457d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
from dbaccess import execQuery, database
from singlecontextbmstats import (
    SingleContextBMStats, extractSingleContextBMStats)
from misc import idToText, textToId, benchmarkToComponents, printJSONHeader,
     getContext

class GetStats1:

    def __init__(self, host, platform, branch, sha1, test_case_filter):
        self.host = host
        self.platform = platform
        self.branch = branch
        self.sha1 = sha1
        self.test_case_filter = test_case_filter
        self.host_id = textToId("host", host)
        self.platform_id = textToId("platform", platform)
        self.branch_id = textToId("branch", branch)
        self.sha1_id = textToId("sha1", sha1)

    def execute(self):
        # self.overall_stats = self.computeOverallStats()
        self.bmstats_list = self.computeBMStatsList()
        self.writeOutput()

    # ### THE COMMENTED-OUT CODE BELOW NEEDS TO BE ADAPTED TO THE
    # ### CURRENT DATABASE SCHEMA!

    # Computes and returns a dictionary of overall statistics.
    # def computeOverallStats(self):

    #     stats = {}

    #     part1 = "SELECT count(*) FROM (SELECT DISTINCT "
    #     part3 = (
    #         " FROM result WHERE host = '" + self.host +
    #         "' AND platform = '" + self.platform +
    #         "' AND branch = '" + self.branch +
    #         "' AND sha1 = '" + self.sha1 + "' ")
    #     part4 = (") AS dummy;")

    #     # Distinct test cases:
    #     query_result = execQuery(part1 + "testCase" + part3 + part4)
    #     stats['tc_combs'] = query_result[0][0]

    #     # Distinct (test case, test function) combinations:
    #     query_result = execQuery(
    #         part1 + "testCase, testFunction" + part3 + part4)
    #     stats['tc_tf_combs'] = query_result[0][0]

    #     # Distinct (test case, test function, data tag) combinations:
    #     query_result = execQuery(
    #         part1 + "testCase, testFunction, dataTag" + part3 + part4)
    #     stats['tc_tf_dt_combs'] = query_result[0][0]

    #     # (test case, test function, data tag) combinations with at least
    #     # one failed value
    #     query_result = execQuery(
    #         part1 + "testCase, testFunction, dataTag" + part3 +
    #         "AND valid = false" + part4)
    #     stats['tc_tf_dt_failed'] = query_result[0][0]

    #     return stats

    # Computes per-benchmark statistics. Returns an n-tuple of
    # SingleContextBMStats objects.
    def computeBMStatsList(self):

        # Get the context
        context = getContext(self.host_id, self.platform_id,
                self.branch_id, self.sha1_id)

        # Get all distinct benchmark/metric combinations matching the context:
        bmark_metrics = execQuery("SELECT DISTINCT benchmarkId, metricId"
            " FROM result WHERE contextId = %d;" % context)

        bmstats_list = []

        # Loop over benchmarks and compute stats:
        for benchmark_id, metric_id in bmark_metrics:

            benchmark = idToText("benchmark", benchmark_id)
            test_case, test_function, data_tag = (
                benchmarkToComponents(benchmark))
            if ((self.test_case_filter != None)
                and (not test_case in self.test_case_filter)):
                continue

            bmstats = extractSingleContextBMStats(
                self.host_id, self.platform_id, self.branch_id, self.sha1_id,
                benchmark_id, metric_id)

            bmstats_list.append(bmstats)

        return tuple(bmstats_list)

    def writeOutputAsJSON(self):
        printJSONHeader()
        print "{"

        # Context:
        print "\"database\": \"" + database() + "\", "
        print "\"host\": \"" + self.host + "\", "
        print "\"platform\": \"" + self.platform + "\", "
        print "\"branch\": \"" + self.branch + "\", "
        print "\"sha1\": \"" + self.sha1 + "\""

        # Overall stats:
        # print ", \"tc_combs\": " + str(self.overall_stats['tc_combs'])
        # print(
        #     ", \"tc_tf_combs\": " + str(self.overall_stats['tc_tf_combs']))
        # print(
        #     ", \"tc_tf_dt_combs\": " +
        #     str(self.overall_stats['tc_tf_dt_combs']))
        # print(
        #     ", \"tc_tf_dt_failed\": " +
        #     str(self.overall_stats['tc_tf_dt_failed']))
        # ### 4 NOW:
        print ", \"tc_combs\": \"-1\""
        print ", \"tc_tf_combs\": \"-1\""
        print ", \"tc_tf_dt_combs\": \"-1\""
        print ", \"tc_tf_dt_failed\": \"-1\""

        # Per-benchmark stats:

        print ", \"per_bm_stats\": ["

        first_row = True
        for bmstats in self.bmstats_list:
            if not first_row:
                print ","
            first_row = False

            s_ntotal = str(bmstats.ntotal)
            s_nvalid = str(bmstats.nvalid)
            s_min = s_max = s_median = s_mean = s_stddev = s_rsd = s_rse = ""

            if bmstats.nvalid > 0:
                s_min = str(bmstats.min_)
                s_max = str(bmstats.max_)
                s_median = str(bmstats.median)
                s_mean = str(bmstats.mean)
                if bmstats.nvalid > 1:
                    s_stddev = str(bmstats.stddev)
                    if not bmstats.mean_is_zero:
                        s_rsd = "{0:.2f}".format(bmstats.rsd)
                        s_rse = "{0:.2f}".format(bmstats.rse)

            s_metric = idToText("metric", bmstats.metric_id)
            benchmark = idToText("benchmark", bmstats.benchmark_id)
            s_test_case, s_test_function, s_data_tag = (
                benchmarkToComponents(benchmark))

            print "["
            print "\"" + s_ntotal + "\", "
            print "\"" + s_nvalid + "\", "
            print "\"" + s_min + "\", "
            print "\"" + s_max + "\", "
            print "\"" + s_median + "\", "
            print "\"" + s_mean + "\", "
            print "\"" + s_stddev + "\", "
            print "\"" + s_rsd + "\", "
            print "\"" + s_rse + "\", "

            for binval in bmstats.hist:
                print "\"{0:.0f}\", ".format(binval)

            print "\"" + s_metric + "\", "
            print (
                "\"" + s_test_case + ":" + s_test_function + "(" +
                s_data_tag + ")\"")

            print "]"

        print "]"

        print "}"

class GetStats1AsJSON(GetStats1):
    def writeOutput(self):
        self.writeOutputAsJSON()