test_benchmark.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. # MODULE: Test of grass.benchmark
  2. #
  3. # AUTHOR(S): Vaclav Petras <wenzeslaus gmail com>
  4. #
  5. # PURPOSE: Benchmarking for GRASS GIS modules
  6. #
  7. # COPYRIGHT: (C) 2021 Vaclav Petras, and by the GRASS Development Team
  8. #
  9. # This program is free software under the GNU General Public
  10. # License (>=v2). Read the file COPYING that comes with GRASS
  11. # for details.
  12. """Basic tests of grass.benchmark"""
  13. from pathlib import Path
  14. from subprocess import DEVNULL
  15. from types import SimpleNamespace
  16. from grass.benchmark import (
  17. benchmark_resolutions,
  18. benchmark_single,
  19. join_results,
  20. load_results,
  21. load_results_from_file,
  22. num_cells_plot,
  23. save_results,
  24. save_results_to_file,
  25. )
  26. from grass.gunittest.case import TestCase
  27. from grass.gunittest.main import test
  28. from grass.pygrass.modules import Module
  29. class TestBenchmarksRun(TestCase):
  30. """Tests that functions for benchmarking can run"""
  31. def test_resolutions(self):
  32. """Test that resolution tests runs without nprocs and plots to file"""
  33. benchmarks = [
  34. dict(
  35. module=Module("r.univar", map="elevation", stdout_=DEVNULL, run_=False),
  36. label="Standard output",
  37. ),
  38. dict(
  39. module=Module(
  40. "r.univar", map="elevation", flags="g", stdout_=DEVNULL, run_=False
  41. ),
  42. label="Standard output",
  43. ),
  44. ]
  45. resolutions = [300, 200, 100]
  46. results = []
  47. for benchmark in benchmarks:
  48. results.append(
  49. benchmark_resolutions(
  50. **benchmark,
  51. resolutions=resolutions,
  52. )
  53. )
  54. plot_file = "test_res_plot.png"
  55. num_cells_plot(results, filename=plot_file)
  56. self.assertTrue(Path(plot_file).is_file())
  57. def test_single(self):
  58. """Test that single benchmark function runs"""
  59. label = "Standard output"
  60. repeat = 4
  61. benchmarks = [
  62. dict(
  63. module=Module("r.univar", map="elevation", stdout_=DEVNULL, run_=False),
  64. label=label,
  65. )
  66. ]
  67. results = []
  68. for benchmark in benchmarks:
  69. results.append(benchmark_single(**benchmark, repeat=repeat))
  70. self.assertEqual(len(results), len(benchmarks))
  71. for result in results:
  72. self.assertTrue(hasattr(result, "all_times"))
  73. self.assertTrue(hasattr(result, "time"))
  74. self.assertTrue(hasattr(result, "label"))
  75. self.assertEqual(len(result.all_times), repeat)
  76. self.assertEqual(results[0].label, label)
  77. class TestBenchmarkResults(TestCase):
  78. """Tests that saving results work"""
  79. def test_save_load(self):
  80. """Test that results can be saved and loaded"""
  81. resolutions = [300, 200]
  82. results = [
  83. benchmark_resolutions(
  84. module=Module(
  85. "r.univar",
  86. map="elevation",
  87. stdout_=DEVNULL,
  88. stderr_=DEVNULL,
  89. run_=False,
  90. ),
  91. label="Standard output",
  92. resolutions=resolutions,
  93. )
  94. ]
  95. results = load_results(save_results(results))
  96. plot_file = "test_res_plot.png"
  97. num_cells_plot(results.results, filename=plot_file)
  98. self.assertTrue(Path(plot_file).is_file())
  99. def test_data_file_roundtrip(self):
  100. """Test functions can save and load to a file"""
  101. original = [SimpleNamespace(nprocs=[1, 2, 3], times=[3, 2, 1], label="Test 1")]
  102. filename = "test_res_file.json"
  103. save_results_to_file(original, filename)
  104. self.assertTrue(Path(filename).is_file())
  105. loaded = load_results_from_file(filename).results
  106. self.assertEqual(original, loaded)
  107. def test_join_results_list(self):
  108. """Test that we can join lists"""
  109. list_1 = [
  110. SimpleNamespace(nprocs=[1, 2, 3], times=[3, 2, 1], label="Test 1"),
  111. SimpleNamespace(nprocs=[1, 2, 3], times=[3, 2, 1], label="Test 2"),
  112. ]
  113. list_2 = [SimpleNamespace(nprocs=[1, 2, 3], times=[3, 2, 1], label="Test 3")]
  114. new_results = join_results([list_1, list_2])
  115. self.assertEqual(len(new_results), 3)
  116. def test_join_results_structure(self):
  117. """Test that we can join a full results structure"""
  118. list_1 = SimpleNamespace(
  119. results=[
  120. SimpleNamespace(nprocs=[1, 2, 3], times=[3, 2, 1], label="Test 1"),
  121. SimpleNamespace(nprocs=[1, 2, 3], times=[3, 2, 1], label="Test 2"),
  122. ]
  123. )
  124. list_2 = [SimpleNamespace(nprocs=[1, 2, 3], times=[3, 2, 1], label="Test 3")]
  125. new_results = join_results([list_1, list_2])
  126. self.assertEqual(len(new_results), 3)
  127. if __name__ == "__main__":
  128. test()