-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest_benchmark_utils.py
77 lines (60 loc) · 2.35 KB
/
test_benchmark_utils.py
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
# Copyright (c) Meta Platforms, Inc. and affiliates.
# pyre-unsafe
import sys
import unittest
from unittest.mock import MagicMock, patch
from ft_utils.benchmark_utils import (
benchmark_operation,
BenchmarkProvider,
execute_benchmarks,
ft_randchoice,
ft_randint,
parse_arguments,
worker,
)
class FakeBench(BenchmarkProvider):
def benchmark_foo(self):
self.__class__.ran = True
class TestBenchmarkUtils(unittest.TestCase):
def test_ft_randint(self):
results = {ft_randint(1, 10) for _ in range(100)}
self.assertTrue(all(1 <= num <= 10 for num in results))
self.assertTrue(len(results) > 1)
def test_ft_randint_reversed(self):
results = {ft_randint(10, 1) for _ in range(100)}
self.assertTrue(all(1 <= num <= 10 for num in results))
def test_ft_randchoice(self):
seq = ["apple", "banana", "cherry"]
results = {ft_randchoice(seq) for _ in range(100)}
self.assertEqual(set(seq), results)
def test_ft_randchoice_empty(self):
with self.assertRaises(IndexError):
ft_randchoice([])
def test_benchmark_provider_init(self):
provider = BenchmarkProvider(100)
self.assertEqual(provider._operations, 100)
@patch("argparse.ArgumentParser.parse_args")
def test_parse_arguments(self, mock_parse_args):
mock_parse_args.return_value = MagicMock(operations=1000, threads=16)
args = parse_arguments("Test")
self.assertEqual(args.operations, 1000)
self.assertEqual(args.threads, 16)
@patch("time.time", side_effect=[1, 2])
def test_benchmark_operation(self, mock_time):
barrier = MagicMock()
barrier.wait = MagicMock()
result = benchmark_operation(lambda: None)
self.assertEqual(result, 1)
@patch("ft_utils.benchmark_utils.benchmark_operation", return_value=1.0)
def test_worker(self, mock_benchmark_operation):
barrier = MagicMock()
results = worker(lambda: None, barrier)
self.assertEqual(results, [1.0] * 5)
def test_discovery(self):
test_args = ["test_benchmark_utils", "--threads", "1", "--operations", "1"]
with patch.object(sys, "argv", test_args):
FakeBench.ran = False
execute_benchmarks(FakeBench)
self.assertTrue(FakeBench.ran)
if __name__ == "__main__":
unittest.main()