Skip to content

Commit cdd9dc0

Browse files
authored
Make generated data less blank (#37)
This commit ensures that the data we generate from Ascii and Json has a definite size to it, primarily by selecting from a fixed array of sizes. This is... a little less arbitrary than I would have liked but it's better than a file full of blank lines. Signed-off-by: Brian L. Troutwine <brian@troutwine.us>
1 parent ed568b2 commit cdd9dc0

File tree

5 files changed

+21
-6
lines changed

5 files changed

+21
-6
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "file_gen"
3-
version = "0.3.3"
3+
version = "0.3.4"
44
authors = ["Brian L. Troutwine <brian@troutwine.us>"]
55
edition = "2018"
66
license = "MIT"

src/file.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,11 @@ impl Log {
199199
maximum_bytes_per_file as f64,
200200
&labels
201201
);
202+
gauge!(
203+
"bytes_per_second",
204+
f64::from(self.bytes_per_second.get()),
205+
&labels
206+
);
202207

203208
let mut fp = BufWriter::with_capacity(
204209
ONE_MEBIBYTE * 100,

src/payload/ascii.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::payload::{Error, Serialize};
22
use arbitrary::{self, Arbitrary, Unstructured};
33
use std::io::Write;
44

5+
const SIZES: [usize; 8] = [64, 128, 256, 512, 1024, 2048, 4096, 8192];
56
const CHARSET: &[u8] =
67
b"abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789().,/\\{}[];:'\"";
78
#[allow(clippy::cast_possible_truncation)]
@@ -14,7 +15,9 @@ struct Member {
1415

1516
impl<'a> Arbitrary<'a> for Member {
1617
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
17-
let mut bytes: Vec<u8> = u.arbitrary()?;
18+
let choice: u8 = u.arbitrary()?;
19+
let size = SIZES[(choice as usize) % SIZES.len()];
20+
let mut bytes: Vec<u8> = vec![0; size];
1821
u.fill_buffer(&mut bytes)?;
1922
bytes
2023
.iter_mut()
@@ -23,7 +26,7 @@ impl<'a> Arbitrary<'a> for Member {
2326
}
2427

2528
fn size_hint(_depth: usize) -> (usize, Option<usize>) {
26-
(0, Some(6144)) // 100B to 6KiB
29+
(128, Some(8192))
2730
}
2831
}
2932

src/payload/json.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use crate::payload::{Error, Serialize};
22
use arbitrary::{size_hint, Arbitrary, Unstructured};
33
use std::io::Write;
44

5+
const SIZES: [usize; 13] = [0, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048];
6+
57
/// A simplistic 'Payload' structure without self-reference
68
#[derive(Debug, serde::Serialize)]
79
struct Member {
@@ -17,17 +19,22 @@ struct Member {
1719

1820
impl<'a> Arbitrary<'a> for Member {
1921
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
22+
let choice: u8 = u.arbitrary()?;
23+
let size = SIZES[(choice as usize) % SIZES.len()];
24+
let mut byte_parade: Vec<u8> = vec![0; size];
25+
u.fill_buffer(&mut byte_parade)?;
26+
2027
let member = Member {
2128
id: u.arbitrary()?,
2229
name: u.arbitrary()?,
2330
seed: u.arbitrary()?,
24-
byte_parade: u.arbitrary()?,
31+
byte_parade,
2532
};
2633
Ok(member)
2734
}
2835

2936
fn size_hint(depth: usize) -> (usize, Option<usize>) {
30-
let byte_parade_hint = (0, Some(6144)); // 0 to 6KiB
37+
let byte_parade_hint = (SIZES[0], Some(SIZES[SIZES.len() - 1]));
3138

3239
size_hint::recursion_guard(depth, |depth| {
3340
size_hint::and_all(&[

0 commit comments

Comments
 (0)