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
10 changes: 5 additions & 5 deletions cli/random-data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ USAGE
OPTIONS:
-b64
base-64 encode output
-seed int
random seed, 0 or unset for current time
-seed string
random seed, leave empty to use random value
-size int
number of bytes to generate
```

## Examples

```sh
random-data -size=64 -b64
vRujjyEvx8lYiELflaDINvkm5nfueWGCdzEOxhRtz7N2EQjoyrpoMdVVOrwAgNO0tVojDAgu0JpU4hKSsdVl8A==
random-data -size=64 -b64 -seed=hello
ce6rEiwW4jN+E6pCW8UZErXC21sGpkTFuaPEW8VrtayX6nhbbSHOmkFQ1MoHbB2KU2J0ocATYGK4JFEsYNEtlA==
```

Note: Specifying the same seed will produce the same results.
Note: Specifying the same seed will produce the same results. Specifying no ssed procudes random results.
15 changes: 7 additions & 8 deletions cli/random-data/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"flag"
"fmt"
"io"
"math/rand"
"os"

random "github.com/ipfs/go-test/random"
Expand All @@ -29,11 +28,11 @@ OPTIONS:

var (
b64 bool
seed int64
seed string
size int64
)
flag.BoolVar(&b64, "b64", false, "base-64 encode output")
flag.Int64Var(&seed, "seed", 0, "random seed, 0 or unset for current time")
flag.StringVar(&seed, "seed", "", "random seed, leave empty to use random value")
flag.Int64Var(&size, "size", 0, "number of bytes to generate")
flag.Parse()

Expand All @@ -54,12 +53,12 @@ OPTIONS:
}
}

func writeData(seed, size int64, b64 bool) error {
var rnd *rand.Rand
if seed == 0 {
rnd = random.NewRand()
func writeData(seed string, size int64, b64 bool) error {
var rnd *random.Random
if seed == "" {
rnd = random.New()
} else {
rnd = random.NewSeededRand(seed)
rnd = random.NewSeeded(random.StringToSeed(seed))
}

var w io.Writer
Expand Down
99 changes: 49 additions & 50 deletions cli/random-files/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,73 +34,72 @@ OPTIONS:
randomize number of files, from 1 to -files
-random-size
randomize file size, from 1 to -filesize (default true)
-seed int
random seed, 0 for current time
-seed string
random seed, leave empty to use random value
```

## Examples

```sh
> random-files -depth=2 -files=3 -seed=1701 foo
foo/rwd67uvnj9yz-
foo/7vovyvr9
foo/fjv0w0
foo/gyubi50rec5/
foo/gyubi50rec5/vr6x-ce4uupj
foo/gyubi50rec5/ob9ud0e8lt_2e
foo/gyubi50rec5/11gip6zea
foo/nzu5j29-sh-ku4/
foo/nzu5j29-sh-ku4/vcs1629n
foo/nzu5j29-sh-ku4/rky_i_qsxrp
foo/nzu5j29-sh-ku4/xr1usy5ic0
foo/w30dzrx2w4_d/
foo/w30dzrx2w4_d/7ued6
foo/w30dzrx2w4_d/r1d3j
foo/w30dzrx2w4_d/av7d09i-av
foo/s6ha-58/
foo/s6ha-58/nukjsxg7t
foo/s6ha-58/7of_84
foo/s6ha-58/h0jgq8mu1n7u
foo/tq_8/
foo/tq_8/sx-a2jgmz_mk6
foo/tq_8/9hzrksz8
foo/tq_8/8b5swu
foo/mts2ph
foo/wsu46w33-df_v44c
foo/fcuxmunecbs_de
foo/vfpwv_asjhs2nz/
foo/vfpwv_asjhs2nz/8533
foo/vfpwv_asjhs2nz/8nj3jouo1xg_
foo/vfpwv_asjhs2nz/v9awmhr
foo/ujj95a/
foo/ujj95a/n1mf
foo/ujj95a/-taw_yfnc2
foo/ujj95a/h5j-x7rkawl6zc8
foo/v2isuq37wxt/
foo/v2isuq37wxt/0v1od94eojxv
foo/v2isuq37wxt/u4z-gcvoe4w
foo/v2isuq37wxt/ogm2b-coenbu8t2
foo/sn35leocdzm15l/
foo/sn35leocdzm15l/b4w3
foo/sn35leocdzm15l/f73xrgznl57
foo/sn35leocdzm15l/7tt51n7lbvv9
foo/m4ttn58z0x77sbo0/
foo/m4ttn58z0x77sbo0/ydv5ld_4o7tvw7
foo/m4ttn58z0x77sbo0/khdlmwfpegefix
foo/m4ttn58z0x77sbo0/ud2mo
```

