Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit65c6d88

Browse files
committed
move back under scaletest cmd
1 parentfafca95 commit65c6d88

File tree

5 files changed

+343
-373
lines changed

5 files changed

+343
-373
lines changed

‎cli/root.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ func (r *RootCmd) Core() []*clibase.Cmd {
105105
// Hidden
106106
r.gitssh(),
107107
r.scaletest(),
108-
r.trafficGen(),
109108
r.vscodeSSH(),
110109
r.workspaceAgent(),
111110
}

‎cli/scaletest.go

Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strconv"
1111
"strings"
1212
"sync"
13+
"sync/atomic"
1314
"syscall"
1415
"time"
1516

@@ -42,6 +43,7 @@ func (r *RootCmd) scaletest() *clibase.Cmd {
4243
Children: []*clibase.Cmd{
4344
r.scaletestCleanup(),
4445
r.scaletestCreateWorkspaces(),
46+
r.scaletestTrafficGen(),
4547
},
4648
}
4749

@@ -947,6 +949,156 @@ func (r *RootCmd) scaletestCreateWorkspaces() *clibase.Cmd {
947949
returncmd
948950
}
949951

952+
typetrafficGenOutputstruct {
953+
DurationSecondsfloat64`json:"duration_s"`
954+
SentBytesint64`json:"sent_bytes"`
955+
RcvdBytesint64`json:"rcvd_bytes"`
956+
}
957+
958+
func (otrafficGenOutput)String()string {
959+
returnfmt.Sprintf("Duration: %.2fs\n",o.DurationSeconds)+
960+
fmt.Sprintf("Sent: %dB\n",o.SentBytes)+
961+
fmt.Sprintf("Rcvd: %dB",o.RcvdBytes)
962+
}
963+
964+
func (r*RootCmd)scaletestTrafficGen()*clibase.Cmd {
965+
var (
966+
duration time.Duration
967+
formatter=cliui.NewOutputFormatter(
968+
cliui.TextFormat(),
969+
cliui.JSONFormat(),
970+
)
971+
bpsint64
972+
client=new(codersdk.Client)
973+
)
974+
975+
cmd:=&clibase.Cmd{
976+
Use:"trafficgen",
977+
Hidden:true,
978+
Short:"Generate traffic to a Coder workspace",
979+
Middleware:clibase.Chain(
980+
clibase.RequireRangeArgs(1,2),
981+
r.InitClient(client),
982+
),
983+
Handler:func(inv*clibase.Invocation)error {
984+
var (
985+
agentNamestring
986+
tickInterval=100*time.Millisecond
987+
)
988+
ws,err:=namedWorkspace(inv.Context(),client,inv.Args[0])
989+
iferr!=nil {
990+
returnerr
991+
}
992+
993+
varagentID uuid.UUID
994+
for_,res:=rangews.LatestBuild.Resources {
995+
iflen(res.Agents)==0 {
996+
continue
997+
}
998+
ifagentName!=""&&agentName!=res.Agents[0].Name {
999+
continue
1000+
}
1001+
agentID=res.Agents[0].ID
1002+
}
1003+
1004+
ifagentID==uuid.Nil {
1005+
returnxerrors.Errorf("no agent found for workspace %s",ws.Name)
1006+
}
1007+
1008+
// Setup our workspace agent connection.
1009+
reconnect:=uuid.New()
1010+
conn,err:=client.WorkspaceAgentReconnectingPTY(inv.Context(), codersdk.WorkspaceAgentReconnectingPTYOpts{
1011+
AgentID:agentID,
1012+
Reconnect:reconnect,
1013+
Height:65535,
1014+
Width:65535,
1015+
Command:"/bin/sh",
1016+
})
1017+
iferr!=nil {
1018+
returnxerrors.Errorf("connect to workspace: %w",err)
1019+
}
1020+
1021+
deferfunc() {
1022+
_=conn.Close()
1023+
}()
1024+
1025+
// Wrap the conn in a countReadWriter so we can monitor bytes sent/rcvd.
1026+
crw:=countReadWriter{ReadWriter:conn}
1027+
1028+
// Set a deadline for stopping the text.
1029+
start:=time.Now()
1030+
deadlineCtx,cancel:=context.WithDeadline(inv.Context(),start.Add(duration))
1031+
defercancel()
1032+
1033+
// Create a ticker for sending data to the PTY.
1034+
tick:=time.NewTicker(tickInterval)
1035+
defertick.Stop()
1036+
1037+
// Now we begin writing random data to the pty.
1038+
writeSize:=int(bps/10)
1039+
rch:=make(chanerror)
1040+
wch:=make(chanerror)
1041+
1042+
// Read forever in the background.
1043+
gofunc() {
1044+
rch<-readContext(deadlineCtx,&crw,writeSize*2)
1045+
conn.Close()
1046+
close(rch)
1047+
}()
1048+
1049+
// Write random data to the PTY every tick.
1050+
gofunc() {
1051+
wch<-writeRandomData(deadlineCtx,&crw,writeSize,tick.C)
1052+
close(wch)
1053+
}()
1054+
1055+
// Wait for both our reads and writes to be finished.
1056+
ifwErr:=<-wch;wErr!=nil {
1057+
returnxerrors.Errorf("write to pty: %w",wErr)
1058+
}
1059+
ifrErr:=<-rch;rErr!=nil {
1060+
returnxerrors.Errorf("read from pty: %w",rErr)
1061+
}
1062+
1063+
duration:=time.Since(start)
1064+
1065+
results:=trafficGenOutput{
1066+
DurationSeconds:duration.Seconds(),
1067+
SentBytes:crw.BytesWritten(),
1068+
RcvdBytes:crw.BytesRead(),
1069+
}
1070+
1071+
out,err:=formatter.Format(inv.Context(),results)
1072+
iferr!=nil {
1073+
returnerr
1074+
}
1075+
1076+
_,err=fmt.Fprintln(inv.Stdout,out)
1077+
returnerr
1078+
},
1079+
}
1080+
1081+
cmd.Options= []clibase.Option{
1082+
{
1083+
Flag:"duration",
1084+
Env:"CODER_SCALETEST_TRAFFICGEN_DURATION",
1085+
Default:"10s",
1086+
Description:"How long to generate traffic for.",
1087+
Value:clibase.DurationOf(&duration),
1088+
},
1089+
{
1090+
Flag:"bps",
1091+
Env:"CODER_SCALETEST_TRAFFICGEN_BPS",
1092+
Default:"1024",
1093+
Description:"How much traffic to generate in bytes per second.",
1094+
Value:clibase.Int64Of(&bps),
1095+
},
1096+
}
1097+
1098+
formatter.AttachOptions(&cmd.Options)
1099+
returncmd
1100+
}
1101+
9501102
typerunnableTraceWrapperstruct {
9511103
tracer trace.Tracer
9521104
spanNamestring
@@ -1023,3 +1175,111 @@ func isScaleTestWorkspace(workspace codersdk.Workspace) bool {
10231175
returnstrings.HasPrefix(workspace.OwnerName,"scaletest-")||
10241176
strings.HasPrefix(workspace.Name,"scaletest-")
10251177
}
1178+
1179+
funcreadContext(ctx context.Context,src io.Reader,bufSizeint)error {
1180+
buf:=make([]byte,bufSize)
1181+
for {
1182+
select {
1183+
case<-ctx.Done():
1184+
returnnil
1185+
default:
1186+
ifctx.Err()!=nil {
1187+
returnnil
1188+
}
1189+
_,err:=src.Read(buf)
1190+
iferr!=nil {
1191+
ifxerrors.Is(err,io.EOF) {
1192+
returnnil
1193+
}
1194+
returnerr
1195+
}
1196+
}
1197+
}
1198+
}
1199+
1200+
funcwriteRandomData(ctx context.Context,dst io.Writer,sizeint,tick<-chan time.Time)error {
1201+
for {
1202+
select {
1203+
case<-ctx.Done():
1204+
returnnil
1205+
case<-tick:
1206+
payload:="#"+mustRandStr(size-1)
1207+
data,err:=json.Marshal(codersdk.ReconnectingPTYRequest{
1208+
Data:payload,
1209+
})
1210+
iferr!=nil {
1211+
returnerr
1212+
}
1213+
if_,err:=copyContext(ctx,dst,data);err!=nil {
1214+
returnerr
1215+
}
1216+
}
1217+
}
1218+
}
1219+
1220+
// copyContext copies from src to dst until ctx is canceled.
1221+
funccopyContext(ctx context.Context,dst io.Writer,src []byte) (int,error) {
1222+
varcountint
1223+
for {
1224+
select {
1225+
case<-ctx.Done():
1226+
returncount,nil
1227+
default:
1228+
ifctx.Err()!=nil {
1229+
returncount,nil
1230+
}
1231+
n,err:=dst.Write(src)
1232+
iferr!=nil {
1233+
ifxerrors.Is(err,io.EOF) {
1234+
// On an EOF, assume that all of src was consumed.
1235+
returnlen(src),nil
1236+
}
1237+
returncount,err
1238+
}
1239+
count+=n
1240+
ifn==len(src) {
1241+
returncount,nil
1242+
}
1243+
// Not all of src was consumed. Update src and retry.
1244+
src=src[n:]
1245+
}
1246+
}
1247+
}
1248+
1249+
typecountReadWriterstruct {
1250+
io.ReadWriter
1251+
bytesRead atomic.Int64
1252+
bytesWritten atomic.Int64
1253+
}
1254+
1255+
func (w*countReadWriter)Read(p []byte) (int,error) {
1256+
n,err:=w.ReadWriter.Read(p)
1257+
iferr==nil {
1258+
w.bytesRead.Add(int64(n))
1259+
}
1260+
returnn,err
1261+
}
1262+
1263+
func (w*countReadWriter)Write(p []byte) (int,error) {
1264+
n,err:=w.ReadWriter.Write(p)
1265+
iferr==nil {
1266+
w.bytesWritten.Add(int64(n))
1267+
}
1268+
returnn,err
1269+
}
1270+
1271+
func (w*countReadWriter)BytesRead()int64 {
1272+
returnw.bytesRead.Load()
1273+
}
1274+
1275+
func (w*countReadWriter)BytesWritten()int64 {
1276+
returnw.bytesWritten.Load()
1277+
}
1278+
1279+
funcmustRandStr(lenint)string {
1280+
randStr,err:=cryptorand.String(len)
1281+
iferr!=nil {
1282+
panic(err)
1283+
}
1284+
returnrandStr
1285+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp