Example #1
0
    def test_cpu_affinity_no_isolcpus(self):
        runner = self.create_runner(['-v'])

        with mock.patch('pyperf._runner.set_cpu_affinity') as mock_setaffinity:
            with mock.patch('pyperf._runner.get_isolated_cpus',
                            return_value=None):
                runner._cpu_affinity()

        self.assertFalse(runner.args.affinity)
        self.assertEqual(mock_setaffinity.call_count, 0)
Example #2
0
    def test_compare_to(self):
        def time_func(loops):
            return 1.0

        def abs_executable(python):
            return python

        run = pyperf.Run([1.5],
                         metadata={'name': 'name'},
                         collect_metadata=False)
        bench = pyperf.Benchmark([run])
        suite = pyperf.BenchmarkSuite([bench])

        with ExitStack() as cm:

            def popen(*args, **kw):
                mock_popen = mock.Mock()
                mock_popen.wait.return_value = 0
                return mock_popen

            mock_subprocess = cm.enter_context(
                mock.patch('pyperf._master.subprocess'))
            mock_subprocess.Popen.side_effect = popen

            cm.enter_context(
                mock.patch('pyperf._runner.abs_executable',
                           side_effect=abs_executable))
            cm.enter_context(
                mock.patch('pyperf._master._load_suite_from_pipe',
                           return_value=suite))

            args = [
                "--python=python1", "--compare-to=python2", "--min-time=5",
                "-p1", "-w3", "-n7", "-l11"
            ]
            runner = self.create_runner(args)
            with tests.capture_stdout():
                runner.bench_time_func('name', time_func)

            def popen_call(python):
                args = [
                    python, mock.ANY, '--worker', '--pipe', mock.ANY,
                    '--worker-task=0', '--values', '7', '--min-time', '5.0',
                    '--loops', '11', '--warmups', '3'
                ]
                kw = {}
                if MS_WINDOWS:
                    kw['close_fds'] = False
                elif six.PY3:
                    kw['pass_fds'] = mock.ANY
                return mock.call(args, env=mock.ANY, **kw)

            call1 = popen_call('python2')
            call2 = popen_call('python1')
            mock_subprocess.Popen.assert_has_calls([call1, call2])
Example #3
0
    def test_cpu_affinity_isolcpus(self):
        runner = self.create_runner(['-v'])

        with mock.patch('pyperf._runner.set_cpu_affinity') as mock_setaffinity:
            with mock.patch('pyperf._runner.get_isolated_cpus',
                            return_value=[1, 2]):
                with tests.capture_stdout() as stdout:
                    runner._cpu_affinity()

        self.assertEqual(runner.args.affinity, '1-2')
        self.assertEqual(stdout.getvalue(),
                         'Pin process to isolated CPUs: 1-2\n')
        mock_setaffinity.assert_called_once_with([1, 2])
Example #4
0
    def exec_runner(self, *args, **kwargs):
        def fake_timer():
            t = fake_timer.value
            fake_timer.value += 1.0
            return t

        fake_timer.value = 0.0

        name = kwargs.pop('name', 'bench')
        time_func = kwargs.pop('time_func', None)

        runner = self.create_runner(args, **kwargs)

        with mock.patch('pyperf.perf_counter', fake_timer):
            with tests.capture_stdout() as stdout:
                with tests.capture_stderr() as stderr:
                    if time_func:
                        bench = runner.bench_time_func(name, time_func)
                    else:
                        bench = runner.bench_func(name, check_args, None, 1, 2)

        stdout = stdout.getvalue()
        stderr = stderr.getvalue()
        if '--stdout' not in args:
            self.assertEqual(stderr, '')

        # check bench_time_func() bench
        self.assertIsInstance(bench, pyperf.Benchmark)
        self.assertEqual(bench.get_name(), name)
        self.assertEqual(bench.get_nrun(), 1)

        return Result(runner, bench, stdout)
Example #5
0
    def test_cpu_affinity_args(self):
        runner = self.create_runner(['-v', '--affinity=3,7'])

        with mock.patch('pyperf._runner.set_cpu_affinity') as mock_setaffinity:
            with tests.capture_stdout() as stdout:
                runner._cpu_affinity()

        self.assertEqual(runner.args.affinity, '3,7')
        self.assertEqual(stdout.getvalue(), 'Pin process to CPUs: 3,7\n')
        mock_setaffinity.assert_called_once_with([3, 7])
