This repository was archived by the owner on Jul 30, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Random data produce #64
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ae464a6
remove unused payload variable
dotnwat 04bdb05
split out separate data generation tasks
dotnwat 8ba466a
produce random data in non-compressible mode
dotnwat 077b26d
add benchmakr for old and new random gen
dotnwat 4ef032a
use faster random data generator
dotnwat 104fd4d
add option for real random bytes
dotnwat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| package worker | ||
|
|
||
| import ( | ||
| "math/rand" | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| type state struct { | ||
| data []byte | ||
| } | ||
|
|
||
| func newstate(size int) state { | ||
| data := make([]byte, size) | ||
| for i := range data { | ||
| // printable ascii range | ||
| data[i] = byte(rand.Intn(126+1-32) + 32) | ||
| } | ||
| return state{ | ||
| data: data, | ||
| } | ||
| } | ||
|
|
||
| func (s *state) generate(size, frag_size int) []byte { | ||
| res := make([]byte, 0, size) | ||
| for { | ||
| remaining := size - len(res) | ||
| if remaining == 0 { | ||
| break | ||
| } | ||
| start := rand.Intn(size) | ||
| view := s.data[start:] | ||
| end := min(len(view), remaining, frag_size) | ||
| res = append(res, view[:end]...) | ||
| } | ||
| return res | ||
| } | ||
|
|
||
| func benchmark_old_random_payload(i int, b *testing.B) { | ||
| for n := 0; n < b.N; n++ { | ||
| randBytes := make([]byte, i) | ||
| // An incompressible high entropy payload. This will likely not be UTF-8 decodable. | ||
| n, err := rand.Read(randBytes) | ||
| if err != nil { | ||
| panic(err.Error()) | ||
| } | ||
| if n != int(i) { | ||
| panic("Unexpected byte count from rand.Read") | ||
| } | ||
| // Convert to a valid UTF-8 string, replacing bad chars with " ". | ||
| // A valid UTF-8 string is needed to avoid any decoding issues | ||
| // for services on the consuming end. | ||
| payload := []byte(strings.ToValidUTF8(string(randBytes), " ")) | ||
|
|
||
| // In converting to valid UTF-8, we may have lost some bytes. | ||
| // Append back the difference. | ||
| diff := int(i) - len(payload) | ||
| if diff > 0 { | ||
| payload = append(payload, make([]byte, diff)...) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func benchmark_random_payload(i int, b *testing.B) { | ||
| s := newstate(1 << 22) | ||
| b.ResetTimer() | ||
| for n := 0; n < b.N; n++ { | ||
| s.generate(i, 1024) | ||
| } | ||
| } | ||
|
|
||
| func benchmark_empty_payload(i int, b *testing.B) { | ||
| gen := func() []byte { | ||
| return make([]byte, i) | ||
| } | ||
| for n := 0; n < b.N; n++ { | ||
| gen() | ||
| } | ||
| } | ||
|
|
||
| func Benchmark_old_random_payload1(b *testing.B) { benchmark_old_random_payload(10, b) } | ||
| func Benchmark_old_random_payload2(b *testing.B) { benchmark_old_random_payload(100, b) } | ||
| func Benchmark_old_random_payload3(b *testing.B) { benchmark_old_random_payload(1000, b) } | ||
| func Benchmark_old_random_payload10(b *testing.B) { benchmark_old_random_payload(10000, b) } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: i know this is copypasta from elsewhere but if we're going to check this in we might as well make the Benchmark function names sensible (10,20,40 should be 4, 5, 6 to indicate number of zeros) |
||
| func Benchmark_old_random_payload20(b *testing.B) { benchmark_old_random_payload(100000, b) } | ||
| func Benchmark_old_random_payload40(b *testing.B) { benchmark_old_random_payload(1000000, b) } | ||
|
|
||
| func Benchmark_random_payload1(b *testing.B) { benchmark_random_payload(10, b) } | ||
| func Benchmark_random_payload2(b *testing.B) { benchmark_random_payload(100, b) } | ||
| func Benchmark_random_payload3(b *testing.B) { benchmark_random_payload(1000, b) } | ||
| func Benchmark_random_payload10(b *testing.B) { benchmark_random_payload(10000, b) } | ||
| func Benchmark_random_payload20(b *testing.B) { benchmark_random_payload(100000, b) } | ||
| func Benchmark_random_payload40(b *testing.B) { benchmark_random_payload(1000000, b) } | ||
|
|
||
| func Benchmark_empty_payload1(b *testing.B) { benchmark_empty_payload(10, b) } | ||
| func Benchmark_empty_payload2(b *testing.B) { benchmark_empty_payload(100, b) } | ||
| func Benchmark_empty_payload3(b *testing.B) { benchmark_empty_payload(1000, b) } | ||
| func Benchmark_empty_payload10(b *testing.B) { benchmark_empty_payload(10000, b) } | ||
| func Benchmark_empty_payload20(b *testing.B) { benchmark_empty_payload(100000, b) } | ||
| func Benchmark_empty_payload40(b *testing.B) { benchmark_empty_payload(1000000, b) } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any good way to reuse the definition in valueGenerator? not a huge deal if not
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i dunno. just choosing my battles