Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create a Rust sample plugin for A/B testing #140

Merged
merged 8 commits into from
Feb 10, 2025

Conversation

pweiber
Copy link
Contributor

@pweiber pweiber commented Dec 12, 2024

Rust version for the A/B testing.

If the hash value is less than or equal to the percentile, the request is served by v2 file. Otherwise, it is served by its original file.

@pweiber pweiber requested a review from a team as a code owner December 12, 2024 14:58
Copy link

snippet-bot bot commented Dec 12, 2024

Here is the summary of changes.

You are about to add 1 region tag.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

# Conflicts:
#	plugins/samples/ab_testing/BUILD
Signed-off-by: pweiber <[email protected]>
fn should_use_v2(user: &str) -> bool {
// Calculate a deterministic hash from user.
// Sum the ASCII values of the characters and then offset by length.
let sum: u64 = user.bytes().map(u64::from).sum();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why we aren't using the default hashing implementation for slices in Rust? https://doc.rust-lang.org/stable/std/hash/trait.Hash.html#method.hash_slice

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did this way because the tests were based on the c++ code, for one of them if I used the default hashing I was getting different values

for the "WithHashAboveThresholdKeepTheSame"

C++ computes std::hash("userAAA") % 100 = 63 (63 > 50 → no redirect).
Rust’s DefaultHasher produces a different value (e.g., hash % 100 ≤ 50 → redirect).

Using like this I'm getting
// For "userAAA": 642 + 21 = 663 → 63%100

Let me know if it is fine to keep it like this or if we should adjust for both

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. Maybe add a comment saying why were are hashing by hand instead of using the rust standard library.

fn should_use_v2(user: &str) -> bool {
// Calculate a deterministic hash from user.
// Sum the ASCII values of the characters and then offset by length.
let sum: u64 = user.bytes().map(u64::from).sum();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. Maybe add a comment saying why were are hashing by hand instead of using the rust standard library.

@pweiber
Copy link
Contributor Author

pweiber commented Feb 6, 2025

@dallen68 Let me know if the comment is fine like that or if we should change something

@dallen68 dallen68 merged commit a1dd3c2 into GoogleCloudPlatform:main Feb 10, 2025
7 checks passed
@pweiber pweiber deleted the rust-ab-testing branch February 10, 2025 17:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants