perf.py 2.6 KB
Newer Older
A
air9 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#!/usr/bin/env python
# coding: utf-8

# Copyright (c) 2020 Huawei Technologies Co., Ltd.
# oec-hardware is licensed under the Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#     http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# PURPOSE.
# See the Mulan PSL v2 for more details.
# Create: 2020-04-01

import re
16
from hwcompatible.test import Test
C
cuixucui 已提交
17
from hwcompatible.command import Command
A
air9 已提交
18 19 20


class PerfTest(Test):
C
cuixucui 已提交
21 22 23
    """
    Perf Test
    """
A
air9 已提交
24 25 26
    def __init__(self):
        Test.__init__(self)
        self.requirements = ["perf"]
27 28 29
        self.perfRecord = "perf record -a -e cycles -o hwcompatible-perf.data sleep 5"
        self.perfEvlist = "perf evlist -i hwcompatible-perf.data"
        self.perfReport = "perf report -i hwcompatible-perf.data --stdio"
A
air9 已提交
30 31

    def exec_perf(self):
C
cuixucui 已提交
32 33 34 35
        """
        Execute perf command
        :return:
        """
A
air9 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
        # record
        print("Collecting the perf record using the command '%s'." % self.perfRecord)
        perfRecordEcho = Command(self.perfRecord).read()
        perfRecordMacth = re.search("perf record", perfRecordEcho)
        if not perfRecordMacth:
            print("Error: failed to record events because of :\n %s." % perfRecordEcho)
        else:
            print("Success to record events :\n %s." % perfRecordEcho)

        # evList
        perfEvlistEcho = Command(self.perfEvlist).read()
        perfEvlistdMacth = re.search("cycles", perfEvlistEcho)
        if not perfEvlistdMacth:
            print("Error: required hardware event not available because of :\n %s." % perfEvlistEcho)
            return False
        else:
            print("Hardware event found : \n %s." % perfEvlistEcho)

        # report
        perfReportEcho = Command(self.perfReport).read()
C
cuixucui 已提交
56
        perfReportMacth = re.search(r"\s*\S+\s+(\[\S+.\S+\])\s+\S+", perfReportEcho)
A
air9 已提交
57 58 59 60 61 62 63 64
        if not perfReportMacth:
            print("Error: no samples found. Failed to fetch report because of:\n %s." % perfReportEcho)
            return False
        else:
            print("Samples found for the hardware event :\n %s." % perfReportEcho)
        return True

    def test(self):
C
cuixucui 已提交
65 66 67 68
        """
        test case
        :return:
        """
A
air9 已提交
69 70 71 72
        if not self.exec_perf():
            return False
        return True

C
cuixucui 已提交
73

A
air9 已提交
74 75 76
if __name__ == "__main__":
    main = PerfTest()
    main.test()