Introduction to the implementation of MockStub and the introduction of mockstub
On the first Wednesday of March 13, I officially joined Baidu and started my work at baiduer. In less than two months, I left the INF department and pulled it to the Baidu Hi-Server team for Human Resources reasons. I have done a lot of things I have never done before for two positions that are completely unadjusted. Summary: The INF product line is a new product that was launched only at the Baidu World Conference in September. Many things cannot be disclosed. The Baidu Hi-Server product line is a relatively old product, it lasted for several years. In December July, it was switched to the Hi-Server side. In less than a month, it mainly supported three things: implementation of a login pile and iOS message push on a public platform, there is also a single test of the Message Protocol. The implementation of the login pile is completed in the Python language, and the main time consumption is the implementation of the company's two custom classes. The iOS message push on the public platform adds code based on the original project to facilitate the pushing of public account information. The single test of the message uses the gtest framework, which is implemented using a parameterized solution. This series of posts is an excellent post about gtest single-test: Success!
I. What is the login pile?
The project needs to perform some offline stress tests, mainly to evaluate the performance of some new modules. Therefore, we need to provide a pile of simulated login services to Support this evaluation. The login post is a Mock login service. After receiving the login request, it simulates the real login service and constructs an authenticated string to return the Response Message to simulate the login.
2. What should I do when I log on to the terminal?
Based on the above analysis of the login pile demand, we can divide the implementation of the login pile as follows:
1. Listener service: establishes a TCP connection to listen to login requests on the specified port;
2. Resolution request package: the received request package is compressed and encrypted in a certain format. The pile needs to be decrypted and decompressed to retrieve the fields we need to pay attention;
3. construct a response packet: Based on the parsed request packet, we construct the response packet corresponding to the request packet according to the response packet format of the real login service, then, compress and encrypt the message and send it to the requester;
4. Concurrency: the pile needs to support concurrent simulated login for multiple accounts, so the service we need to implement should be a multi-thread.
3. Implement logging in!
The format of the Request Response Message involves company secrets, so it is not described here. The main idea of the message is:
Instance --> Json --> pack --> encrypt --> send ---------------- receive -------------> decrypt --> unpack --> Anti-Json --> get the specific field information.
Construct a response package --> Json --> pack --> encrypt --> response!
Today's main content is the use of concurrent sockets. in Python, The SocketServer module is used to establish a connection. The SocketServer. ThreadingTCPServer class supports concurrent TCP connections. So how can we use this concurrent TCP Service? Let's take it step by step.
The first step is to implement a custom TCPHandle class, which is defined as follows:
Class MyTCPHandle (SocketServer. StreamRequestHandler ):
In this custom class, you need to implement the handle method:
Def handle (self ):
Finally, establish a TCP connection in main:
Mockserver = SocketServer. ThreadingTCPServer ('', int (sys. argv [1]), MyTCPHandle)
The second parameter is the service port, which is completed by passing parameters.
Finally, TCP started a busy job:
Mockserver. serve_forever ()
This log has been written since July 28. It has been busy with work and cannot be extracted. Today, I am determined to finish writing it. But in terms of content, it is obviously a lot worse than when I was planning to write logs. That's it. I hope it can help you a little bit!
Published with Windows LiveWriter.