基于 IP 地址分配 A/B 测试变体的最佳实践
我开始在 Grails Web 应用程序中编写一些用于 A/B 测试的代码。我想确保来自同一 IP 地址的请求始终看到相同的变化。不是存储 IP-> 变体的映射,而是通过删除点将 IP 地址简单地转换为整数,然后将其用作随机数生成器的种子,是否可以? Grails 过滤器中发生以下情况:
def ip = request.remoteAddr
def random = new Random(ip.replaceAll(/\./, '').toInteger())
def value = random.nextBoolean()
session.assignment = value
// value should always be the same for a given IP address
我知道通过 IP 地址识别用户并不可靠,我也会使用会话变量/cookie,但这对于我们有新会话的情况似乎很有用,并且没有设置cookie(或者用户禁用了cookie)。
I am starting to write some code for A/B testing in a Grails web application. I want to ensure that requests from the same IP address always see the same variation. Rather than store a map of IP->variant, is it OK to simply turn the IP address into an integer by removing the dots, then use that as the seed for a random number generator? The following is taking place in a Grails Filter:
def ip = request.remoteAddr
def random = new Random(ip.replaceAll(/\./, '').toInteger())
def value = random.nextBoolean()
session.assignment = value
// value should always be the same for a given IP address
I know that identifying users by IP address is not reliable, and I will be using session variables/cookies as well, but this seems to be useful for the case where we have a new session, and no cookies set (or the user has cookies disabled).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以简单地获取 32 位数字并执行 ip mod number_of_test_scenarios。或者使用 ruby 中提供的标准哈希函数。但我觉得我应该指出这种方法的一些问题:
You could simply take the 32-bit number and do ip mod number_of_test_scenarios. Or use a standard hashing function provided in ruby. But I feel I should point out a few problems with this approach: