Skip to content

Commit 40e7c7b

Browse files
committed
graph: Add better docs for 'stress'; add a UsizeMap
1 parent 5c25b9a commit 40e7c7b

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

graph/examples/stress.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,29 @@ impl Template<ValueMap> for ValueMap {
145145
}
146146
}
147147

148+
type UsizeMap = BTreeMap<usize, usize>;
149+
150+
/// Template for testing roughly a GraphQL response, i.e., a `BTreeMap<String, Value>`
151+
impl Template<UsizeMap> for UsizeMap {
152+
type Item = UsizeMap;
153+
154+
fn create(size: usize) -> Self {
155+
let mut map = BTreeMap::new();
156+
for i in 0..size {
157+
map.insert(i * 2, i * 3);
158+
}
159+
map
160+
}
161+
162+
fn sample(&self, size: usize) -> Box<Self::Item> {
163+
Box::new(BTreeMap::from_iter(
164+
self.iter()
165+
.take(size)
166+
.map(|(k, v)| (k.to_owned(), v.to_owned())),
167+
))
168+
}
169+
}
170+
148171
/// Helper to deal with different template objects
149172
struct Cacheable<T> {
150173
cache: LfuCache<usize, T>,
@@ -172,10 +195,14 @@ impl<T: Template<T>> Cacheable<T> {
172195
#[derive(StructOpt)]
173196
#[structopt(name = "stress", about = "Stress test for the LFU Cache")]
174197
struct Opt {
198+
/// Number of cache evictions and insertions
175199
#[structopt(short, long, default_value = "1000")]
176200
niter: usize,
201+
/// Print this many intermediate messages
177202
#[structopt(short, long, default_value = "10")]
178203
print_count: usize,
204+
/// Use objects of size 0 up to this size, chosen unifromly randomly
205+
/// unless `--fixed` is given
179206
#[structopt(short, long, default_value = "1024")]
180207
obj_size: usize,
181208
#[structopt(short, long, default_value = "1000000")]
@@ -184,6 +211,7 @@ struct Opt {
184211
template: String,
185212
#[structopt(short, long)]
186213
samples: bool,
214+
/// Always use objects of size `--obj-size`
187215
#[structopt(short, long)]
188216
fixed: bool,
189217
}
@@ -288,6 +316,8 @@ pub fn main() {
288316
stress::<ValueMap>(&opt);
289317
} else if opt.template == "string" {
290318
stress::<String>(&opt);
319+
} else if opt.template == "usizemap" {
320+
stress::<UsizeMap>(&opt)
291321
} else {
292322
println!("unknown value for --template")
293323
}

0 commit comments

Comments
 (0)