In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Today, I would like to share with you the relevant knowledge of how C++ can achieve high-performance HTTP services. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's learn about it together.
Actual combat (practical information)
Needless to say, let's take a look at how 10 lines of C++ code can achieve a high-performance Http service that can easily QPS hundreds of thousands. Linus said: talk is cheap,show me the code ↓
Int main () {WFHttpServer server ([] (WFHttpTask * task) {task- > get_resp ()-> append_output_body ("Hello World!");}); if (server.start (8888) = = 0) {getchar (); / / press "Enter" to end. Server.stop ();} return 0;}
This server uses workflow, and it is very easy to install and compile. Take Linux as an example, after pulling down the code, you can compile it with one line of command:
➜git clone https://github.com/sogou/workflow➜ cd workflow ➜make➜ cd tutorial ➜make➜. / helloworld
The code is in the tutorial directory, and the compiled helloworld can be run directly. If you listen on port 8888, curl can access it:
➜curl-I http://localhost:8888HTTP/1.1 200OKContent-Length: 25Connection: Keep-AliveHello World!
With the above 10 lines of code, we interpret it in detail:
We chose the Http protocol, so we constructed a WFHttpServer
A network interaction is a task, because it is a Http protocol, so we are WFHttpTask
For server, my interactive task is to fill in the reply after receiving the request, which can be obtained through task- > get_req () and task- > get_resp ().
The logic is in a function (that is, the lambda above), which indicates what to do after receiving the message. Here, fill in the sentence "Hello World!"
Server starts and exits using two simple api, start () and stop (), with getchar () in the middle; it gets stuck because workflow is a purely asynchronous framework.
Pure async is the high performance of this Http server:
First, multithreading provides services.
If we do something blocking in this function after receiving the request (such as waiting for locks, io requests, or busy calculations, etc.), then when a user requests me, I will have no thread to deal with new users.
Second, network threads and execution threads have excellent scheduling strategies.
No matter how many threads are occupied, there may be times when they are occupied. We need that no matter what time-consuming operation the server function wants to do, it will not affect the network thread
Third, take linux as an example, the encapsulation of epoll is efficient and easy to use.
If the service only plans to support 10, 000 QPS, in fact, the underlying implementation is very simple, but if we want 100, 000, or even close to one million, then we have very high requirements for the server bottom layer to send and receive Imax O model.
Let's take a look at how workflow achieves these high concurrency capabilities:
Based on the above architecture, workflow-based server can easily reach hundreds of thousands of QPS, high throughput, low cost, fast development, perfect support of all Sogou's back-end online services! For detailed code implementation, please refer to the workflow source code. Then we talk in terms of data, and compare it with nginx, a world-renowned high-performance Http server, and brpc, a domestic open source framework pioneer, to take a look at the relationship between QPS and concurrency under fixed data length:
The above is the wrk stress test done with the same variable on the same machine. You can go to github to view the machine configuration, parameters and code of the stress test tool. When the data length remains unchanged, QPS increases with the increase of concurrency, and then tends to be stable. In this process, workflow always has obvious advantages, which is higher than nginx and brpc. Especially for the two curves with data length of 64 and 512, when the degree of concurrency is enough, the QPS of 50W can be maintained.
These are all the contents of this article entitled "how C++ can achieve High-performance HTTP Services". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.