Completed
Pull Request — master (#58)
by
unknown
01:18
created

ElasticReportBackend.handle_loading()   B

Complexity

Conditions 5

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 5
c 3
b 0
f 0
dl 0
loc 23
rs 8.2508
1
from .base_report_backend import BaseReportBackend
2
from ..elasticsearch_storage import ElasticsearchStorage
3
4
5
class ElasticReportBackend(BaseReportBackend):
6
    def __init__(self, config):
7
        self.elasticsearch_host = config.getoption("benchmark_elasticsearch_host")
8
        self.elasticsearch_index = config.getoption("benchmark_elasticsearch_index")
9
        self.elasticsearch_doctype = config.getoption("benchmark_elasticsearch_doctype")
10
        self.project_name = config.getoption("benchmark_project")
11
        super(ElasticReportBackend, self).__init__(config)
12
        self.storage = ElasticsearchStorage(self.elasticsearch_host,
13
                                            self.elasticsearch_index,
14
                                            self.elasticsearch_doctype,
15
                                            self.logger,
16
                                            default_machine_id=self.machine_id)
17
18
    def handle_saving(self, benchmarks, machine_info):
19
        save = benchmarks and self.save or self.autosave
20
        if save:
21
            commit_info = self.config.hook.pytest_benchmark_generate_commit_info(config=self.config)
22
            self.config.hook.pytest_benchmark_update_commit_info(config=self.config, commit_info=commit_info)
23
24
            output_json = self.config.hook.pytest_benchmark_generate_json(
25
                config=self.config,
26
                benchmarks=benchmarks,
27
                include_data=self.save_data,
28
                machine_info=machine_info,
29
                commit_info=commit_info,
30
            )
31
            self.config.hook.pytest_benchmark_update_json(
32
                config=self.config,
33
                benchmarks=benchmarks,
34
                output_json=output_json,
35
            )
36
            output_benchmarks = output_json.pop("benchmarks")
37
            for bench in output_benchmarks:
38
                # add top level info from output_json dict to each record
39
                bench.update(output_json)
40
                doc_id = "%s_%s" % (save, bench["fullname"])
41
                self.storage.save(bench, doc_id)
42
            self.logger.info("Saved benchmark data to %s to index %s as doctype %s" %
43
                             (
44
                                 self.elasticsearch_host,
45
                                 self.elasticsearch_index,
46
                                 self.elasticsearch_doctype
47
                             )
48
                             )
49
50
    def handle_loading(self, machine_info):
51
        compared_mapping = {}
52
        if self.compare:
53
            compared_benchmarks = list(self.storage.load(self.project_name))[-1:]
54
55
            if not compared_benchmarks:
56
                msg = "Can't compare. No benchmark records in project %s in elastic %s." % (self.project_name, self.storage.location)
57
                code = "BENCHMARK-C1"
58
                self.logger.warn(code, msg, fslocation=self.storage.location)
59
60
            for commit_time, compared_benchmark in compared_benchmarks:
61
                self.config.hook.pytest_benchmark_compare_machine_info(
62
                    config=self.config,
63
                    benchmarksession=self,
64
                    machine_info=machine_info,
65
                    compared_benchmark=compared_benchmark,
66
                )
67
                compared_mapping[commit_time] = dict(
68
                    (bench['fullname'], bench) for bench in compared_benchmark['benchmarks']
69
                )
70
                self.logger.info("Comparing against benchmarks from: %s" % commit_time)
71
72
        return compared_mapping
73