mysql.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import json
  2. import MySQLdb
  3. import traceback
  4. from colorama import Fore
  5. from toolset.utils.output_helper import log
  6. from toolset.databases.abstract_database import AbstractDatabase
  7. class Database(AbstractDatabase):
  8. margin = 1.015
  9. @classmethod
  10. def get_connection(cls, config):
  11. return MySQLdb.connect(config.database_host, "benchmarkdbuser",
  12. "benchmarkdbpass", "hello_world")
  13. @classmethod
  14. def get_current_world_table(cls, config):
  15. results_json = []
  16. try:
  17. db = cls.get_connection(config)
  18. cursor = db.cursor()
  19. cursor.execute("SELECT * FROM World")
  20. results = cursor.fetchall()
  21. results_json.append(json.loads(json.dumps(dict(results))))
  22. db.close()
  23. except Exception:
  24. tb = traceback.format_exc()
  25. log("ERROR: Unable to load current MySQL World table.",
  26. color=Fore.RED)
  27. log(tb)
  28. return results_json
  29. @classmethod
  30. def test_connection(cls, config):
  31. try:
  32. db = cls.get_connection(config)
  33. cursor = db.cursor()
  34. cursor.execute("SELECT 1")
  35. cursor.fetchall()
  36. db.close()
  37. return True
  38. except:
  39. return False
  40. @classmethod
  41. def get_queries(cls, config):
  42. db = cls.get_connection(config)
  43. cursor = db.cursor()
  44. cursor.execute("Show global status where Variable_name in ('Com_select','Com_update')")
  45. res = 0
  46. records = cursor.fetchall()
  47. for row in records:
  48. res = res + int(row[1])
  49. return res
  50. @classmethod
  51. def get_rows(cls, config):
  52. db = cls.get_connection(config)
  53. cursor = db.cursor()
  54. cursor.execute("""SELECT r.variable_value-u.variable_value FROM
  55. (SELECT variable_value FROM PERFORMANCE_SCHEMA.SESSION_STATUS where Variable_name like 'Innodb_rows_read') r,
  56. (SELECT variable_value FROM PERFORMANCE_SCHEMA.SESSION_STATUS where Variable_name like 'Innodb_rows_updated') u""")
  57. record = cursor.fetchone()
  58. return int(int(record[0]) * cls.margin) #Mysql lowers the number of rows read
  59. @classmethod
  60. def get_rows_updated(cls, config):
  61. db = cls.get_connection(config)
  62. cursor = db.cursor()
  63. cursor.execute("show session status like 'Innodb_rows_updated'")
  64. record = cursor.fetchone()
  65. return int(int(record[1]) * cls.margin) #Mysql lowers the number of rows updated
  66. @classmethod
  67. def reset_cache(cls, config):
  68. #No more Cache in Mysql 8.0
  69. #cursor = self.db.cursor()
  70. #cursor.execute("RESET QUERY CACHE")
  71. #self.db.commit()
  72. return