Skip to content

Commit 6418c5d

Browse files
committed
run as cs mode
Signed-off-by: FingerLeader <[email protected]>
1 parent 9675d18 commit 6418c5d

File tree

3 files changed

+53
-26
lines changed

3 files changed

+53
-26
lines changed

go.mod

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
module main
22

3-
go 1.15
3+
go 1.16
44

55
require (
6-
github.com/dustin/go-humanize v1.0.0 // indirect
6+
github.com/dustin/go-humanize v1.0.0
77
github.com/shirou/gopsutil v3.21.11+incompatible
8+
github.com/stretchr/testify v1.7.1 // indirect
89
github.com/yusufpapurcu/wmi v1.2.2 // indirect
910
golang.org/x/sys v0.0.0-20211209171907-798191bca915 // indirect
1011
)

go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
13
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
24
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
35
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
46
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
7+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
8+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
59
github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI=
610
github.com/shirou/gopsutil v3.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
11+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
12+
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
13+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
714
github.com/yusufpapurcu/wmi v1.2.2 h1:KBNDSne4vP5mbSWnJbO+51IMOXJB67QiYCSBrubbPRg=
815
github.com/yusufpapurcu/wmi v1.2.2/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
916
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
1017
golang.org/x/sys v0.0.0-20211209171907-798191bca915 h1:P+8mCzuEpyszAT6T42q0sxU+eveBAF/cJ2Kp0x6/8+0=
1118
golang.org/x/sys v0.0.0-20211209171907-798191bca915/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
19+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
20+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
21+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

main.go

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"flag"
2020
"fmt"
2121
"os"
22+
"os/exec"
2223
"strconv"
2324
"syscall"
2425
"time"
@@ -31,12 +32,14 @@ var (
3132
memSize string
3233
growthTime string
3334
workers int
35+
client bool
3436
)
3537

3638
func init() {
3739
flag.StringVar(&memSize, "size", "0KB", "size of memory you want to allocate")
3840
flag.StringVar(&growthTime, "time", "0s", "time to reach the size of memory you allocated")
3941
flag.IntVar(&workers, "workers", 1, "number of workers allocating memory")
42+
flag.BoolVar(&client, "client", false, "the process runs as a client")
4043
flag.Parse()
4144
}
4245

@@ -92,34 +95,47 @@ func run(length uint64, timeLine time.Duration) {
9295
}
9396

9497
func main() {
95-
memInfo, _ := psutil.VirtualMemory()
96-
var length uint64
97-
98-
if memSize[len(memSize)-1] != '%' {
99-
var err error
100-
length, err = humanize.ParseBytes(memSize)
101-
if err != nil {
102-
// TODO
103-
fmt.Println(err)
98+
if !client {
99+
workQueue := make(chan struct{}, workers)
100+
for {
101+
workQueue <- struct{}{}
102+
go func() {
103+
err := exec.Command("./memStress", "--size", memSize, "--time", growthTime, "--client", "1").Run()
104+
if err != nil {
105+
fmt.Println(err)
106+
}
107+
<-workQueue
108+
}()
109+
time.Sleep(time.Second)
104110
}
111+
105112
} else {
106-
percentage, err := strconv.ParseFloat(memSize[0:len(memSize)-1], 64)
107-
if err != nil {
108-
fmt.Println(err)
113+
memInfo, _ := psutil.VirtualMemory()
114+
var length uint64
115+
116+
if memSize[len(memSize)-1] != '%' {
117+
var err error
118+
length, err = humanize.ParseBytes(memSize)
119+
if err != nil {
120+
// TODO
121+
fmt.Println(err)
122+
}
123+
} else {
124+
percentage, err := strconv.ParseFloat(memSize[0:len(memSize)-1], 64)
125+
if err != nil {
126+
fmt.Println(err)
127+
}
128+
length = uint64(float64(memInfo.Total) / 100.0 * percentage)
109129
}
110-
length = uint64(float64(memInfo.Total) / 100.0 * percentage)
111-
}
112-
113-
timeLine, err := time.ParseDuration(growthTime)
114-
if err != nil {
115-
// TODO
116-
}
117130

118-
for i := 0; i < workers; i++ {
119-
go run(length, timeLine)
120-
}
131+
timeLine, err := time.ParseDuration(growthTime)
132+
if err != nil {
133+
// TODO
134+
}
135+
run(length, timeLine)
121136

122-
for {
123-
time.Sleep(time.Second * 2)
137+
for {
138+
time.Sleep(time.Second * 2)
139+
}
124140
}
125141
}

0 commit comments

Comments
 (0)