Skip to content

Commit

Permalink
Make generated data less blank (#37)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
blt authored Jul 1, 2021
1 parent ed568b2 commit cdd9dc0
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "file_gen"
version = "0.3.3"
version = "0.3.4"
authors = ["Brian L. Troutwine <brian@troutwine.us>"]
edition = "2018"
license = "MIT"
Expand Down
5 changes: 5 additions & 0 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,11 @@ impl Log {
maximum_bytes_per_file as f64,
&labels
);
gauge!(
"bytes_per_second",
f64::from(self.bytes_per_second.get()),
&labels
);

let mut fp = BufWriter::with_capacity(
ONE_MEBIBYTE * 100,
Expand Down
7 changes: 5 additions & 2 deletions src/payload/ascii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::payload::{Error, Serialize};
use arbitrary::{self, Arbitrary, Unstructured};
use std::io::Write;

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

impl<'a> Arbitrary<'a> for Member {
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
let mut bytes: Vec<u8> = u.arbitrary()?;
let choice: u8 = u.arbitrary()?;
let size = SIZES[(choice as usize) % SIZES.len()];
let mut bytes: Vec<u8> = vec![0; size];
u.fill_buffer(&mut bytes)?;
bytes
.iter_mut()
Expand All @@ -23,7 +26,7 @@ impl<'a> Arbitrary<'a> for Member {
}

fn size_hint(_depth: usize) -> (usize, Option<usize>) {
(0, Some(6144)) // 100B to 6KiB
(128, Some(8192))
}
}

Expand Down
11 changes: 9 additions & 2 deletions src/payload/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use crate::payload::{Error, Serialize};
use arbitrary::{size_hint, Arbitrary, Unstructured};
use std::io::Write;

const SIZES: [usize; 13] = [0, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048];

/// A simplistic 'Payload' structure without self-reference
#[derive(Debug, serde::Serialize)]
struct Member {
Expand All @@ -17,17 +19,22 @@ struct Member {

impl<'a> Arbitrary<'a> for Member {
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
let choice: u8 = u.arbitrary()?;
let size = SIZES[(choice as usize) % SIZES.len()];
let mut byte_parade: Vec<u8> = vec![0; size];
u.fill_buffer(&mut byte_parade)?;

let member = Member {
id: u.arbitrary()?,
name: u.arbitrary()?,
seed: u.arbitrary()?,
byte_parade: u.arbitrary()?,
byte_parade,
};
Ok(member)
}

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

size_hint::recursion_guard(depth, |depth| {
size_hint::and_all(&[
Expand Down

0 comments on commit cdd9dc0

Please sign in to comment.