ptest.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. package http
  2. import (
  3. "encoding/json"
  4. "go-common/app/admin/ep/melloi/model"
  5. "go-common/app/admin/ep/melloi/service"
  6. "go-common/library/log"
  7. bm "go-common/library/net/http/blademaster"
  8. "go-common/library/net/http/blademaster/binding"
  9. )
  10. //excute performTest
  11. func doPtest(c *bm.Context) {
  12. var (
  13. script model.Script
  14. ptestParam model.DoPtestParam
  15. )
  16. userName := c.Request.Form.Get("userName")
  17. param := c.Request.Form.Get("param")
  18. resJtl := c.Request.Form.Get("resJtl")
  19. jmeterLog := c.Request.Form.Get("jmeterLog")
  20. cookie := c.Request.Header.Get("Cookie")
  21. if err := json.Unmarshal([]byte(param), &script); err != nil {
  22. log.Error("sorry,script err(%v)", err)
  23. c.JSON(nil, err)
  24. return
  25. }
  26. log.Info("接口名:(%s),压测脚本路径:(%s)", script.TestName, script.SavePath)
  27. if script.Upload {
  28. testNames := service.QueryTestNamesByJmfile(script.SavePath)
  29. ptestParam = model.DoPtestParam{
  30. TestNames: testNames,
  31. FileName: script.SavePath,
  32. LoadTime: 1800,
  33. UserName: userName,
  34. Upload: true,
  35. ProjectName: script.ProjectName,
  36. FileSplit: script.FileSplit,
  37. SplitNum: script.SplitNum,
  38. APIHeader: script.APIHeader,
  39. }
  40. } else {
  41. ptestParam = model.DoPtestParam{
  42. TestNames: service.StringToSlice(script.TestName),
  43. FileName: script.SavePath,
  44. LoadTime: script.LoadTime,
  45. UserName: userName,
  46. URL: script.URL,
  47. FileSplit: script.FileSplit,
  48. SplitNum: script.SplitNum,
  49. APIHeader: script.APIHeader,
  50. }
  51. }
  52. ptestParam.Department = script.Department
  53. ptestParam.Project = script.Project
  54. ptestParam.APP = script.App
  55. ptestParam.Domain = script.Domain
  56. ptestParam.Cookie = cookie
  57. ptestParam.JmeterLog = jmeterLog
  58. ptestParam.ResJtl = resJtl
  59. ptestParam.ScriptID = script.ID
  60. ptestParam.UseBusinessStop = script.UseBusinessStop
  61. ptestParam.BusinessStopPercent = script.BusinessStopPercent
  62. ptestParam.Fusing = script.Fusing
  63. c.JSON(srv.DoPtest(c, ptestParam))
  64. }
  65. //addPtest 加压
  66. func addPtest(c *bm.Context) {
  67. var ptestAdd model.PtestAdd
  68. if err := c.BindWith(&ptestAdd, binding.Form); err != nil {
  69. c.JSON(nil, err)
  70. return
  71. }
  72. log.Info("AddPtest ----")
  73. c.JSON(nil, srv.AddPtest(c, ptestAdd))
  74. }
  75. //stopPtest
  76. func stopPtest(c *bm.Context) {
  77. var ptestJob model.PtestJob
  78. if err := c.BindWith(&ptestJob, binding.Form); err != nil {
  79. c.JSON(nil, err)
  80. return
  81. }
  82. c.JSON(nil, srv.StopPtest(c, ptestJob))
  83. }
  84. //stopAllPtest
  85. func stopAllPtest(c *bm.Context) {
  86. var allPtestStop model.AllPtestStop
  87. if err := c.BindWith(&allPtestStop, binding.Form); err != nil {
  88. c.JSON(nil, err)
  89. return
  90. }
  91. c.JSON(srv.QueryOrStopAllPtestByJobName(c, allPtestStop.ReportSuID, true, 3))
  92. }
  93. //reducePtest
  94. func reducePtest(c *bm.Context) {
  95. var (
  96. reducePtest model.ReducePtest
  97. resMap = make(map[string]interface{})
  98. )
  99. if err := c.BindWith(&reducePtest, binding.Form); err != nil {
  100. c.JSON(nil, err)
  101. return
  102. }
  103. message, err := srv.ReducePtest(c, reducePtest)
  104. if err != nil {
  105. return
  106. }
  107. resMap["message"] = message
  108. c.JSON(resMap, err)
  109. }
  110. //queryAllJob
  111. func queryAllJob(c *bm.Context) {
  112. var (
  113. ptestJob model.PtestJob
  114. resMap = make(map[string]interface{})
  115. )
  116. if err := c.BindWith(&ptestJob, binding.Form); err != nil {
  117. c.JSON(nil, err)
  118. return
  119. }
  120. ptestJobs, err := srv.QueryOrStopAllPtestByJobName(c, ptestJob.ReportSuID, false, 3)
  121. if err != nil {
  122. log.Error("query ptestJob err", err)
  123. return
  124. }
  125. resMap["ptest_jobs"] = ptestJobs
  126. c.JSON(resMap, err)
  127. }
  128. //queryAllJobFree queryAll JobFree
  129. func queryAllJobFree(c *bm.Context) {
  130. var (
  131. ptestJob model.PtestJob
  132. resMap = make(map[string]interface{})
  133. )
  134. if err := c.BindWith(&ptestJob, binding.Form); err != nil {
  135. c.JSON(nil, err)
  136. return
  137. }
  138. ptestJobs, err := srv.QueryAllJobFree(c, &ptestJob)
  139. if err != nil {
  140. log.Error("query ptestJob err", err)
  141. return
  142. }
  143. resMap["ptest_jobs"] = ptestJobs
  144. c.JSON(resMap, err)
  145. }
  146. //doDebug
  147. func doDebug(c *bm.Context) {
  148. var script model.Script
  149. var ptestParam model.DoPtestParam
  150. param := c.Request.Form.Get("param")
  151. userName := c.Request.Form.Get("userName")
  152. if err := json.Unmarshal([]byte(param), &script); err != nil {
  153. log.Error("sorry,script err(%v)", err)
  154. c.JSON(nil, err)
  155. return
  156. }
  157. log.Info("接口名:(%s),压测脚本路径:(%s)", script.TestName, script.SavePath)
  158. if script.Upload {
  159. ptestParam = model.DoPtestParam{
  160. TestNames: service.QueryTestNamesByJmfile(script.SavePath),
  161. FileName: script.SavePath,
  162. LoadTime: 1800,
  163. UserName: userName,
  164. Upload: true,
  165. ProjectName: script.ProjectName,
  166. ResJtl: script.ResJtl,
  167. JmeterLog: script.JmeterLog,
  168. Department: script.Department,
  169. Project: script.Project,
  170. APP: script.App,
  171. ScriptID: script.ID,
  172. }
  173. } else {
  174. ptestParam = model.DoPtestParam{
  175. TestNames: service.StringToSlice(script.TestName),
  176. FileName: script.SavePath,
  177. LoadTime: script.LoadTime,
  178. UserName: userName,
  179. ResJtl: script.ResJtl,
  180. JmeterLog: script.JmeterLog,
  181. Department: script.Department,
  182. Project: script.Project,
  183. APP: script.App,
  184. ScriptID: script.ID,
  185. Domain: script.Domain,
  186. }
  187. }
  188. c.JSON(srv.DoPtest(c, ptestParam))
  189. }
  190. func doPtestByFile(c *bm.Context) {
  191. var script model.Script
  192. if err := c.BindWith(&script, binding.JSON); err != nil {
  193. c.JSON(nil, err)
  194. return
  195. }
  196. resJtl := script.ResJtl
  197. jmeterLog := script.JmeterLog
  198. script.ResJtl = ""
  199. script.JmeterLog = ""
  200. resp, err := srv.DoPtestByFile(c, script, resJtl, jmeterLog)
  201. if err != nil {
  202. c.JSON(err, nil)
  203. return
  204. }
  205. c.JSON(resp, err)
  206. }
  207. //excute batch ptest
  208. func doPtestBatch(c *bm.Context) {
  209. var PtestBatch model.PtestBatch
  210. if err := c.BindWith(&PtestBatch, binding.JSON); err != nil {
  211. c.JSON(nil, err)
  212. return
  213. }
  214. cookie := c.Request.Header.Get("Cookie")
  215. status, _ := srv.DoPtestArr(c, PtestBatch, cookie)
  216. var doPostMap = make(map[string]string)
  217. doPostMap["status"] = status
  218. c.JSON(doPostMap, nil)
  219. }
  220. //doPtestByScriptId
  221. func doPtestByScriptId(c *bm.Context) {
  222. var script model.Script
  223. if err := c.BindWith(&script, binding.Form); err != nil {
  224. c.JSON(nil, err)
  225. return
  226. }
  227. cookie := c.Request.Header.Get("Cookie")
  228. executor := c.Request.Form.Get("executor")
  229. c.JSON(srv.DoPtestByScriptId(c, &script, cookie, executor))
  230. }