自定义脚本

  1. require("../oltp_common")
  2. math.randomseed(tostring(os.time()):reverse():sub(1, 7))
  3. Names = {}
  4. Desc = {}
  5. --[[
  6. 线程启动的时候调用
  7. ]]
  8. function thread_init()
  9. drv = sysbench.sql.driver()
  10. con = drv:connect()
  11. -- con:query("set names utf8mb4")
  12. -- 这里可以从文件中取出预设好的值,然后随机作为条件查询
  13. -- -- 初始化测试数据,参数
  14. -- -- 初始化人名数据
  15. -- local names = io.open("./name", "r")
  16. -- if names ~= nil then
  17. -- for line in names:lines() do
  18. -- table.insert(Names, line)
  19. -- end
  20. -- names:close()
  21. -- end
  22. -- -- 初始化描述
  23. -- local descs = io.open("./desc", "r")
  24. -- if descs ~= nil then
  25. -- for line in descs:lines() do
  26. -- table.insert(Desc, line)
  27. -- end
  28. -- descs:close()
  29. -- end
  30. end
  31. --[[
  32. 线程结束的时候调用
  33. ]]
  34. -- function thread_done()
  35. -- Con:disconnect()
  36. -- -- print(string.format("stop thread %d", sysbench.tid))
  37. -- end
  38. -- 随机字符串【中文会乱码】
  39. function getRandom(n)
  40. local t = "电力业务许可证侣昼碴拖瑞旋想讯牢将汗雾力道瘁策愧啥幻迅民稠胯毋贬康讶殃"
  41. -- local t = "ewdsadfasdadadadfwefsfasfa"
  42. local s = ""
  43. for i =1, n do
  44. local rand = math.random(1, #t)
  45. s = s .. t:sub(rand,rand)
  46. end;
  47. return s
  48. end;
  49. -- 压测test_company
  50. function test_company()
  51. -- 随机范围内的数字
  52. local rand_id = math.random(1, 100000000)
  53. -- 走覆盖索引
  54. -- con:query(string.format("Select id from test_company where id = %d", rand_id))
  55. -- 回表所有字段
  56. -- con:query(string.format("Select * from test_company where id = %d", rand_id))
  57. -- 回表单字段
  58. con:query(string.format("Select company_code from test_company where id = %d", rand_id))
  59. end
  60. -- 压测test_jobs
  61. function test_jobs()
  62. -- 随机范围内的数字
  63. local rand_id = math.random(1, 100000000)
  64. -- 走覆盖索引
  65. con:query(string.format("Select company_id from test_jobs where id = %d", rand_id))
  66. -- 回表所有字段
  67. -- con:query(string.format("Select * from test_jobs where company_id = %d", rand_id))
  68. -- 回表单字段
  69. -- con:query(string.format("Select job_type from test_jobs where company_id = %d", rand_id))
  70. end
  71. --[[
  72. 执行
  73. ]]
  74. function event()
  75. -- test_company()
  76. test_jobs()
  77. end