diff --git a/Cargo.lock b/Cargo.lock index f43c2fa..cd22f3a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -143,6 +143,16 @@ dependencies = [ "generic-array", ] +[[package]] +name = "bloomfilter" +version = "3.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f6d7f06817e48ea4e17532fa61bc4e8b9a101437f0623f69d2ea54284f3a817" +dependencies = [ + "getrandom 0.2.16", + "siphasher", +] + [[package]] name = "bumpalo" version = "3.19.0" @@ -180,6 +190,7 @@ checksum = "9555578bc9e57714c812a1f84e4fc5b4d21fcb063490c624de019f7464c91268" name = "client" version = "0.1.0" dependencies = [ + "bloomfilter", "rand 0.9.1", "reqwest", "serde", @@ -505,8 +516,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "335ff9f135e4384c8150d6f27c6daed433577f86b4750418338c01a1a2528592" dependencies = [ "cfg-if", + "js-sys", "libc", "wasi 0.11.1+wasi-snapshot-preview1", + "wasm-bindgen", ] [[package]] @@ -1617,6 +1630,12 @@ dependencies = [ "rand_core 0.6.4", ] +[[package]] +name = "siphasher" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56199f7ddabf13fe5074ce809e7d3f42b42ae711800501b5b16ea82ad029c39d" + [[package]] name = "slab" version = "0.4.10" diff --git a/client/Cargo.toml b/client/Cargo.toml index 2776a63..c572fe6 100644 --- a/client/Cargo.toml +++ b/client/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" edition = "2024" [dependencies] +bloomfilter = "3.0.1" rand = "0.9.1" reqwest = { version = "0.12.22", features = ["json"] } serde = { version = "1.0.219", features = ["derive"] } diff --git a/client/src/main.rs b/client/src/main.rs index 0fcf15d..315cd7f 100644 --- a/client/src/main.rs +++ b/client/src/main.rs @@ -1,8 +1,11 @@ -use rand::distr::Alphabetic; +use bloomfilter::Bloom; +use rand::distr::{Alphabetic, Alphanumeric, SampleString}; use rand::prelude::*; use reqwest::{Method, Request, Url}; use serde::Deserialize; +use serde::de::value::BoolDeserializer; use std::collections::HashMap; +use std::hash::{DefaultHasher, Hash, Hasher}; use std::time::Duration; #[derive(Deserialize, Debug)] @@ -19,23 +22,30 @@ async fn main() { .build() .unwrap(); - const users_url: &str = "http://localhost:9000/api/v1/user"; + const USERS_URL: &str = "http://localhost:9000/api/v1/user"; let mut rng = rand::rng(); let mut ids = Vec::new(); + let mut bf = Bloom::new_for_fp_rate(1000000usize, 0.0001f64).unwrap(); - while true { - let uesrname: String = (1..rng.random_range(1..20)) - .into_iter() - .map(|_| rand::rng().sample(rand::distr::Alphabetic) as char) - .collect(); - let email = format!("{}@uocat.com", uesrname); - let mut map = HashMap::new(); - map.insert("name", uesrname); - map.insert("email", email); + loop { + // let username: String = (4..rng.random_range(4..20)) + // .into_iter() + // .map(|_| rand::rng().sample(rand::distr::Alphabetic) as char) + // .collect(); + let username_len = rng.random_range(4..20); + let username: String = Alphanumeric.sample_string(&mut rng, username_len); + + if bf.check(&username) { + println!("Skip {username}"); + continue; + } + + let email = format!("{}@uocat.com", username); + let map = HashMap::from([("name", username.clone()), ("email", email)]); // send user request - let res = client.put(users_url).json(&map).send().await; + let res = client.put(USERS_URL).json(&map).send().await; if let Ok(res) = res { if res.status() == reqwest::StatusCode::OK { @@ -44,6 +54,7 @@ async fn main() { println!("Inserted {:?}", user); } } + bf.set(&username); } } }