迹忆客 EN >

当前位置:主页 > 学无止境 > 网络 >

HTTP REST API 和 WebSocket REST API 之间的性能差异

作者:迹忆客 最近更新:2022/10/16 浏览次数:

什么是API?

应用程序编程接口 (API) 是一种软件,它为其他软件提供与之交互的接口。


什么是 REST API?

REST 或 RESTful API 设计(Representational State Transfer)是一种使用现有协议的架构风格。 REST API 设计有六个关键约束:

  • 使用统一界面 (UI)
  • 基于客户端-服务器
  • 无状态操作
  • RESTful 资源缓存
  • 分层系统
  • 按需编码

什么是HTTP REST API?

虽然 REST 几乎可以用于任何协议,但它通常利用 HTTP。 所以基本上 HTTP REST API 是一个 API,它使用 HTTP 协议在服务器和客户端之间传输请求/响应数据,同时使用 REST API 设计原则。


什么是 WebSocket REST API?

这是一个使用 WebSockets 协议的 API,它使用 WebSockets 协议(构建在 HTTP 协议之上)在服务器和客户端之间传输请求/响应数据,同时使用 REST API 设计原则。


为什么这对响应速度很重要?

虽然 WebSocket 在仅用于单个请求(打开 Web 套接字 -> 发送请求 -> 获取响应 -> 关闭 Web 套接字)时可能会过大,但当我们在单个连接上执行多个请求时,它们会非常快。 WebSockets 背后的整个想法是在客户端和服务器之间建立双向通信通道。 这意味着两件事:

  • 这种类型的连接使我们能够在不发送任何请求的情况下从服务器接收更新(例如,我们可以创建通知)
  • 因为我们只创建一次连接并多次使用它(对于我们想要的任意数量的请求),则可以节省大量服务器资源和带宽,因此我们可以获得更快的响应。

性能测试和结果

用于执行测试的硬件是具有 M1 Max 处理器和 32GB RAM 和 iPad mini 4 的 Apple MacBook Pro 16"。服务器是在 VM(2 核,4GB ram)内创建的。NodeJS(v.18.2 .0) 用于为 HTTP REST API 和 WebSocket REST API 创建相同的“书籍”模拟数据。响应长度为 358 字节。

为了我们的测试目的,我们创建了一个简单的 Flutter 应用程序(v3.0.1)来扮演测试工具的角色。 我选择 Dio 包来创建 HTTP 请求并测试 HTTP RESP API 部分和 web_socket_channel 包用于 WebSocket REST API 部分。 多个请求的结果以毫秒为单位。

Emulator 测试

Ipad mini4 测试


最后的想法

从上面的表格和图表中,我们可以毫不奇怪地看到,随着请求数量的增加,WebSocket 的优势越来越大,无论我们是在模拟器还是真实设备上进行测试。

在我的下一篇文章中,我们将解释如何使用 WebSocket REST API 的 API 版本控制构建简单的 NodeJS 服务器架构。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

HTTP POST request in Node.js

发布时间:2025/04/17 浏览次数:131 分类:Node.js

In this article, we will learn how to use Node.js to make a post request using a third-party package. HTTP Post Request in Node.js The HTTP POST method creates or adds resources on the server. The key difference between POST and PUT request

Calling REST API in Node.js

发布时间:2025/04/17 浏览次数:197 分类:Node.js

REST is short for Representational State Transfer . A REST API or RESTful API is an API or Web API (Application Programming Interface) that conforms to the constraints/restrictions of the REST architectural style and allows interaction with

Apache2.4 installation and precautions under Windows7

发布时间:2025/04/08 浏览次数:125 分类:OPERATING SYSTEM

To install apache2.4 in Windows 7, we first need to download the apache2.4 installation program. Here we download the software from the apache official website http://httpd.apache.org/download.cgi . First, let's see how to download apache2.

How to redirect a website from HTTP to HTTPS

发布时间:2025/03/16 浏览次数:118 分类:NETWORK

HTTPS is a protocol for secure communication over computer networks and is widely used on the Internet. More and more website owners are migrating from HTTP to HTTPS, mainly due to the following 5 reasons: Google announced that websites usi

How to Fix the “SSL Handshake Failed” Error (5 Methods)

发布时间:2025/03/16 浏览次数:98 分类:NETWORK

Installing a Secure Sockets Layer (SSL) certificate on your WordPress site enables it to use HTTPS for a secure connection. Unfortunately, there are a lot of things that can go wrong in the process of verifying a valid SSL certificate and e

Detailed introduction to Let's Encrypt

发布时间:2025/03/16 浏览次数:131 分类:NETWORK

Let's Encrypt is a free, automated, and open certificate authority that officially launched in April 2016. It was originally founded in 2012 by two Mozilla employees. Their goal for founding Let's Encrypt was really simple; to encrypt the e

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便