Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: the qps feature cannot work well #201

Merged
merged 1 commit into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ jobs:
- name: Unit Test
run: |
make test-all-backend test-operator
- name: Long Test
run: |
make testlong
- name: Report
if: github.actor == 'linuxsuren'
env:
Expand Down
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ plugin-git:
test:
go test ./... -cover -v -coverprofile=coverage.out
go tool cover -func=coverage.out
testlong:
go test pkg/limit/limiter_long_test.go -v
test-ui:
cd console/atest-ui && npm run test:unit
test-e2e:
Expand Down
4 changes: 2 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ You can install in various methods:

* CLI via `hd i atest`
* Web server
* [Kubernetes](https://github.com/LinuxSuRen/api-testing/tree/master/sample/kubernetes)
* [Argo CD](https://github.com/LinuxSuRen/api-testing/blob/master/sample/argocd/simple.yaml)
* [Kubernetes](https://github.com/LinuxSuRen/api-testing/tree/master/docs/manifests/kubernetes)
* [Argo CD](https://github.com/LinuxSuRen/api-testing/blob/master/docs/manifests/argocd/simple.yaml)

If you're developing APIs locally, the best way is installing it as a container service.
Then you can access it via your browser.
Expand Down
File renamed without changes.
26 changes: 11 additions & 15 deletions pkg/limit/limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import (
)

type RateLimiter interface {
TryAccept() bool
Accept()
Accept() bool
Stop()
Burst() int32
}
Expand All @@ -28,43 +27,40 @@ func NewDefaultRateLimiter(qps, burst int32) RateLimiter {
burst = 5
}
limiter := &defaultRateLimiter{
qps: qps,
burst: burst,
singal: make(chan struct{}, 1),
qps: qps,
burst: burst,
singal: make(chan struct{}, 1),
lastToken: time.Now(),
}
go limiter.updateBurst()
return limiter
}

func (r *defaultRateLimiter) TryAccept() bool {
_, ok := r.resver()
return ok
}

func (r *defaultRateLimiter) resver() (delay time.Duration, ok bool) {
delay = time.Now().Sub(r.lastToken) / time.Millisecond
delayRequire := time.Second / time.Duration(r.qps)
r.lastToken = time.Now()
if delay > 0 {
if delay >= delayRequire {
ok = true
} else if r.Burst() > 0 {
r.Setburst(r.Burst() - 1)
ok = true
} else {
delay = time.Second / time.Duration(r.qps)
delay = delayRequire - delay
}
return
}

func (r *defaultRateLimiter) Accept() {
func (r *defaultRateLimiter) Accept() bool {
delay, ok := r.resver()
if ok {
return
return ok
}

if delay > 0 {
time.Sleep(delay)
}
return
return ok
}

func (r *defaultRateLimiter) Setburst(burst int32) {
Expand Down
62 changes: 62 additions & 0 deletions pkg/limit/limiter_long_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//go:build longtest
// +build longtest

/*
MIT License

Copyright (c) 2023 API Testing Authors.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package limit_test

import (
"testing"
"time"

"github.com/linuxsuren/api-testing/pkg/limit"
"github.com/stretchr/testify/assert"
)

func TestLimiterWithLongTime(t *testing.T) {
for i := 0; i < 10; i++ {
testLimiter(t, int32(8+i*2))
}
}

func testLimiter(t *testing.T, count int32) {
t.Log("test limit with count", count)
limiter := limit.NewDefaultRateLimiter(count, 1)
num := int32(0)

loop := true
go func(l limit.RateLimiter) {
for loop {
l.Accept()
num += 1
}
}(limiter)

select {
case <-time.After(time.Second):
loop = false
}
assert.True(t, num <= count+1, num)
}
8 changes: 5 additions & 3 deletions pkg/limit/limiter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (
"github.com/stretchr/testify/assert"
)

func TestXxx(t *testing.T) {
limiter := NewDefaultRateLimiter(1, 1)
func TestLimiter(t *testing.T) {
t.Log("run rate limit test")
limiter := NewDefaultRateLimiter(0, 0)
num := 0

loop := true
Expand All @@ -22,6 +23,7 @@ func TestXxx(t *testing.T) {
select {
case <-time.After(time.Second):
loop = false
limiter.Stop()
}
assert.True(t, num <= 10)
assert.True(t, num <= 10, num)
}
Loading