测试nginx网站代码

by Nitish Phanse

由Nitish Phanse

在40行以下代码中使用NGINX进行A / B测试 (A/B testing with NGINX in under 40 lines of code)

A/B Testing, has enabled designers and product managers to get a deep insight into user behavioral patterns.

A / B测试使设计师和产品经理可以深入了解用户的行为模式。

On the one hand, it has allowed product managers more flexibility while conceptualizing user journeys. But on the other, it’s become a developer’s nightmare, being told to make two versions of the same component.

一方面,它使产品经理在概念化用户旅程的同时具有更大的灵活性。 但另一方面,它被告知要制作同一组件的两个版本,这已成为开发人员的噩梦。

“The general concept behind A/B testing is to create an experiment with a control group and one or more experimental groups (called “cells” within Netflix) which receive alternative treatments. Each member belongs exclusively to one cell within a given experiment, with one of the cells always designated the “default cell”. This cell represents the control group, which receives the same experience as all Netflix members not in the test.”

“ A / B测试背后的一般概念是与对照组和一个或多个接受替代治疗的实验组(在Netflix中称为“细胞”)建立实验。 每个成员在给定实验中仅属于一个单元,其中一个单元始终指定为“默认单元”。 这个单元代表对照组,与未参加测试的所有Netflix成员一样,他们将获得相同的体验。”

- Netflix blog

-Netflix博客

当前的生态系统能提供什么? (What does the present ecosystem offer?)

A lot of companies like Mixpanel, VWO, and Optimisely provide client SDKs (JavaScript code) which have to be added in the head tag of the page HTML. The tests can then be created via a dashboard.

很多公司(例如Mixpanel,VWO和Optimizely)都提供了客户端SDK(JavaScript代码),这些必须添加到页面HTML的head标签中。 然后可以通过仪表板创建测试。

Although the above methods give you a lot of options when it comes to button colors, component height, and other CSS attributes, it doesn’t really allow you to create two separate flows altogether.

尽管上述方法在按钮颜色,组件高度和其他CSS属性方面为您提供了很多选择,但实际上并不允许您完全创建两个单独的流程。

Also, some external libraries can really hamper your page load times and can create a jittery/laggy experience for users.

此外,某些外部库确实会妨碍您的页面加载时间,并可能给用户带来不安/缓慢的体验。

介绍NGINX (Presenting NGINX)

Nginx is a light web server that offers a bunch of functionality like load balancing, reverse proxying, and HTML compression. It’s easy to setup and really offers a lot of control to developers.

Nginx是一个轻量级的Web服务器,提供了很多功能,例如负载平衡,反向代理和HTML压缩。 它易于设置,确实为开发人员提供了很多控制。

NGINX is a terrific tool for distributing traffic for split tests.

NGINX是用于分配流量以进行拆分测试的绝佳工具。

It’s stable, it’s blazingly fast, and configurations for typical use cases are prevalent online. More complex configuration can be accomplished after just a couple hours exploring the documentation. Small companies may not have resources to spend on paid software for A/B testing, but NGINX provides an option to carry out some form of A/B testing.

它稳定,速度极快,典型用例的配置在网上很普遍。 在浏览文档仅几个小时之后,即可完成更复杂的配置。 小型公司可能没有资源来购买用于A / B测试的付费软件,但是NGINX提供了进行某种形式的A / B测试的选项。

For example, say you want to see which of the forms below will have better conversion:

举例来说,假设您想查看下列哪种表格的转换效果更好:

Your hypothesis might be that fewer form fields imply less data being inputted by the user, thus leading to more conversions.

您的假设可能是更少的表单字段意味着用户输入的数据更少,从而导致更多的转换。

So we can define two buckets: Version A and Version B. The former is the control group, which is shown to 80% of the traffic. The latter is the test group, which forms the remaining 20% of the traffic.

因此,我们可以定义两个存储桶:版本A 和版本B。前者是对照组,显示为80%的流量。 后者是测试组,占剩余流量的20%。

Port 7770 will host one bucket of the code, whereas port 7777 will host the second bucket of the code.

端口7770将托管代码的一个存储桶,而端口7777将托管代码的第二个存储桶。

码 (Code)

Your nginx.conf file can be modified as shown below.

您的nginx.conf文件可以如下所示进行修改。

