Skip to content

Commit 8adf907

Browse files
committed
feat: add performance testing to run on the page
1 parent c1c203f commit 8adf907

17 files changed

+127
-32
lines changed

cmd/sponge/server/handler.go

Lines changed: 96 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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
335377
func 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

cmd/sponge/server/http.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ func NewRouter(spongeAddr string, isLog bool) *gin.Engine {
5959
apiV1.POST("/getTemplateInfo", GetTemplateInfo)
6060
apiV1.POST("/assistant", HandleAssistant)
6161
apiV1.POST("/performanceTest", HandlePerformanceTest)
62+
apiV1.POST("/performanceTest/stop", HandleStopPerformanceTest)
6263
apiV1.POST("/uploadFiles", UploadFiles)
6364
apiV1.POST("/listTables", ListTables)
6465
apiV1.GET("/listDrivers", ListDbDrivers)

cmd/sponge/server/record.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,13 @@ type parameters struct {
3838
DepProtoDir string `json:"depProtoDir"`
3939
OnlyPrint bool `json:"onlyPrint"`
4040

41-
LLMType string `json:"llmType"`
42-
LLMModel string `json:"llmModel"`
43-
APIKey string `json:"apiKey"`
44-
GoDir string `json:"goDir"`
45-
GoFile string `json:"goFile"`
46-
IsSpecifiedGoFile bool `json:"isSpecifiedGoFile"`
41+
LLMType string `json:"llmType"`
42+
LLMModel string `json:"llmModel"`
43+
APIKey string `json:"apiKey"`
44+
GoDir string `json:"goDir"`
45+
GoFile string `json:"goFile"`
46+
TargetType string `json:"targetType"`
47+
IsCleanAICode bool `json:"isCleanAICode"`
4748

4849
Protocol string `json:"protocol"`
4950
URL string `json:"url"`
@@ -143,6 +144,9 @@ func parseCommandArgs(args []string) *parameters {
143144
if ss[0] == "--only-print" {
144145
params.OnlyPrint = true
145146
}
147+
if ss[0] == "--is-clean" {
148+
params.IsCleanAICode = true
149+
}
146150
} else {
147151
val := ss[1]
148152
switch ss[0] {
@@ -197,10 +201,12 @@ func parseCommandArgs(args []string) *parameters {
197201
params.APIKey = val
198202
case "--dir":
199203
params.GoDir = val
200-
params.IsSpecifiedGoFile = false
204+
params.TargetType = "goDir"
201205
case "--file":
202206
params.GoFile = val
203-
params.IsSpecifiedGoFile = true
207+
params.TargetType = "goFile"
208+
case "--is-clean":
209+
params.IsCleanAICode = val == "true"
204210

205211
case "--url":
206212
params.URL = val
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
var appConfig = {
1+
const appConfig = {
22
spongeServiceAddr: "http://localhost:24631/api/v1",
33
};

cmd/sponge/server/static/css/app.161971ad6f41b518cab65fa120506c99.css

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/sponge/server/static/css/app.161971ad6f41b518cab65fa120506c99.css.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/sponge/server/static/css/app.81ae100c53d45c1085ec370cf96a8ce1.css

Lines changed: 0 additions & 2 deletions
This file was deleted.

cmd/sponge/server/static/css/app.81ae100c53d45c1085ec370cf96a8ce1.css.map

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
<!DOCTYPE html><html><head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><title>Sponge Generate Code</title><link rel=icon type=image/png sizes=32x32 href="/static/favicon.png?v=1.0"><script type=text/javascript src=/static/appConfig.js async></script><link href=/static/css/app.81ae100c53d45c1085ec370cf96a8ce1.css rel=stylesheet></head><body style="margin: 0px; padding: 0px;"><style>.el-tooltip__popper {box-shadow: 3px 3px 10px 5px #d3d3d6;border-width: 0px !important;}
2-
.el-tooltip__popper[x-placement^="top"] .popper__arrow:after {border-top-color: #dcdfe6 !important;}</style><div id=app></div><script type=text/javascript src=/static/js/manifest.2ae2e69a05c33dfc65f8.js></script><script type=text/javascript src=/static/js/vendor.d8cdb3748af43d2ae51a.js></script><script type=text/javascript src=/static/js/app.abed82fc254ec468ebb1.js></script></body></html>
1+
<!DOCTYPE html><html><head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><title>Sponge Generate Code</title><link rel=icon type=image/png sizes=32x32 href="/static/favicon.png?v=1.0"><script type=text/javascript src=/static/appConfig.js async></script><link href=/static/css/app.161971ad6f41b518cab65fa120506c99.css rel=stylesheet></head><body style="margin: 0px; padding: 0px;"><style>.el-tooltip__popper {box-shadow: 3px 3px 10px 5px #d3d3d6;border-width: 0px !important;}
2+
.el-tooltip__popper[x-placement^="top"] .popper__arrow:after {border-top-color: #dcdfe6 !important;}</style><div id=app></div><script type=text/javascript src=/static/js/manifest.2ae2e69a05c33dfc65f8.js></script><script type=text/javascript src=/static/js/vendor.81645738747fb507a683.js></script><script type=text/javascript src=/static/js/app.7d14490af8ba07678235.js></script></body></html>

cmd/sponge/server/static/js/app.7d14490af8ba07678235.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)