validate_benchout.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/python3
  2. # Copyright (C) 2014-2026 Free Software Foundation, Inc.
  3. # This file is part of the GNU C Library.
  4. #
  5. # The GNU C Library is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU Lesser General Public
  7. # License as published by the Free Software Foundation; either
  8. # version 2.1 of the License, or (at your option) any later version.
  9. #
  10. # The GNU C Library is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. # Lesser General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Lesser General Public
  16. # License along with the GNU C Library; if not, see
  17. # <https://www.gnu.org/licenses/>.
  18. """Benchmark output validator
  19. Given a benchmark output file in json format and a benchmark schema file,
  20. validate the output against the schema.
  21. """
  22. from __future__ import print_function
  23. import json
  24. import sys
  25. import os
  26. try:
  27. import import_bench as bench
  28. except ImportError:
  29. print('Import Error: Output will not be validated.')
  30. # Return success because we don't want the bench target to fail just
  31. # because the jsonschema module was not found.
  32. sys.exit(os.EX_OK)
  33. def print_and_exit(message, exitcode):
  34. """Prints message to stderr and returns the exit code.
  35. Args:
  36. message: The message to print
  37. exitcode: The exit code to return
  38. Returns:
  39. The passed exit code
  40. """
  41. print(message, file=sys.stderr)
  42. return exitcode
  43. def main(args):
  44. """Main entry point
  45. Args:
  46. args: The command line arguments to the program
  47. Returns:
  48. 0 on success or a non-zero failure code
  49. Exceptions:
  50. Exceptions thrown by validate_bench
  51. """
  52. if len(args) != 2:
  53. return print_and_exit("Usage: %s <bench.out file> <bench.out schema>"
  54. % sys.argv[0], os.EX_USAGE)
  55. try:
  56. bench.parse_bench(args[0], args[1])
  57. except IOError as e:
  58. return print_and_exit("IOError(%d): %s" % (e.errno, e.strerror),
  59. os.EX_OSFILE)
  60. except bench.validator.ValidationError as e:
  61. return print_and_exit("Invalid benchmark output: %s" % e.message,
  62. os.EX_DATAERR)
  63. except bench.validator.SchemaError as e:
  64. return print_and_exit("Invalid schema: %s" % e.message, os.EX_DATAERR)
  65. except json.decoder.JSONDecodeError as e:
  66. return print_and_exit("Benchmark output in %s is not JSON." % args[0],
  67. os.EX_DATAERR)
  68. print("Benchmark output in %s is valid." % args[0])
  69. return os.EX_OK
  70. if __name__ == '__main__':
  71. sys.exit(main(sys.argv[1:]))