转自:https://medium.com/better-programming/load-testing-socket-io-web-applications-and-infrastructure-3e96011898e0 关于artillery的一个实践

Are you shipping a scalable real-time back end? This article provides a tutorial for load testing your Socket.IO-based WebSocket server, to simulate production load and identify performance bottlenecks or scalability issues.

Socket.IO is a framework built on top of the WebSocket protocol, which upgrades an HTTP connection using an Upgrade: websocket header, initiating a WebSocket handshake.

This provides a persistent connection between a client and server where both parties can emit messages at any time, without the need for the client to poll the server.

Socket.IO adds additional functionality on top of this, such as increasing browser and device support of your application by providing an option to fall back to Ajax requests when using a client which does not support WebSockets, or a client on a network which restricts the upgrade HTTP header or protocol changes.


Setup

To follow along with this tutorial, you can either clone and run the demo repositorylocally (suggested), or you can use the version hosted by me on Heroku.

If you choose to run the app locally, you will need to clone the repository and install the required dependencies using npm. The npm start script will start the app on your configured PORT, or 8080 by default.

First, let’s get familiar with the application. Once running, the application should work as below.

The main features to note are being able to send a realtime message between users, and see a count of the current participants in the chat room.

Below is an example of two different users sending and receiving messages in realtime — great!

Application demo


Load Testing

Now that we’ve set up the application, we can start our load testing. For this, we’ll use the Artillery Community Edition tool, which is a free and open-source load testing and functional testing toolkit.

To install the Artillery.io toolkit globally:

$ npm install artillery -g

Next, we need to create a YAML configuration file, which will specify the test configuration and scenarios.

simple-test.yaml

config:
target: "wss://socketio-loadtest.herokuapp.com"
socketio:
transports: ["websocket"]
phases:
- duration: 10 # Run scenario for 10 seconds
arrivalCount: 20 # Create 20 virtual users per scenario
scenarios:
- engine: "socketio"
flow:
- emit:
channel: "add user"
data: "John Doe"
- emit:
channel: "new message"
data: "Hello! {{ $randomString() }}"
- think: 5 # do nothing for 5 seconds, then disconnect

In the above configuration file, we specify our target URL, and Socket.IO engine. We configure our phases section to specify the number of virtual users we will emulate and the duration of the test. Initially, these are low numbers.

In the scenarios section, we specify what WebSocket events we would like the artillery.io client to emit to the server.

For this simple example, we will add a new user to the chat, using the add user event in the demo application, then post a new message. After five seconds, the user leaves which disconnects the WebSocket.

To run the above script, we will use the artillery run command.

$ artillery run load-test/simple-test.yaml

