|
| 1 | +package gocql |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "sync" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +const queries = 100 |
| 10 | + |
| 11 | +const skipRateLimiterTestMsg = "Skipping rate limiter test, due to limit of simultaneously alive goroutines. Should be tested locally" |
| 12 | + |
| 13 | +func TestRateLimiter50k(t *testing.T) { |
| 14 | + t.Skip(skipRateLimiterTestMsg) |
| 15 | + fmt.Println("Running rate limiter test with 50_000 workers") |
| 16 | + RunRateLimiterTest(t, 50_000) |
| 17 | +} |
| 18 | + |
| 19 | +func TestRateLimiter100k(t *testing.T) { |
| 20 | + t.Skip(skipRateLimiterTestMsg) |
| 21 | + fmt.Println("Running rate limiter test with 100_000 workers") |
| 22 | + RunRateLimiterTest(t, 100_000) |
| 23 | +} |
| 24 | + |
| 25 | +func TestRateLimiter200k(t *testing.T) { |
| 26 | + t.Skip(skipRateLimiterTestMsg) |
| 27 | + fmt.Println("Running rate limiter test with 200_000 workers") |
| 28 | + RunRateLimiterTest(t, 200_000) |
| 29 | +} |
| 30 | + |
| 31 | +func RunRateLimiterTest(t *testing.T, workerCount int) { |
| 32 | + cluster := createCluster() |
| 33 | + cluster.RateLimiterConfig = &RateLimiterConfig{ |
| 34 | + rate: 350000, |
| 35 | + burst: 150, |
| 36 | + } |
| 37 | + |
| 38 | + session := createSessionFromCluster(cluster, t) |
| 39 | + defer session.Close() |
| 40 | + |
| 41 | + execRelease(session.Query("drop keyspace if exists pargettest")) |
| 42 | + execRelease(session.Query("create keyspace pargettest with replication = {'class' : 'SimpleStrategy', 'replication_factor' : 1}")) |
| 43 | + execRelease(session.Query("drop table if exists pargettest.test")) |
| 44 | + execRelease(session.Query("create table pargettest.test (a text, b int, primary key(a))")) |
| 45 | + execRelease(session.Query("insert into pargettest.test (a, b) values ( 'a', 1)")) |
| 46 | + |
| 47 | + var wg sync.WaitGroup |
| 48 | + |
| 49 | + for i := 1; i <= workerCount; i++ { |
| 50 | + wg.Add(1) |
| 51 | + |
| 52 | + go func() { |
| 53 | + defer wg.Done() |
| 54 | + for j := 0; j < queries; j++ { |
| 55 | + iterRelease(session.Query("select * from pargettest.test where a='a'")) |
| 56 | + } |
| 57 | + }() |
| 58 | + } |
| 59 | + |
| 60 | + wg.Wait() |
| 61 | +} |
| 62 | + |
| 63 | +func iterRelease(query *Query) { |
| 64 | + _, err := query.Iter().SliceMap() |
| 65 | + if err != nil { |
| 66 | + println(err.Error()) |
| 67 | + panic(err) |
| 68 | + } |
| 69 | + query.Release() |
| 70 | +} |
| 71 | + |
| 72 | +func execRelease(query *Query) { |
| 73 | + if err := query.Exec(); err != nil { |
| 74 | + println(err.Error()) |
| 75 | + panic(err) |
| 76 | + } |
| 77 | + query.Release() |
| 78 | +} |
0 commit comments