Skip to content

Commit

Permalink
add --obj.name
Browse files Browse the repository at this point in the history
  • Loading branch information
Anis Eleuch committed Mar 6, 2024
1 parent 37ee3b1 commit 13975f3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions cli/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func newGenSource(ctx *cli.Context, sizeField string) func() generator.Source {
}
opts := []generator.Option{
generator.WithCustomPrefix(ctx.String("prefix")),
generator.WithCustomName(ctx.String("obj.name")),
generator.WithPrefixSize(prefixSize),
}
tokens := strings.Split(ctx.String(sizeField), ",")
Expand Down
5 changes: 4 additions & 1 deletion cli/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ var getFlags = []cli.Flag{
Value: "10MiB",
Usage: "Size of each generated object. Can be a number or 10KiB/MiB/GiB. All sizes are base 2 binary.",
},
cli.StringFlag{
Name: "obj.name",
Usage: "Specify an object name",
},
cli.BoolFlag{
Name: "range",
Usage: "Do ranged get operations. Will request with random offset and length.",
Expand All @@ -51,7 +55,6 @@ var getFlags = []cli.Flag{
Name: "range-size",
Usage: "Use a fixed range size while doing random range offsets with --range",
},

cli.IntFlag{
Name: "versions",
Value: 1,
Expand Down
20 changes: 20 additions & 0 deletions pkg/generator/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package generator

import (
"errors"
"fmt"
"math/rand"
)

Expand All @@ -27,6 +28,7 @@ import (
type Options struct {
src func(o Options) (Source, error)
customPrefix string
customName string
random RandomOpts
csv CsvOpts
minSize int64
Expand All @@ -48,6 +50,16 @@ func (o Options) getSize(rng *rand.Rand) int64 {
return GetExpRandSize(rng, o.minSize, o.totalSize)
}

// getSize will return a size for an object.
func (o Options) getName(counter uint64, rng *rand.Rand) string {
if o.customName != "" {
return o.customName
}
var nBuf [16]byte
randASCIIBytes(nBuf[:], rng)
return fmt.Sprintf("%d.%s.rnd", counter, string(nBuf[:]))
}

func defaultOptions() Options {
o := Options{
src: newRandom,
Expand Down Expand Up @@ -115,6 +127,14 @@ func WithCustomPrefix(prefix string) Option {
}
}

// WithCustomName adds custom object name
func WithCustomName(name string) Option {
return func(o *Options) error {
o.customName = name
return nil
}
}

// WithPrefixSize sets prefix size.
func WithPrefixSize(n int) Option {
return func(o *Options) error {
Expand Down
4 changes: 1 addition & 3 deletions pkg/generator/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,8 @@ func newRandom(o Options) (Source, error) {

func (r *randomSrc) Object() *Object {
atomic.AddUint64(&r.counter, 1)
var nBuf [16]byte
randASCIIBytes(nBuf[:], r.rng)
r.obj.Size = r.o.getSize(r.rng)
r.obj.setName(fmt.Sprintf("%d.%s.rnd", atomic.LoadUint64(&r.counter), string(nBuf[:])))
r.obj.setName(r.o.getName(atomic.LoadUint64(&r.counter), r.rng))

// Reset scrambler
r.obj.Reader = r.buf.Reset(r.obj.Size)
Expand Down

0 comments on commit 13975f3

Please sign in to comment.