If we view our application in a browser (either locally, or at https://socketio-loadtest.herokuapp.com), virtual users can be seen joining the chatroom and leaving messages.

Simple socket.IO load test

The above simple script can be modified as you wish to increase the number of users in a scenario by tweaking the configuration values. For a more advanced test, you can find more verbose scenario examples below.


Assertions

Now that we have the tool setup, we can set the task to fail based on certain conditions, which is great for a CI environment.

We can choose to set a maximum allowed latency in milliseconds for minmaxmedianp95, and p99, or a maximum failure percentage rate. These can be configured to test again certain business rules, or SLOs.

To configure the above, we add an ensure entry to our configuration. Further examples can be found in the artillery.io documentation.

config:
target: "ws://localhost:8080"
ensure:
maxErrorRate: 1 # fail if error rate exceeds 1%
max: 500 # fail if max response time exceeds 500ms

Advanced Configuration

In the advanced example, dynamic variables are used to simulate a scenario which is closer to how the application will be used in production (real data). This is done using the faker npm module, inside a custom processor which allows us to run our custom code.

The load phase is configured to increase the arrival rate from 10 to 50 users over two minutes, followed by 10 minutes at 50 new users per second.

advanced-test.yaml

config:
target: "ws://localhost:8080"
ensure:
max: 500 # fail if max response time exceeds 500ms
maxErrorRate: 1 # fail if error rate exceeds 1%
socketio:
transports: ["websocket"]
processor: "./custom.js" # set a processor for dynamic variables
phases:
- duration: 120
arrivalRate: 10
rampTo: 50
name: "Warm up phase"
- duration: 600
arrivalRate: 50
name: "Sustained max load"
scenarios:
- engine: "socketio"
flow:
- function: "getChatData" # load variables
- emit:
channel: "add user"
data: "{{ name }}"
- emit:
channel: "new message"
data: "{{ greeting }}"
- think: 10 # stay connected for 10 seconds
- emit:
channel: "new message"
data: "{{ goodbye }}"

Run artillery using the advanced-test.yaml file

$ artillery run load-test/advanced-test.yaml

Advanced socket.IO load test

The above is a snippet of the ramp-up phase defined in the configuration file, where the arrival rate of virtual users increases from 10 to 50 users over two minutes.

The advanced scenario example can be tweaked in many ways depending on what type of test you want to run, such as how many concurrent connections can be handled, or maximum number of users before performance begins to degrade.


Recap

Load testing our Socket.IO applications is an important step in the application lifecycle, to learn:

  • The number of concurrent WebSocket connections the application can support.
  • How the application handles failure.
  • Determines if the current infrastructure is sufficient for the expected load.
  • Gives confidence in the system and its reliability and performance.
  • Helps ensure that degradation of any component in the stack doesn’t lower the state of security.

Next in the Socket.IO blog series will be a tutorial on how to debug memory leaks in Socket.IO applications. If you have any ideas on similar topics you’d be interested in, feel free to get in touch with those ideas.

Load Testing Socket.IO Web Applications and Infrastructure的更多相关文章

  1. node socket.io web

    soket.io & web http://socket.io/get-started/chat/ 新建一個文件夾 soketWeb ; 在sokertWeb 文件夾內新建一個 package ...

  2. Node学习笔记(三):基于socket.io web版你画我猜(二)

    上一篇基础实现的功能是客户端canvas作图,导出dataURL从而实现图片信息推送,下面具体讲下服务端的配置及客户端的配置同步 首先先画一个流程图,讲下大概思路 <canvas id=&quo ...

  3. Socket IO Web实时推送

    1服务器pom.xml引入 <!-- 服务端 --> <dependency> <groupId>com.corundumstudio.socketio</g ...

  4. Node学习笔记(三):基于socket.io web版你画我猜(一)

    经过惨淡的面试,也是知道了自己的不足,刚好最近在学习node,心中便有了做一个web版的你画我猜的想法 首先说下思路,在做准备工作的时候,有两个大概的思路: 1.规定一块div,捕捉鼠标事件,动态生成 ...

  5. tsung: an open-source multi-protocol distributed load testing tool

     ROPERTIES::type: KnowledgeBase_Cloud:END: 开源.多协议.分布式的压力测试工具   Item Summary tsung-recorder start 通过p ...

  6. [Node.js]29. Level 6: Socket.io: Setting up Socket.io server-side & Client socket.io setup

    Below we've already created an express server, but we want to start building a real-time Q&A mod ...

  7. [Node.js] Level 6. Socket.io

    6.2 Setting Up socket.io Server-Side So far we've created an Express server. Now we want to start bu ...

  8. 基于Node.js+socket.IO创建的Web聊天室

    这段时间进了一个新的项目组,项目是用Appcan来做一个跨平台的移动运维系统,其中前台和后台之间本来是打算用WebSocket来实现的,但写好了示例后发现android不支持WebSocket,大为受 ...

  9. 使用Node.js的socket.io模块开发实时web程序

    首发:个人博客,更新&纠错&回复 今天的思维漫游如下:从.net的windows程序开发,摸到nodejs的桌面程序开发,又熟悉了一下nodejs,对“异步”的理解有了上上周对操作系统 ...

随机推荐

  1. myeclipse导入项目中文乱码怎么解决教程

    大家在Myeclipse导入项目的时候,应该都遇见过一些乱码的问题,不单单只是Myeclipse有这个问题,那么怎么解决Myeclipse导入项目乱码的问题呢,问题出现的原因是什么呢,下面来看看答案. ...

  2. ClassPathBeanDefinitionScanner 说明

    Spring 工具类 ClassPathBeanDefinitionScanner 组件Bean定义扫描https://blog.csdn.net/andy_zhang2007/article/det ...

  3. WPF矢量字体图标(iconfont)

    原文:WPF矢量字体图标(iconfont) 转载:点击打开链接 步骤: 一.下载添加iconfont文件 二.添加到资源文件夹,并设置不复制,且为资源文件 三.增加FIcon.xaml文件 < ...

  4. React Children 使用

    React 有一个特殊的属性children, 主要用于组件需要渲染内容,但它并不知道具体要渲染什么内容,怎么会有这种使用场景?确实比较少,但并不是没有,比如弹出框.当你写一个弹出框组件的时候,你知道 ...

  5. echarts的地图省份颜色自适应变化

    在使用echarts的地图的时候省份的颜色可能随着数据的多少显示不同的颜色,但是当后台返回的数据的变化较大时可能就不好控制了,所以需要设置根据后台的数据进行自适应 将后台返回的数据中的value放入一 ...

  6. JS工程师的成长路径

    JS 说起来必须是一个神器,这个当年10天内被开发出来的神器,以一种谁也想象不到的速度快速发展,它击败了Java Applet,逼死Flash,当Android和IOS看似一统全球的时候,JS慢条斯理 ...

  7. 单词cymophanite猫眼石cymophanite英语

    金绿石的猫眼石(Cymophanite)是所谓正宗的猫眼石,非常罕有,尤其是5卡以上而质优的,其售价可以高达七万多港元一卡. 相传这类猫眼石是宝石学家的宝石,从此可知其地位在珠宝玉石之中的重要性.颜色 ...

  8. 如何实现android和服务器长连接

    转载 这种功能实际上就是数据同步,同时要考虑手机本身.电量.网络流量等等限制因素,所以通常在移动端上有一下两个解决方案: 1.一种是定时去server查询数据,通常是使用HTTP协议来访问web服务器 ...

  9. PostgreSQL 基本数据类型及常用SQL 函数操作

    数据类型 名字 别名 描述 bigint int8 有符号的8字节整数 bigserial serial8 自动增长的8字节整数 bit [ (n) ]   定长位串 bit varying [ (n ...

  10. LP线性规划求解 之 单纯形 算法

    LP线性规划求解 之 单纯形 算法 认识-单纯形 核心: 顶点旋转 随机找到一个初始的基本可行解 不断沿着可行域旋转(pivot) 重复2,直到结果不能改进为止 案例-过程 以上篇的case2的松弛型 ...