It made:

```sh
> tree foo
foo
├── 7vovyvr9
├── fjv0w0
├── gyubi50rec5
│   ├── 11gip6zea
│   ── ob9ud0e8lt_2e
│   └── vr6x-ce4uupj
├── nzu5j29-sh-ku4
│   ├── rky_i_qsxrp
│   ├── vcs1629n
│   └── xr1usy5ic0
├── rwd67uvnj9yz-
├── s6ha-58
│   ├── 7of_84
│   ── h0jgq8mu1n7u
│   └── nukjsxg7t
├── tq_8
│   ├── 8b5swu
│   ── 9hzrksz8
│   └── sx-a2jgmz_mk6
── w30dzrx2w4_d
├── 7ued6
── av7d09i-av
└── r1d3j
├── fcuxmunecbs_de
├── m4ttn58z0x77sbo0
│   ├── khdlmwfpegefix
│   ├── ud2mo
│   ── ydv5ld_4o7tvw7
── mts2ph
├── sn35leocdzm15l
│   ├── 7tt51n7lbvv9
│   ├── b4w3
│   └── f73xrgznl57
├── ujj95a
│   ├── -taw_yfnc2
│   ├── h5j-x7rkawl6zc8
│   ── n1mf
── v2isuq37wxt
│   ├── 0v1od94eojxv
│   ├── ogm2b-coenbu8t2
│   ── u4z-gcvoe4w
── vfpwv_asjhs2nz
│   ├── 8533
│   ├── 8nj3jouo1xg_
│   └── v9awmhr
└── wsu46w33-df_v44c

6 directories, 18 files
```

Note: Specifying the same seed will produce the same results.

Note: Specifying the same seed will produce the same results. Specifying no ssed procudes random results.

### Acknowledgments

Expand Down
2 changes: 1 addition & 1 deletion cli/random-files/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ OPTIONS:
flag.BoolVar(&cfg.RandomDirs, "random-dirs", cfg.RandomDirs, "randomize number of subdirectories, from 1 to -dirs")
flag.BoolVar(&cfg.RandomFiles, "random-files", cfg.RandomFiles, "randomize number of files, from 1 to -files")
flag.BoolVar(&cfg.RandomSize, "random-size", cfg.RandomSize, "randomize file size, from 1 to -filesize")
flag.Int64Var(&cfg.Seed, "seed", cfg.Seed, "random seed, 0 for current time")
flag.StringVar(&cfg.Seed, "seed", "", "random seed, leave empty to use random value")
flag.Parse()

paths = flag.Args()
Expand Down
7 changes: 4 additions & 3 deletions random/doc.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Package random provides functionality to generate pseudo-random test data.
//
// All random numbers and data are created deterministically using a
// pseudo-random number generator. This generator's output is determined by the
// value a seed that us set using the current time, or set explicitly.
// All random numbers and data are created deterministically using the ChaCha8
// pseudo-random number generator. This generator's output is determined by a
// seed value that can be set explicitly. If not set, then cryptographically
// secure random data is used as the seed.
package random
53 changes: 28 additions & 25 deletions random/files/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ import (
"errors"
"fmt"
"io"
"math/rand"
"os"
"path/filepath"

"github.com/ipfs/go-test/random"
)

const (
MinimumNameSize = 4
fileNameAlpha = "abcdefghijklmnopqrstuvwxyz01234567890-_"
MinimumNameSize = 4
fileNameAlphabet = "abcdefghijklmnopqrstuvwxyz0123456789-_"
)

// Config contains settings for creating random files and directories.
Expand Down Expand Up @@ -42,9 +41,9 @@ type Config struct {
// RandomSize specifies whether or not to randomize the file size from 1 to
// the value configured by FileSize.
RandomSize bool
// Seed sets the seen for the random number generator when set to a
// non-zero value.
Seed int64
// Seed sets the seed for the random number generator when set to a
// non-empty value.
Seed string
}

