@@ -10,7 +10,9 @@ import (
1010 "os"
1111 "path"
1212 "path/filepath"
13+ "sort"
1314 "strings"
15+ "sync"
1416 "time"
1517
1618 "github.com/gin-gonic/gin"
@@ -22,6 +24,7 @@ import (
2224 "github.com/go-dev-frame/sponge/pkg/gofile"
2325 "github.com/go-dev-frame/sponge/pkg/krand"
2426 "github.com/go-dev-frame/sponge/pkg/mgo"
27+ "github.com/go-dev-frame/sponge/pkg/process"
2528 "github.com/go-dev-frame/sponge/pkg/sgorm"
2629 "github.com/go-dev-frame/sponge/pkg/sgorm/mysql"
2730 "github.com/go-dev-frame/sponge/pkg/sgorm/postgresql"
@@ -307,7 +310,7 @@ func HandleAssistant(c *gin.Context) {
307310 args := strings .Split (form .Arg , " " )
308311 params := parseCommandArgs (args )
309312
310- ctx , _ := context .WithTimeout (context .Background (), time .Minute * 30 ) // nolint
313+ ctx , _ := context .WithTimeout (context .Background (), time .Minute * 60 ) // nolint
311314 result := gobash .Run (ctx , "sponge" , args ... )
312315 resultInfo := ""
313316 count := 0
@@ -331,6 +334,45 @@ func HandleAssistant(c *gin.Context) {
331334 response .Success (c , resultInfo )
332335}
333336
337+ var processMap = sync.Map {}
338+
339+ func getCommand (args []string ) string {
340+ if len (args ) < 4 {
341+ return ""
342+ }
343+ commandArgs := []string {"sponge" , args [0 ], args [1 ]}
344+ for _ , arg := range args {
345+ if strings .Contains (arg , "--url" ) {
346+ commandArgs = append (commandArgs , arg )
347+ }
348+ if strings .Contains (arg , "--method" ) {
349+ commandArgs = append (commandArgs , arg )
350+ }
351+ }
352+ sort .Strings (commandArgs )
353+ return strings .Join (commandArgs , "&" )
354+ }
355+
356+ func addProcess (key string , pid int ) {
357+ processMap .Store (key , pid )
358+ }
359+
360+ func getPid (key string ) (int , bool ) {
361+ value , ok := processMap .Load (key )
362+ if ! ok {
363+ return - 1 , false
364+ }
365+ valueInt , ok := value .(int )
366+ if ! ok {
367+ return - 1 , false
368+ }
369+ return valueInt , true
370+ }
371+
372+ func removeProcess (key string ) {
373+ processMap .Delete (key )
374+ }
375+
334376// HandlePerformanceTest handle performance test
335377func HandlePerformanceTest (c * gin.Context ) {
336378 form := & GenerateCodeForm {}
@@ -358,13 +400,22 @@ func HandlePerformanceTest(c *gin.Context) {
358400 params .PushType = "custom"
359401 }
360402
361- ctx , _ := context .WithTimeout (context .Background (), time .Minute * 30 ) // nolint
403+ ctx , _ := context .WithTimeout (context .Background (), time .Hour * 24 ) // nolint
362404 result := gobash .Run (ctx , "sponge" , args ... )
405+ key := ""
406+ pid := 0
363407 resultInfo := ""
364408 count := 0
365409 for v := range result .StdOut {
366410 count ++
367- if count == 1 { // first line is the command
411+ if count == 1 { // first line is the command and pid value
412+ pid = gobash .ParsePid (v )
413+ if pid > 0 {
414+ key = getCommand (args )
415+ if result .Pid > 0 {
416+ addProcess (key , result .Pid )
417+ }
418+ }
368419 continue
369420 }
370421 if strings .Contains (v , "Waiting for assistant responses" ) {
@@ -379,12 +430,48 @@ func HandlePerformanceTest(c *gin.Context) {
379430
380431 recordObj ().set (c .ClientIP (), form .Path , params )
381432
382- resultInfo = splitString (resultInfo , "Performance Test Report ==========" )
433+ if result .Pid > 0 {
434+ removeProcess (key )
435+ }
383436
384- response .Success (c , resultInfo )
437+ lineContent , reportStr := splitString (resultInfo , "Performance Test Report ==========" )
438+ command := "sponge " + strings .Join (args , " " )
439+ insertStr := fmt .Sprintf (`
440+
441+ [Overview]
442+ • %-19s%s
443+ • %-19s%s` , "Command:" , command ,
444+ "End Time:" , time .Now ().Format (time .DateTime ))
445+ reportStr = strings .ReplaceAll (reportStr , lineContent , lineContent + insertStr )
446+
447+ response .Success (c , reportStr )
448+ }
449+
450+ // HandleStopPerformanceTest handle stop performance test
451+ func HandleStopPerformanceTest (c * gin.Context ) {
452+ form := & GenerateCodeForm {}
453+ err := c .ShouldBindJSON (form )
454+ if err != nil {
455+ responseErr (c , err , errcode .InvalidParams )
456+ return
457+ }
458+
459+ args := strings .Split (form .Arg , " " )
460+ key := getCommand (args )
461+ pid , _ := getPid (key )
462+ if pid > 0 {
463+ err = process .Kill (pid )
464+ if err != nil {
465+ responseErr (c , err , errcode .InternalServerError )
466+ return
467+ }
468+ removeProcess (key )
469+ }
470+
471+ response .Success (c )
385472}
386473
387- func splitString (str string , sep string ) string {
474+ func splitString (str string , sep string ) ( lineContent string , out string ) {
388475 lines := strings .Split (str , "\n " )
389476 startIndex := 0
390477 isFound := false
@@ -393,14 +480,15 @@ func splitString(str string, sep string) string {
393480 if strings .Contains (line , sep ) {
394481 isFound = true
395482 startIndex = i
483+ lineContent = line
396484 break
397485 }
398486 }
399487
400488 if isFound {
401- return strings .Join (lines [startIndex :], "\n " )
489+ return lineContent , strings .Join (lines [startIndex :], "\n " )
402490 }
403- return str
491+ return "" , str
404492}
405493
406494// GetRecord generate run command record
0 commit comments