Skip to content

Commit

Permalink
jobs benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Feb 22, 2022
1 parent 43bcd79 commit 7f558b9
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 0 deletions.
39 changes: 39 additions & 0 deletions internal/scheduler/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,45 @@ func TestScheduler_basic(t *testing.T) {
}
}

func BenchmarkScheduler_EnqueueAndWaitForJob(b *testing.B) {
ss, err := state.NewStateStore()
if err != nil {
b.Fatal(err)
}

tmpDir := b.TempDir()
ctx := context.Background()

s := NewScheduler(&closedDirJobs{js: ss.JobStore}, 1)
s.Start(ctx)
b.Cleanup(func() {
s.Stop()
})

ids := make(job.IDs, 0)
for i := 0; i < b.N; i++ {
i := i
dirPath := filepath.Join(tmpDir, fmt.Sprintf("folder-%d", i))

newId, err := ss.JobStore.EnqueueJob(job.Job{
Func: func(c context.Context) error {
return nil
},
Dir: document.DirHandleFromPath(dirPath),
Type: "test-type",
})
if err != nil {
b.Fatal(err)
}
ids = append(ids, newId)
}

err = ss.JobStore.WaitForJobs(ctx, ids...)
if err != nil {
b.Fatal(err)
}
}

func TestScheduler_defer(t *testing.T) {
ss, err := state.NewStateStore()
if err != nil {
Expand Down
99 changes: 99 additions & 0 deletions internal/state/jobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ package state
import (
"context"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"sort"
"testing"
"time"
Expand Down Expand Up @@ -59,6 +63,101 @@ func TestJobStore_EnqueueJob(t *testing.T) {
}
}

func BenchmarkJobStore_EnqueueJob_basic(b *testing.B) {
ss, err := NewStateStore()
if err != nil {
b.Fatal(err)
}

tmpDir := b.TempDir()

for i := 0; i < b.N; i++ {
i := i
dirPath := filepath.Join(tmpDir, fmt.Sprintf("folder-%d", i))

_, err := ss.JobStore.EnqueueJob(job.Job{
Func: func(c context.Context) error {
return nil
},
Dir: document.DirHandleFromPath(dirPath),
Type: "test-type",
})
if err != nil {
b.Fatal(err)
}
}

ids, err := ss.JobStore.ListQueuedJobs()
if err != nil {
b.Fatal(err)
}

if len(ids) != b.N {
b.Fatalf("expected %d jobs, %d given", b.N, len(ids))
}
}

func TestJobStore_EnqueueJob_verify(t *testing.T) {
ss, err := NewStateStore()
if err != nil {
t.Fatal(err)
}
ss.SetLogger(testLogger())

tmpDir := t.TempDir()

jobCount := 50

for i := 0; i < jobCount; i++ {
i := i
dirPath := filepath.Join(tmpDir, fmt.Sprintf("folder-%d", i))

_, err := ss.JobStore.EnqueueJob(job.Job{
Func: func(c context.Context) error {
return nil
},
Dir: document.DirHandleFromPath(dirPath),
Type: "test-type",
})
if err != nil {
t.Fatal(err)
}
}

ids, err := ss.JobStore.ListQueuedJobs()
if err != nil {
t.Fatal(err)
}

if len(ids) != jobCount {
t.Fatalf("expected %d jobs, %d given", jobCount, len(ids))
}

for _, id := range ids {
err := ss.JobStore.FinishJob(id, nil)
if err != nil {
t.Error(err)
}
}

ids, err = ss.JobStore.allJobs()
if err != nil {
t.Fatal(err)
}

if len(ids) != 0 {
t.Fatalf("expected %d jobs, %d given", 0, len(ids))
}
}

func testLogger() *log.Logger {
if testing.Verbose() {
return log.New(os.Stdout, "", log.LstdFlags|log.Lshortfile)
}

return log.New(ioutil.Discard, "", 0)
}

func TestJobStore_DequeueJobsForDir(t *testing.T) {
ss, err := NewStateStore()
if err != nil {
Expand Down

0 comments on commit 7f558b9

Please sign in to comment.