test_benchmark.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. join_results,
  19. load_results,
  20. load_results_from_file,
  21. num_cells_plot,
  22. save_results,
  23. save_results_to_file,
  24. )
  25. from grass.gunittest.case import TestCase
  26. from grass.gunittest.main import test
  27. from grass.pygrass.modules import Module
  28. class TestBenchmarksRun(TestCase):
  29. """Tests that functions for benchmarking can run"""
  30. def test_resolutions(self):
  31. """Test that resolution tests runs without nprocs and plots to file"""
  32. benchmarks = [
  33. dict(
  34. module=Module("r.univar", map="elevation", stdout_=DEVNULL, run_=False),
  35. label="Standard output",
  36. ),
  37. dict(
  38. module=Module(
  39. "r.univar", map="elevation", flags="g", stdout_=DEVNULL, run_=False
  40. ),
  41. label="Standard output",
  42. ),
  43. ]
  44. resolutions = [300, 200, 100]
  45. results = []
  46. for benchmark in benchmarks:
  47. results.append(
  48. benchmark_resolutions(
  49. **benchmark,
  50. resolutions=resolutions,
  51. )
  52. )
  53. plot_file = "test_res_plot.png"
  54. num_cells_plot(results, filename=plot_file)
  55. self.assertTrue(Path(plot_file).is_file())
  56. class TestBenchmarkResults(TestCase):
  57. """Tests that saving results work"""
  58. def test_save_load(self):
  59. """Test that results can be saved and loaded"""
  60. resolutions = [300, 200]
  61. results = [
  62. benchmark_resolutions(
  63. module=Module(
  64. "r.univar",
  65. map="elevation",
  66. stdout_=DEVNULL,
  67. stderr_=DEVNULL,
  68. run_=False,
  69. ),
  70. label="Standard output",
  71. resolutions=resolutions,
  72. )
  73. ]
  74. results = load_results(save_results(results))
  75. plot_file = "test_res_plot.png"
  76. num_cells_plot(results.results, filename=plot_file)
  77. self.assertTrue(Path(plot_file).is_file())
  78. def test_data_file_roundtrip(self):
  79. """Test functions can save and load to a file"""
  80. original = [SimpleNamespace(nprocs=[1, 2, 3], times=[3, 2, 1], label="Test 1")]
  81. filename = "test_res_file.json"
  82. save_results_to_file(original, filename)
  83. self.assertTrue(Path(filename).is_file())
  84. loaded = load_results_from_file(filename).results
  85. self.assertEqual(original, loaded)
  86. def test_join_results_list(self):
  87. """Test that we can join lists"""
  88. list_1 = [
  89. SimpleNamespace(nprocs=[1, 2, 3], times=[3, 2, 1], label="Test 1"),
  90. SimpleNamespace(nprocs=[1, 2, 3], times=[3, 2, 1], label="Test 2"),
  91. ]
  92. list_2 = [SimpleNamespace(nprocs=[1, 2, 3], times=[3, 2, 1], label="Test 3")]
  93. new_results = join_results([list_1, list_2])
  94. self.assertEqual(len(new_results), 3)
  95. def test_join_results_structure(self):
  96. """Test that we can join a full results structure"""
  97. list_1 = SimpleNamespace(
  98. results=[
  99. SimpleNamespace(nprocs=[1, 2, 3], times=[3, 2, 1], label="Test 1"),
  100. SimpleNamespace(nprocs=[1, 2, 3], times=[3, 2, 1], label="Test 2"),
  101. ]
  102. )
  103. list_2 = [SimpleNamespace(nprocs=[1, 2, 3], times=[3, 2, 1], label="Test 3")]
  104. new_results = join_results([list_1, list_2])
  105. self.assertEqual(len(new_results), 3)
  106. if __name__ == "__main__":
  107. test()