https://github.com/fluuxio/random
Fast Random generators for use from single threaded data injection code
https://github.com/fluuxio/random
go golang performance
Last synced: about 1 month ago
JSON representation
Fast Random generators for use from single threaded data injection code
- Host: GitHub
- URL: https://github.com/fluuxio/random
- Owner: FluuxIO
- License: bsd-3-clause
- Created: 2017-12-27T21:51:13.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-01-10T08:01:47.000Z (over 8 years ago)
- Last Synced: 2025-11-13T17:30:06.420Z (9 months ago)
- Topics: go, golang, performance
- Language: Go
- Homepage:
- Size: 5.86 KB
- Stars: 1
- Watchers: 8
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Random
Fast Random Generator for use from single threaded data injection code. It is especially useful if you
need to load test a service while generating random data. It that case, the random generator locks can
become a bottleneck.
## How to use
You first need to create a `RandomUnsafe` generator:
```go
r := random.NewRandomUnsafe()
```
You will need to keep it as a reference. Please not that it is only safe to use from a single thread, but you can
generate several `RandomUnsafe` values at the beginning of different Goroutines.
You can then call one of the available method to generate specific values.
For example, to generate an Int:
```go
r.Intn(100)
```
To generate a variable length string between 15 and 20 characters:
```go
r.String(15, 25)
```
## Benchmark
Here are a few example showing the gain
```bash
$ go test -bench .
goos: darwin
goarch: amd64
pkg: fluux.io/random
BenchmarkRandString-4 2000000 808 ns/op
BenchmarkRandomString-4 20000000 56.5 ns/op
BenchmarkRandId-4 1000000 1665 ns/op
BenchmarkRandomId-4 1000000 1053 ns/op
BenchmarkRandInt-4 50000000 32.0 ns/op
BenchmarkRandomInt-4 100000000 23.2 ns/op
PASS
ok fluux.io/random 10.386s
```