1 Star 0 Fork 1

Gitee 极速下载 / ymratelimit

Create your Gitee Account
Explore and code with more than 6 million developers,Free private repositories !:)
Sign up
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/maczam/ymratelimit
This repository doesn't specify license. Without author's permission, this code is only for learning and cannot be used for other purposes.
Clone or download
ratelimit_test.go 1.18 KB
Copy Edit Web IDE Raw Blame History
infi.he authored 2020-06-15 14:18 . first commit
package ymratelimit
import (
"fmt"
"sync"
"sync/atomic"
"testing"
"time"
)
func TestNewTokenBucket(t *testing.T) {
rl := NewTokenBucket(time.Minute, 50) // per second
startTime := time.Now()
wg := sync.WaitGroup{}
count := int32(0)
stop := false
for i := 0; i < 2; i++ {
wg.Add(1)
go func() {
for !stop {
if rl.TakeAvailable() {
atomic.AddInt32(&count, 1)
}
time.Sleep(time.Millisecond * 100)
}
wg.Done()
}()
}
time.Sleep(time.Second * 70)
stop = true
endTime := time.Now()
wg.Wait()
fmt.Println("t2与t1相差:", endTime.Sub(startTime), ";count:", count) //t2与t1相差: 50s
}
func TestNewLeakyBucket(t *testing.T) {
rl := NewLeakyBucket(time.Second, 15) // per second
startTime := time.Now()
wg := sync.WaitGroup{}
count := int32(0)
stop := false
for i := 0; i < 20; i++ {
wg.Add(1)
go func() {
for !stop {
if rl.TakeAvailable() {
atomic.AddInt32(&count, 1)
}
//time.Sleep(time.Millisecond * 100)
}
wg.Done()
}()
}
time.Sleep(time.Second * 5)
stop = true
endTime := time.Now()
wg.Wait()
fmt.Println("t2与t1相差:", endTime.Sub(startTime), ";count:", count) //t2与t1相差: 50s
}

Comment ( 0 )

Sign in for post a comment

1
https://gitee.com/mirrors/ymratelimit.git
git@gitee.com:mirrors/ymratelimit.git
mirrors
ymratelimit
ymratelimit
master

Search