Skip to content

Commit

Permalink
Add SRM test example
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkBiesheuvel committed Nov 11, 2024
1 parent 4bae73f commit 3909a53
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
15 changes: 15 additions & 0 deletions examples/srm-test/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "srm-test"
version = "0.1.0"
edition = "2021"

[dependencies]
rand = "0.8.5"

[dependencies.optimizely]
path = "../../optimizely"
features = ["online"]

[dependencies.uuid]
version = "1.3.0"
features = ["v4", "fast-rng"]
41 changes: 41 additions & 0 deletions examples/srm-test/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use optimizely::{decision::DecideOptions, Client};
use std::{collections::HashMap, error::Error, ops::AddAssign};
use uuid::Uuid;

const FILE_PATH: &str = "../../datafiles/sandbox.json";
const FLAG_KEY: &str = "buy_button";

fn main() -> Result<(), Box<dyn Error>> {
let client = Client::from_local_datafile(FILE_PATH)?.initialize();

// Do not send any decision events during SRM testing
let decide_options = DecideOptions {
disable_decision_event: true,
..DecideOptions::default()
};

// Create counter
let mut counter: HashMap<String, usize> = HashMap::new();

// Run a two million times
for _ in 0..2_000_000 {
// Generate visitor ID using UUIDv4
let user_id = Uuid::new_v4().as_hyphenated().to_string();

// Get variation key
let variation_key = client.create_user_context(&user_id)
.decide_with_options(FLAG_KEY, &decide_options)
.variation_key()
.to_string();

// Count variation
counter.entry(variation_key).or_insert(0).add_assign(1);
}

// Dump counter to stdout
for (key, total) in counter {
println!("Variation '{}' had {} visitors", key, total);
}

Ok(())
}

0 comments on commit 3909a53

Please sign in to comment.