Skip to content
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
70 changes: 70 additions & 0 deletions rego_benchmark_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright 2025 alilestera
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package rego_test

import (
"sync"
"testing"
"time"

"github.com/alilestera/rego"
)

var runTimes = 100000

var (
shortTerm = 10
regoCap = 10000
tasks = 100000
)

// demoFunc is used to simulate a task that takes a certain amount of time to complete.
func demoFunc() {
time.Sleep(time.Duration(shortTerm) * time.Millisecond)
}

func BenchmarkRunGoroutines(b *testing.B) {
var wg sync.WaitGroup
for range b.N {
wg.Add(runTimes)
for range runTimes {
go func() {
demoFunc()
wg.Done()
}()
}
wg.Wait()
}
}

func BenchmarkRunRego(b *testing.B) {
var wg sync.WaitGroup
r := rego.New(regoCap)
defer r.ReleaseWait()

b.ResetTimer()
for range b.N {
wg.Add(runTimes)
for range runTimes {
r.Submit(func() {
demoFunc()
wg.Done()
})
}
wg.Wait()
}
}
37 changes: 13 additions & 24 deletions rego_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,18 @@ import (
"github.com/alilestera/rego"
)

var (
shortTerm = 10
regoCap = 10000
n = 100000
)

// demoFunc is used to simulate a task that takes a certain amount of time to complete.
func demoFunc(n int) {
time.Sleep(time.Duration(n) * time.Millisecond)
}

func TestRegoSubmit(t *testing.T) {
defer goleak.VerifyNone(t)

r := rego.New(regoCap)
defer r.ReleaseWait()

var wg sync.WaitGroup
for range n {
for range tasks {
wg.Add(1)
r.Submit(func() {
defer wg.Done()
demoFunc(shortTerm)
demoFunc()
wg.Done()
})
}
wg.Wait()
Expand All @@ -63,13 +52,13 @@ func TestRegoSubmitWait(t *testing.T) {
defer r.ReleaseWait()

var num int
for range n {
for range tasks {
r.SubmitWait(func() {
num++
})
}

assert.Equal(t, n, num)
assert.Equal(t, tasks, num)
}

func TestRegoReleaseCompleteWaiting(t *testing.T) {
Expand All @@ -79,19 +68,19 @@ func TestRegoReleaseCompleteWaiting(t *testing.T) {
var num int32
var wg sync.WaitGroup

for range n {
for range tasks {
wg.Add(1)
go func() {
defer wg.Done()
r.Submit(func() {
atomic.AddInt32(&num, 1)
})
wg.Done()
}()
}
wg.Wait()
r.ReleaseWait()

assert.Equal(t, n, int(num))
assert.Equal(t, tasks, int(num))
}

func TestRegoReleaseIgnoreWaiting(t *testing.T) {
Expand All @@ -101,20 +90,20 @@ func TestRegoReleaseIgnoreWaiting(t *testing.T) {
var num int32
var wg sync.WaitGroup

for range n {
for range tasks {
wg.Add(1)
go func() {
defer wg.Done()
r.Submit(func() {
atomic.AddInt32(&num, 1)
})
wg.Done()
}()
}
wg.Wait()
r.Release()

if r.Waiting() > 0 {
assert.NotEqual(t, n, int(num), "must be some tasks are ignored when waiting queue not empty")
assert.NotEqual(t, tasks, int(num), "must be some tasks are ignored when waiting queue not empty")
}
}

Expand Down Expand Up @@ -143,8 +132,8 @@ func TestRegoWithMinWorkers(t *testing.T) {
for range minimum {
wg.Add(1)
r.Submit(func() {
defer wg.Done()
demoFunc(shortTerm)
demoFunc()
wg.Done()
})
}
wg.Wait()
Expand Down