<commit_before>import time
import mock
import json
import tempfile
import unittest
from pathlib import Path

from cumulusci.tasks.robotframework import robotperflistener


class TestRobotPerfListener(unittest.TestCase):
    def start_listener(self, verbosity=0):
        """Helper function that does setup to the point of starting a test"""
        task = mock.MagicMock()
        self.rpl = robotperflistener.RobotPerfListener(task, verbosity)
        self.rpl.start_suite("fake_suite")
        self.rpl.start_test("fake_test")
        return self.rpl

    def finish_listener(self):
        """Helper function that does teardown and returns json"""
        self.rpl.end_test("fake_test")
        self.rpl.end_suite("fake_suite")
        with tempfile.TemporaryDirectory() as temporary:
            tempdir = Path(temporary)
            self.rpl.output_file(tempdir / "junk.xml")
            self.rpl.close()
            return json.load(open(tempdir / "perf.json"))

    def test_lifecyle(self):
        """Test the basics"""
        self.start_listener()
        json = self.finish_listener()
        assert json

    def test_metric_totalling(self):
        rpl = self.start_listener()
        rpl.create_aggregate_metric("my summation", "sum")
        rpl.store_metric_value("my summation", 5)
        rpl.store_metric_value("my summation", 15)
        rpl.store_metric_value("my summation", 25)
        json = self.finish_listener()
        assert json["fake_suite"]["fake_test"]["totals"]["my summation"] == 45

    def test_metric_averaging(self):
        rpl = self.start_listener()
        rpl.create_aggregate_metric("my mean", "average")
        rpl.store_metric_value("my mean", 7)
        rpl.store_metric_value("my mean", 10)
        rpl.store_metric_value("my mean", 13)
        json = self.finish_listener()
        assert json["fake_suite"]["fake_test"]["totals"]["my mean"] == 10

    def test_metric_duration(self):
        rpl = self.start_listener()
        rpl.create_duration_metric("my duration")
        time.sleep(0.25)
        rpl.end_duration_metric("my duration")<commit_msg>Python 2 compatibility.<commit_after>import os
import time
import mock
import json
import tempfile
import unittest

from cumulusci.tasks.robotframework import robotperflistener


class TestRobotPerfListener(unittest.TestCase):
    def start_listener(self, verbosity=0):
        """Helper function that does setup to the point of starting a test"""
        task = mock.MagicMock()
        self.rpl = robotperflistener.RobotPerfListener(task, verbosity)
        self.rpl.start_suite("fake_suite")
        self.rpl.start_test("fake_test")
        return self.rpl

    def finish_listener(self):
        """Helper function that does teardown and returns json"""
        self.rpl.end_test("fake_test")
        self.rpl.end_suite("fake_suite")
        with tempfile.TemporaryDirectory() as tempdir:
            self.rpl.output_file(os.path.join(tempdir, "junk.xml"))
            self.rpl.close()
            return json.load(open(os.path.join(tempdir, "perf.json")))

    def test_lifecyle(self):
        """Test the basics"""
        self.start_listener()
        json = self.finish_listener()
        assert json

    def test_metric_totalling(self):
        rpl = self.start_listener()
        rpl.create_aggregate_metric("my summation", "sum")
        rpl.store_metric_value("my summation", 5)
        rpl.store_metric_value("my summation", 15)
        rpl.store_metric_value("my summation", 25)
        json = self.finish_listener()
        assert json["fake_suite"]["fake_test"]["totals"]["my summation"] == 45

    def test_metric_averaging(self):
        rpl = self.start_listener()
        rpl.create_aggregate_metric("my mean", "average")
        rpl.store_metric_value("my mean", 7)
        rpl.store_metric_value("my mean", 10)
        rpl.store_metric_value("my mean", 13)
        json = self.finish_listener()
        assert json["fake_suite"]["fake_test"]["totals"]["my mean"] == 10

    def test_metric_duration(self):
        rpl = self.start_listener()
        rpl.create_duration_metric("my duration")
        time.sleep(0.25)
        rpl.end_duration_metric("my duration")
{'fraction_non_alphanumeric': 0.11548556430446194, 'fraction_numerical': 0.00882844189930804, 'mean_word_length': 3.058083252662149, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3749294', 'n_tokens_mistral': 1328, 'n_tokens_neox': 1277, 'n_words': 259}