// DefaultConfig returns default settings for creating random files and
Expand Down Expand Up @@ -75,11 +74,11 @@ func Create(cfg Config, roots ...string) error {
return err
}

var rnd *rand.Rand
if cfg.Seed == 0 {
rnd = random.NewRand()
var rnd *random.Random
if cfg.Seed == "" {
rnd = random.New()
} else {
rnd = random.NewSeededRand(cfg.Seed)
rnd = random.NewSeeded(random.StringToSeed(cfg.Seed))
}

for _, root := range roots {
Expand Down Expand Up @@ -128,7 +127,7 @@ func RandomName(sizes ...int) string {
cfg = DefaultConfig()
}

return cfg.randomName(random.NewRand())
return cfg.randomName(random.New())
}

func (cfg *Config) validate() error {
Expand Down Expand Up @@ -165,11 +164,11 @@ func validateNameSize(minSize, maxSize int) error {
return nil
}

func (cfg *Config) writeTree(rnd *rand.Rand, root string, depth int) error {
func (cfg *Config) writeTree(rnd *random.Random, root string, depth int) error {
nFiles := cfg.Files
if nFiles != 0 {
if cfg.RandomFiles && nFiles > 1 {
nFiles = rnd.Intn(nFiles) + 1
nFiles = rnd.IntN(nFiles) + 1
}

for i := 0; i < nFiles; i++ {
Expand All @@ -182,15 +181,15 @@ func (cfg *Config) writeTree(rnd *rand.Rand, root string, depth int) error {
return cfg.writeSubdirs(rnd, root, depth)
}

func (cfg *Config) writeSubdirs(rnd *rand.Rand, root string, depth int) error {
func (cfg *Config) writeSubdirs(rnd *random.Random, root string, depth int) error {
if depth == cfg.Depth {
return nil
}
depth++

nDirs := cfg.Dirs
if cfg.RandomDirs && nDirs > 1 {
nDirs = rnd.Intn(nDirs) + 1
nDirs = rnd.IntN(nDirs) + 1
}

for i := 0; i < nDirs; i++ {
Expand All @@ -202,7 +201,7 @@ func (cfg *Config) writeSubdirs(rnd *rand.Rand, root string, depth int) error {
return nil
}

func (cfg *Config) writeSubdir(rnd *rand.Rand, root string, depth int) error {
func (cfg *Config) writeSubdir(rnd *random.Random, root string, depth int) error {
name := cfg.randomName(rnd)
root = filepath.Join(root, name)
if err := os.MkdirAll(root, 0755); err != nil {
Expand All @@ -216,20 +215,24 @@ func (cfg *Config) writeSubdir(rnd *rand.Rand, root string, depth int) error {
return cfg.writeTree(rnd, root, depth)
}

func (cfg *Config) randomName(rnd *rand.Rand) string {
func (cfg *Config) randomName(rnd *random.Random) string {
sizeDiff := cfg.NameMaxSize - cfg.NameMinSize
n := cfg.NameMinSize
size := cfg.NameMinSize
if sizeDiff != 0 {
n += rnd.Intn(sizeDiff)
size += rnd.IntN(sizeDiff + 1)
}
b := make([]byte, n)
for i := 0; i < n; i++ {
b[i] = fileNameAlpha[rnd.Intn(len(fileNameAlpha))]
if size <= 0 {
return ""
}
return string(b)
src := make([]byte, size)
rnd.Read(src)
for i := range src {
src[i] = fileNameAlphabet[src[i]%byte(len(fileNameAlphabet))]
}
return string(src)
}

func (cfg *Config) writeFile(rnd *rand.Rand, root string) error {
func (cfg *Config) writeFile(rnd *random.Random, root string) error {
name := cfg.randomName(rnd)
filePath := filepath.Join(root, name)
f, err := os.Create(filePath)
Expand All @@ -240,7 +243,7 @@ func (cfg *Config) writeFile(rnd *rand.Rand, root string) error {
if cfg.FileSize > 0 {
fileSize := cfg.FileSize
if cfg.RandomSize && fileSize > 1 {
fileSize = rnd.Int63n(fileSize) + 1
fileSize = rnd.Int64N(fileSize) + 1
}

if _, err := io.CopyN(f, rnd, fileSize); err != nil {
Expand Down
Loading
Loading