Load Testing Socket.IO Web Applications and Infrastructure

转自: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 scenarioscenarios:  - 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.

原文地址:https://www.cnblogs.com/rongfengliang/p/11505637.html

时间: 2024-11-12 16:41:57

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

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

经过惨淡的面试,也是知道了自己的不足,刚好最近在学习node,心中便有了做一个web版的你画我猜的想法 首先说下思路,在做准备工作的时候,有两个大概的思路: 1.规定一块div,捕捉鼠标事件,动态生成position absolute,长宽1px的红色小div,这样可以模拟出划线的轨迹,做一个long polling,不断获取DOM结构,推送到socket端口,然后再广播给所有客户端 2.利用canvas作图,将canvas的数据推送到socket端口,广播所有客户端 其实之所以有两种想法,无非

[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 building a real-time Q&A moderation service and we've decided to use socket.io. Using the http module, create an new http server and pass the expressapp

反向Ajax,第3部分:Web服务器和Socket.IO

英文原文:Reverse Ajax, Part 3: Web servers and Socket.IO 前言 时至今日,用户期待的是可通过web访问快速.动态的应用.这一文章系列展示了如何使用反向Ajax(Reverse Ajax)技术来开发事件驱动的web应用.系列的第1部分介绍了反向Ajax.轮询(polling).流(streaming).Comet和长轮询(long polling).你已经了解了Comet是如何使用HTTP长轮询的,这是可靠地实现反向Ajax的最好方式,因为现有的所有

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

首发:个人博客,更新&纠错&回复 今天的思维漫游如下:从.net的windows程序开发,摸到nodejs的桌面程序开发,又熟悉了一下nodejs,对“异步”的理解有了上上周对操作系统的学习而更能理解.然后发现了Node.js中的socket.io这个模块,又觉得跟前几天用.net做客户端的socket游戏了.技术世界,兜兜转转,相逢一笑,疑是故人. socket.io用来做实时web程序,解决之前的B/S程序只有无状态连接,特定需求还需要用长连接这种“奇技淫巧”的问题.当然,这是html

HTML5 Web socket和socket.io

what is websockets Two-way communication over ont TCP socket, a type of PUSH technology HTML5的新特性,用于双向推送消息(例如网页聊天,手机推送消息等) 原理: client利用regular http请求webpage 请求的webpage 执行javascript脚本,open a connection to server. 有新的信息时服务器和客户端可以相互发送信息(Real-time traffi

Socket.IO介绍:支持WebSocket、用于WEB端的即时通讯的框架

一.基本介绍 WebSocket是HTML5的一种新通信协议,它实现了浏览器与服务器之间的双向通讯.而Socket.IO是一个完全由JavaScript实现.基于Node.js.支持WebSocket的协议用于实时通信.跨平台的开源框架,它包括了客户端的JavaScript和服务器端的Node.js. Socket.IO除了支持WebSocket通讯协议外,还支持许多种轮询(Polling)机制以及其它实时通信方式,并封装成了通用的接口,并且在服务端实现了这些实时机制的相应代码.Socket.I

Hosting socket.io WebSocket apps in IIS using iisnode

In this post I explain how to configure a socket.io node.js application to use of WebSockets when hosting it in IIS 8 using iisnode. This complements a recent post in which I showed how to host node.js WebSocket applications in IIS on Windows using i

为Phonegap Android平台增加websocket支持,使默认成为socket.io首选通

为Phonegap Android平台增加websocket支持,使默认成为socket.io首选通道选择 广而告之 使用socket.io作为跨浏览器平台的实时推送首选,经测试在各个主流浏览器上测试都确实具有良好的下实时表现.这里为推广socketio-netty服务器端实现哈,做次广告,同时预热一下: socketio-netty : 又一款socket.io服务器端实现,兼容0.9-1.0版本~ 示范目的 我们要构建一个在市面上常见浏览器上都可以正常运行的集体聊天应用,保证在IE6+,Fi

【socket.io研究】0.前提准备

WebSocket出现之前,web实时推送,一般采用轮询和Comet技术(可细分为长轮询机制和流技术两种),需要大量http请求,服务器受不了.HTML5定义了WebSocket协议,基于TCP协议,由通讯协议和编程API组成,在浏览器和服务器之间建立双向连接,以基于事件的方式,赋予浏览器实时通讯的能力. 建立WebSocket连接的过程是浏览器首先发起一个http请求,在请求头中附带着“Upgrade: WebSocket”头信息,表名申请协议升级,服务器解析后产生应答信息,服务器与客户端的W