http {    # ...    # application version 1a    upstream version_a {        server server 127.0.0.1:7770; ## Can be an external ip too    }
# application version 1b    upstream version_b {        server server 127.0.0.1:7777; ## Can be an external ip too    }
split_clients "app${remote_addr}${http_user_agent}${date_gmt}"   $appversion {        80%     version_1a;        *       version_1b;    }
server {        # ...        listen 80;        location / {            proxy_set_header Host $host;            proxy_pass http://$appversion;        }    }}

Create two upstreams, one for each bucket.

创建两个上游,每个存储桶一个。

The split_client directive helps us divert traffic along with the specified weightage to a particular upstream.

split_client 指令可帮助我们将流量和指定的权重转移到特定的上游。

The app${remote_addr}${http_user_agent}${date_gmt}appversion creates a hash based on the above parameters and is used by nginx to log a request made to either bucket. Preferably these parameters are those which are pertaining solely to a user, like user_agent, remote addr.

app${remote_addr}${http_user_agent}${date_gmt}appversion 根据上述参数创建一个哈希,nginx使用该哈希记录对任一存储桶发出的请求。 优选地,这些参数是仅与用户有关的那些参数,例如user_agentremote addr

Ok — so this will work, but it doesn’t give the user a persistent experience.

好的-这样就可以了,但是并不能给用户持久的体验。

If I refresh my page, there is a chance I’ll switch between buckets, and this can be a horrid user experience.

如果刷新页面,则有可能在存储桶之间切换,这可能会带来可怕的用户体验。

Consider the above case: imagine trying to fill a six field form, and then suddenly, on refreshing, seeing a two field form. Confusing!

考虑上面的情况:假设尝试填写一个六字段表单,然后在刷新时突然看到一个两字段表单。 令人困惑!

不同的方法 (A different approach)

  1. Proxy pass request to either bucket代理通过请求到任一存储桶
  2. Set a cookie with an expiration time equal to duration of test.设置cookie的有效时间等于测试持续时间。
  3. Check for cookie existence and proxy pass to correct bucket to ensure a uniform user experience.检查cookie的存在和代理传递以更正存储桶,以确保统一的用户体验。

We will use NGINX’s map directive and map the $cookie_name variable to different buckets that we have created.

我们将使用NGINX的map指令并映射$cookie_name 变量以创建不同的存储桶。

http {    # ...    # application version a    upstream version_a {        server server 127.0.0.1:7770; ## Can be an external ip too    }
# application version b    upstream version_b {        server server 127.0.0.1:7777; ## Can be an external ip too    }    split_clients "app${remote_addr}${http_user_agent}${date_gmt}"     $appversion {        80%     version_a;        *       version_b;    }
map $cookie_split_test_version $upstream_group {        default $appversion;        "version_a" "version_a";        "version_b" "version_b";    }
server {        # ...        listen 80;        location / {            add_header Set-Cookie "split_test_version=$upstream_group;Path=/;Max-Age=518400;";
proxy_set_header Host $host;
if ($upstream_group = "version_a") {                proxy_pass http://127.0.0.1:7777;                break;            }
if ($upstream_group = "version_b") {                proxy_pass http://127.0.0.1:7770;                break;            }
proxy_pass http://$appversion;        }    }}

As it’s a little hard to format the above code…

由于格式化上面的代码有点困难……

结论 (Conclusion)

  1. NGINX provides a very simple API to create an A/B test environment.NGINX提供了一个非常简单的API来创建A / B测试环境。
  2. Allows for multiple buckets to be created. The example above shows two buckets, but we can split traffic and create more buckets.允许创建多个存储桶。 上面的示例显示了两个存储桶,但我们可以拆分流量并创建更多存储桶。
  3. As the same code is hosted on two ports, deployment can become tricky (presently I have two branches: a master and a test branch), whether done off a different branch or from the same one.由于将相同的代码托管在两个端口上,因此部署可能会变得很棘手(目前我有两个分支:一个主分支和一个测试分支),无论是在其他分支上进行还是从同一分支进行。
  4. Carrying more than one A/B test can become tricky. Yes, you can use the location directive and set different cookies based on the required tests, but having two tests (Test 1, Control: 80, Test 20 & Test 2 Control: 50, Test 50) is impossible. That said, you should not have more than one A/B test at a time per page. Otherwise you will end up having 2^n versions of your page, where n is the number of tests, and tracking conversions will be hell.

    进行多个A / B测试可能会很棘手。 是的,您可以使用location指令并根据所需的测试设置不同的cookie,但是不可能有两个测试( Test 1,Control:80,Test 20&Test 2 Control:50,Test 50 )。 就是说,您每页一次不能进行多个A / B测试。 否则,您最终将拥有2 ^ n个页面版本,其中n是测试数量,并且跟踪转换将是地狱。

  5. Tracking can now be done at a very granular level as the code bases are effectively separate.由于代码库是有效分离的,因此现在可以在非常精细的级别上进行跟踪。

Do let me know if I’ve made any mistake in the above. Happy to correct and learn. Hope you liked the article.

如果我在上面有任何错误,请告诉我。 乐于纠正和学习。 希望您喜欢这篇文章。

PS: Did anyone notice it is less than 40 lines of code?!

PS:有没有人注意到它少于40行代码?

翻译自: https://www.freecodecamp.org/news/a-b-testing-with-nginx-in-40-lines-of-code-d4f94397130a/

测试nginx网站代码

测试nginx网站代码_在40行以下代码中使用NGINX进行A / B测试相关推荐

  1. 聚类 python 代码_不足 20 行 Python 代码,高效实现 k-means 均值聚类算法

    下载好向圈APP可以快速联系圈友 您需要 登录 才可以下载或查看,没有帐号?立即注册 x 不足 20 行 Python 代码,高效实现 k-means 均值聚类算法-1.jpg (143.81 KB, ...

  2. 50行python游戏代码_使用50行Python代码从零开始实现一个AI平衡小游戏

    使用50行Python代码从零开始实现一个AI平衡小游戏 发布时间:2020-10-23 09:26:14 来源:脚本之家 阅读:74 集智导读: 本文会为大家展示机器学习专家 Mike Shi 如何 ...

  3. 简单20行python代码_用 20 行 python 代码实现人脸识别!Python实现就是这么简单!...

    今天给大家介绍一个世界上最简洁的人脸识别库 face_recognition,你可以使用 Python 和命令行工具进行提取.识别.操作人脸. 基于业内领先的 C++ 开源库 dlib 中的深度学习模 ...

  4. python秒表游戏代码_用20行Python代码实现2048小游戏,你会吗?

    前些天在b站上看到有个大佬用c写了一个2048小游戏,我便一下来了兴趣.心想着,我貌似也能用Python来整一波,话不多说,直接开搞. 2048的游戏规则: 2048游戏总共有16个格子,初始时会有两 ...

  5. python画人脸代码_[转]7行Python代码的人脸识别

    随着去年alphago 的震撼表现,AI 再次成为科技公司的宠儿.AI涉及的领域众多,图像识别中的人脸识别是其中一个有趣的分支.百度的BFR,Face++的开放平台,汉王,讯飞等等都提供了人脸识别的A ...

  6. 服不服?40行Python代码,实现卷积特征可视化

    点击上方"小白学视觉",选择加"星标"或"置顶" 重磅干货,第一时间送达 本文转自|深度学习这件小事 卷积神经网络(CNN)变革了计算机视觉 ...

  7. 40行Python代码,实现卷积特征可视化

    最近在阅读 Jeremy Rifkin 的书<The End of Work>时,我读到一个有趣的关于 AI 的定义.Rifkin 写到:「今天,当科学家们谈论人工智能时,他们通常是指『一 ...

  8. python爬虫教程:如何用40行Python代码实现天气预报和每日鸡汤推送功能

    这篇文章主要介绍了通过40行Python代码实现天气预报和每日鸡汤推送功能,代码简单易懂,非常不错具有一定的参考借鉴价值 ,需要的朋友可以参考下 情人节刚过去几天,但是这和我们程序员有什么关系呢,对我 ...

  9. python博弈论代码_使用 40 多行的 Python 代码实现一个简单的演化过程

    Python部落(python.freelycode.com)组织翻译,禁止转载,欢迎转发. 在纳米比亚的 PyCon 会议上,我发表了一篇名为 <使用 Python 解决"升级版的剪 ...

最新文章

  1. Android打Path的方法
  2. 牛人总结python中string模块各属性以及函数的用法,果断转了,好东西
  3. JAVA控件属性列表_Gradle获取项目属性列表
  4. sprintf函数的格式化字符串_尚学堂百战程序员:Python 字符串处理
  5. 大数运算(7)——大数阶乘(求阶乘)
  6. java input属性_如何在h:inputText中指定命令属性?
  7. SAP CRM Product Relationship的设计原理
  8. Linux中如何让进程在后台运行
  9. 使用数字滤波器处理音频噪声(附Matlab程序)
  10. 沪深300指数的跟踪基金排名
  11. 轻量级kali虚拟机
  12. 感谢同事的临别赠言,愿自己一路顺风。
  13. ubuntu--制作图标
  14. 运动耳机排行榜10强,运动人士必备的几款运动耳机分享
  15. ULONG64转CString
  16. 斗鱼App产品体验报告
  17. Excel汉字转全拼以及首字母
  18. 狄利克雷卷积与莫比乌斯函数
  19. Zeppelin使用JDBC连接MySQL
  20. Visio Pro 2019里面同时添加上下标的简单方法

热门文章

  1. 解决Neither the JAVA_HOME nor the JRE_HOME environment variable is defined问题
  2. 微信小程序如何进行登录授权和获取用户信息
  3. 病毒的灵魂拷问(绝对原创)
  4. 微信小程序插件功能页开发详细流程
  5. swift 浮点型字符串的运算
  6. 使用 CocoaPods 给微信集成 SDK 打印收发消息
  7. Windows Server 2012关闭Server Manager开机自启动
  8. mac os x常用快捷键及用法
  9. linux + ffmpeg + eclipse 调试
  10. 感恩心成就了车建新和红星美凯龙