Skip to content

Commit cbb88b5

Browse files
committed
add concurrency config option
1 parent f7b57d0 commit cbb88b5

File tree

3 files changed

+53
-21
lines changed

3 files changed

+53
-21
lines changed

config.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ type Config struct {
1515
TeamName string `json:"team_name"` // optional: if empty then a new team will be created
1616
teamID string
1717

18-
UserCount int `json:"user_count"` // number of users to create
18+
ConcurrentUsers int `json:"concurrent_users"` // number of users to simulate concurrently
19+
UserCount int `json:"user_count"` // number of users to create
1920

2021
ChannelsPerUser int `json:"channels_per_user"`
2122
BoardsPerChannel int `json:"boards_per_channel"`
@@ -35,6 +36,7 @@ func createDefaultConfig(filename string) error {
3536
AdminUsername: "",
3637
AdminPassword: "",
3738
TeamName: "",
39+
ConcurrentUsers: DefaultConcurrentUsers,
3840
UserCount: DefaultUserCount,
3941
ChannelsPerUser: DefaultChannelsPerUser,
4042
BoardsPerChannel: DefaultBoardsPerChannel,

main.go

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
)
1717

1818
const (
19+
DefaultConcurrentUsers = 3
1920
DefaultUserCount = 5
2021
DefaultChannelsPerUser = 3
2122
DefaultBoardsPerChannel = 5
@@ -56,7 +57,7 @@ func main() {
5657
}
5758

5859
defer func(l *logr.Logr) {
59-
if lgr.IsShutdown() {
60+
if l.IsShutdown() {
6061
return
6162
}
6263
ctx, cancel := context.WithTimeout(context.Background(), time.Second*15)
@@ -148,32 +149,61 @@ func main() {
148149
func run(ri *runInfo, workersExited chan struct{}) {
149150
defer close(workersExited)
150151
var wg sync.WaitGroup
151-
for i := 0; i < ri.cfg.UserCount; i++ {
152-
wg.Add(1)
153152

154-
username := strings.ToLower(makeName("."))
153+
var usersLeft int32 = int32(ri.cfg.UserCount)
154+
concurrency := ri.cfg.ConcurrentUsers
155+
156+
if !ri.quiet {
157+
s := fmt.Sprintf("Creating %d users with %d concurrent threads.\n\n", usersLeft, concurrency)
158+
ri.output.Write(s)
159+
}
155160

156-
go func(u string) {
161+
for i := 0; i < concurrency; i++ {
162+
wg.Add(1)
163+
go func() {
157164
defer wg.Done()
158-
stats, err := runUser(u, ri)
159-
if err != nil {
160-
ri.logger.Error("Cannot simulate user", logr.Err(err))
161-
}
162-
163-
if !ri.quiet {
164-
s := fmt.Sprintf("%s: channels=%d boards=%d cards=%d text=%d\n",
165-
username, stats.ChannelCount, stats.BoardCount, stats.CardCount, stats.TextCount)
166-
167-
ri.output.Write(s)
168-
}
169-
}(username)
165+
runConcurrentUsers(ri, &usersLeft)
166+
}()
170167
}
171168

172169
wg.Wait()
173170
}
174171

172+
func runConcurrentUsers(ri *runInfo, usersLeft *int32) {
173+
fmt.Println("Starting thread")
174+
175+
for {
176+
select {
177+
case <-ri.abort:
178+
fmt.Println("Exiting thread (abort)")
179+
return
180+
default:
181+
}
182+
183+
left := atomic.AddInt32(usersLeft, -1)
184+
if left <= 0 {
185+
fmt.Println("Exiting thread (userLeft <= 0)")
186+
return
187+
}
188+
189+
username := strings.ToLower(makeName("."))
190+
191+
stats, err := runUser(username, ri)
192+
if err != nil {
193+
ri.logger.Error("Cannot simulate user", logr.String("username", username), logr.Err(err))
194+
}
195+
196+
if !ri.quiet {
197+
s := fmt.Sprintf("%s: channels=%d boards=%d cards=%d text=%d remaining=%d\n",
198+
username, stats.ChannelCount, stats.BoardCount, stats.CardCount, stats.TextCount, left)
199+
200+
ri.output.Write(s)
201+
}
202+
}
203+
}
204+
175205
func setUpInterruptHandler(cleanUp func()) {
176-
sig := make(chan os.Signal)
206+
sig := make(chan os.Signal, 1)
177207
signal.Notify(sig, os.Interrupt, syscall.SIGTERM)
178208

179209
go func() {

user.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func runUser(username string, ri *runInfo) (stats, error) {
6060
// create user
6161
user, err := ri.admin.CreateUser(username, ri.cfg.teamID)
6262
if err != nil {
63-
return stats, err
63+
return stats, fmt.Errorf("cannot create user: %w", err)
6464
}
6565

6666
// add user to team
@@ -73,7 +73,7 @@ func runUser(username string, ri *runInfo) (stats, error) {
7373

7474
client, err := NewClient(ri.cfg.SiteURL, user.Username, password)
7575
if err != nil {
76-
return stats, err
76+
return stats, fmt.Errorf("cannot create client: %w", err)
7777
}
7878

7979
// create channels, boards, cards, and content

0 commit comments

Comments
 (0)