test_benchmark_cli.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. """Tests of grass.benchmark CLI"""
  13. import sys
  14. from pathlib import Path
  15. from subprocess import DEVNULL
  16. import grass
  17. from grass.benchmark import (
  18. benchmark_nprocs,
  19. benchmark_resolutions,
  20. save_results_to_file,
  21. )
  22. from grass.benchmark.app import main as benchmark_main
  23. from grass.gunittest.case import TestCase
  24. from grass.gunittest.main import test
  25. from grass.pygrass.modules import Module
  26. def remove_file(path):
  27. """Remove filename if exists"""
  28. if sys.version_info < (3, 8):
  29. try:
  30. Path(path).unlink()
  31. except FileNotFoundError:
  32. pass
  33. else:
  34. Path(path).unlink(missing_ok=True)
  35. class TestBenchmarkCLI(TestCase):
  36. """Tests that benchmarkin CLI works"""
  37. json_filename = "plot_test.json"
  38. png_filename1 = "plot_test1.png"
  39. png_filename2 = "plot_test2.png"
  40. def tearDown(self):
  41. """Remove test files"""
  42. remove_file(self.json_filename)
  43. remove_file(self.png_filename1)
  44. remove_file(self.png_filename2)
  45. def test_plot_nprocs_workflow(self):
  46. """Test that plot nprocs workflow runs"""
  47. label = "Standard output"
  48. repeat = 4
  49. # The benchmark part has only Python API, not CLI.
  50. try:
  51. result = benchmark_nprocs(
  52. module=Module("r.univar", map="elevation", stdout_=DEVNULL, run_=False),
  53. label=label,
  54. repeat=repeat,
  55. max_nprocs=3,
  56. )
  57. except grass.exceptions.ParameterError:
  58. self.skipTest("r.univar without nprocs parameter")
  59. save_results_to_file([result], self.json_filename)
  60. benchmark_main(["plot", "nprocs", self.json_filename, self.png_filename1])
  61. self.assertTrue(Path(self.png_filename1).is_file())
  62. def test_plot_cells_workflow(self):
  63. """Test that plot cells workflow runs"""
  64. label = "Standard output"
  65. repeat = 4
  66. # The benchmark part has only Python API, not CLI.
  67. result = benchmark_resolutions(
  68. module=Module("r.univar", map="elevation", stdout_=DEVNULL, run_=False),
  69. label=label,
  70. repeat=repeat,
  71. resolutions=[1000, 500],
  72. )
  73. save_results_to_file([result], self.json_filename)
  74. benchmark_main(["plot", "cells", self.json_filename, self.png_filename1])
  75. self.assertTrue(Path(self.png_filename1).is_file())
  76. benchmark_main(
  77. ["plot", "cells", "--resolutions", self.json_filename, self.png_filename2]
  78. )
  79. self.assertTrue(Path(self.png_filename2).is_file())
  80. if __name__ == "__main__":
  81. test()