Skip to content

Commit

Permalink
Change interface name: NumOfWorkers
Browse files Browse the repository at this point in the history
  • Loading branch information
otiai10 committed Sep 7, 2023
1 parent 0b8d7bd commit 4118a68
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 20 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,17 @@ type Options struct {
// e.g., You can use embed.FS to copy files from embedded filesystem.
FS fs.FS

// NumberOfWorkers represents the number of workers used for
// NumOfWorkers represents the number of workers used for
// concurrent copying contents of directories.
// If 0 or 1, it does not use goroutine for copying directories.
// Please refer to https://pkg.go.dev/golang.org/x/sync/semaphore for more details.
NumberOfWorkers int64
NumOfWorkers int64

// PreferConcurrent is a function to determine whether or not
// to use goroutine for copying contents of directories.
// If PreferConcurrent is nil, which is default, it does concurrent
// copying for all directories.
// If NumberOfWorkers is 0 or 1, this function will be ignored.
// If NumOfWorkers is 0 or 1, this function will be ignored.
PreferConcurrent func(srcdir, destdir string) (bool, error)
}
```
Expand Down
6 changes: 3 additions & 3 deletions all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,14 +476,14 @@ func (r *SleepyReader) Read(p []byte) (int, error) {
return n, e
}

func TestOptions_NumberOfWorkers(t *testing.T) {
opt := Options{NumberOfWorkers: 3}
func TestOptions_NumOfWorkers(t *testing.T) {
opt := Options{NumOfWorkers: 3}
err := Copy("test/data/case19", "test/data.copy/case19", opt)
Expect(t, err).ToBe(nil)
}

func TestOptions_PreferConcurrent(t *testing.T) {
opt := Options{NumberOfWorkers: 4, PreferConcurrent: func(sd, dd string) (bool, error) {
opt := Options{NumOfWorkers: 4, PreferConcurrent: func(sd, dd string) (bool, error) {
return strings.HasSuffix(sd, "concurrent"), nil
}}
err := Copy("test/data/case19", "test/data.copy/case19_preferconcurrent", opt)
Expand Down
16 changes: 8 additions & 8 deletions benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,33 @@ import (
"testing"
)

func BenchmarkOptions_NumberOfWorkers_0(b *testing.B) {
func BenchmarkOptions_NumOfWorkers_0(b *testing.B) {
var num int64 = 0 // 0 or 1 = single-threaded
opt := Options{NumberOfWorkers: num}
opt := Options{NumOfWorkers: num}
for i := 0; i < b.N; i++ {
Copy("test/data/case19", fmt.Sprintf("test/data.copy/case19-%d-%d", num, i), opt)
}
}

func BenchmarkOptions_NumberOfWorkers_2(b *testing.B) {
func BenchmarkOptions_NumOfWorkers_2(b *testing.B) {
var num int64 = 2
opt := Options{NumberOfWorkers: num}
opt := Options{NumOfWorkers: num}
for i := 0; i < b.N; i++ {
Copy("test/data/case19", fmt.Sprintf("test/data.copy/case19-%d-%d", num, i), opt)
}
}

func BenchmarkOptions_NumberOfWorkers_4(b *testing.B) {
func BenchmarkOptions_NumOfWorkers_4(b *testing.B) {
var num int64 = 4
opt := Options{NumberOfWorkers: num}
opt := Options{NumOfWorkers: num}
for i := 0; i < b.N; i++ {
Copy("test/data/case19", fmt.Sprintf("test/data.copy/case19-%d-%d", num, i), opt)
}
}

func BenchmarkOptions_NumberOfWorkers_8(b *testing.B) {
func BenchmarkOptions_NumOfWorkers_8(b *testing.B) {
var num int64 = 8
opt := Options{NumberOfWorkers: num}
opt := Options{NumOfWorkers: num}
for i := 0; i < b.N; i++ {
Copy("test/data/case19", fmt.Sprintf("test/data.copy/case19-%d-%d", num, i), opt)
}
Expand Down
4 changes: 2 additions & 2 deletions copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ type timespec struct {
// Copy copies src to dest, doesn't matter if src is a directory or a file.
func Copy(src, dest string, opts ...Options) error {
opt := assureOptions(src, dest, opts...)
if opt.NumberOfWorkers > 1 {
opt.intent.sem = semaphore.NewWeighted(opt.NumberOfWorkers)
if opt.NumOfWorkers > 1 {
opt.intent.sem = semaphore.NewWeighted(opt.NumOfWorkers)
opt.intent.ctx = context.Background()
}
if opt.FS != nil {
Expand Down
8 changes: 4 additions & 4 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,17 @@ type Options struct {
// e.g., You can use embed.FS to copy files from embedded filesystem.
FS fs.FS

// NumberOfWorkers represents the number of workers used for
// NumOfWorkers represents the number of workers used for
// concurrent copying contents of directories.
// If 0 or 1, it does not use goroutine for copying directories.
// Please refer to https://pkg.go.dev/golang.org/x/sync/semaphore for more details.
NumberOfWorkers int64
NumOfWorkers int64

// PreferConcurrent is a function to determine whether or not
// to use goroutine for copying contents of directories.
// If PreferConcurrent is nil, which is default, it does concurrent
// copying for all directories.
// If NumberOfWorkers is 0 or 1, this function will be ignored.
// If NumOfWorkers is 0 or 1, this function will be ignored.
PreferConcurrent func(srcdir, destdir string) (bool, error)

// Internal use only
Expand Down Expand Up @@ -161,7 +161,7 @@ func assureOptions(src, dest string, opts ...Options) Options {
}

func shouldCopyDirectoryConcurrent(opt Options, srcdir, destdir string) (bool, error) {
if opt.NumberOfWorkers <= 1 {
if opt.NumOfWorkers <= 1 {
return false, nil
}
if opt.PreferConcurrent == nil {
Expand Down

0 comments on commit 4118a68

Please sign in to comment.