Example #6
0
    def test_cpu_config(self):
        nohz_full = '2-3\n'

        def mock_open(filename, *args, **kw):
            filename = filename.replace('\\', '/')
            if filename == '/sys/devices/system/cpu/cpu0/cpufreq/scaling_driver':
                data = 'DRIVER\n'
            elif filename == '/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor':
                data = 'GOVERNOR\n'
            elif filename.startswith('/sys/devices/system/cpu/nohz_full'):
                data = nohz_full
            elif filename.startswith('/sys/devices/system/cpu/cpu2'):
                raise IOError
            elif filename == '/sys/devices/system/cpu/cpuidle/current_driver':
                data = 'IDLE_DRV\n'
            else:
                raise ValueError("unexpect open: %r" % filename)
            return six.StringIO(data)

        with mock.patch('pyperf._collect_metadata.get_isolated_cpus',
                        return_value=[2]):
            with mock.patch('pyperf._utils.open',
                            create=True,
                            side_effect=mock_open):
                metadata = {}
                perf_metadata.collect_cpu_config(metadata, [0, 2])
        self.assertEqual(
            metadata['cpu_config'], '0=driver:DRIVER, governor:GOVERNOR; '
            '2=nohz_full, isolated; '
            'idle:IDLE_DRV')

        nohz_full = '  (null)\n'
        with mock.patch('pyperf._collect_metadata.get_isolated_cpus'):
            with mock.patch('pyperf._utils.open',
                            create=True,
                            side_effect=mock_open):
                metadata = {}
                perf_metadata.collect_cpu_config(metadata, [0, 2])
        self.assertEqual(
            metadata['cpu_config'], '0=driver:DRIVER, governor:GOVERNOR; '
            'idle:IDLE_DRV')
Example #7
0
    def test_power8_cpu_frequencies(self):
        def mock_open(filename, *args, **kw):
            filename = filename.replace('\\', '/')
            if filename == '/proc/cpuinfo':
                data = self.POWER8_CPUINFO
            else:
                raise ValueError("unexpect open: %r" % filename)
            return six.StringIO(data)

        with mock.patch('pyperf._utils.open', create=True, side_effect=mock_open):
            metadata = {}
            perf_metadata.collect_cpu_freq(metadata, [0, 159])
            perf_metadata.collect_cpu_model(metadata)
            self.assertEqual(metadata['cpu_freq'],
                             '0,159=3425 MHz')
            self.assertEqual(metadata['cpu_machine'],
                             'PowerNV 8247-22L')
Example #8
0
    def test_get_isolated_cpus(self):
        BUILTIN_OPEN = 'builtins.open' if six.PY3 else '__builtin__.open'

        def check_get(line):
            with mock.patch(BUILTIN_OPEN) as mock_open:
                mock_file = mock_open.return_value
                mock_file.readline.return_value = line
                return cpu_utils.get_isolated_cpus()

        # no isolated CPU
        self.assertIsNone(check_get(''))

        # isolated CPUs
        self.assertEqual(check_get('1-2'), [1, 2])

        # /sys/devices/system/cpu/isolated doesn't exist (ex: Windows)
        with mock.patch(BUILTIN_OPEN, side_effect=IOError):
            self.assertIsNone(cpu_utils.get_isolated_cpus())
Example #9
0
    def test_intel_cpu_frequencies(self):
        def mock_open(filename, *args, **kw):
            filename = filename.replace('\\', '/')
            if filename == '/proc/cpuinfo':
                data = self.INTEL_CPU_INFO
            elif filename == '/sys/devices/system/cpu/cpu0/cpufreq/scaling_driver':
                data = 'DRIVER\n'
            elif filename == '/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor':
                data = 'GOVERNOR\n'
            elif filename.startswith('/sys/devices/system/cpu/cpu2'):
                raise IOError
            else:
                raise ValueError("unexpect open: %r" % filename)
            return six.StringIO(data)

        with mock.patch('pyperf._utils.open', create=True, side_effect=mock_open):
            metadata = {}
            perf_metadata.collect_cpu_freq(metadata, [0, 2])
            perf_metadata.collect_cpu_model(metadata)
            self.assertEqual(metadata['cpu_freq'],
                             '0=1288 MHz; 2=1200 MHz')
            self.assertEqual(metadata['cpu_model_name'],
                             'Intel(R) Core(TM) i7-3520M CPU @ 2.90GHz')
Example #10
0
 def check_get(line):
     with mock.patch(BUILTIN_OPEN) as mock_open:
         mock_file = mock_open.return_value
         mock_file.readline.return_value = line
         return cpu_utils.get_isolated_cpus()