Disclosure of Invention
In view of this, an object of the present application is to provide an asynchronous request monitoring method and apparatus, so as to monitor the execution time of an asynchronous request, so as to further optimize the asynchronous request, where the specific technical scheme is as follows:
in a first aspect, the present application provides an asynchronous request monitoring method, including:
marking the starting execution time of the service asynchronous request after receiving a first message representing that the service asynchronous request initiated by a client is successfully initiated;
marking the execution end time of the service asynchronous request after receiving a request response result returned by the service server end responding to the service asynchronous request;
and calculating the time difference between the execution ending time and the execution starting time corresponding to the service asynchronous request to obtain the execution time of the service asynchronous request.
Optionally, after a request response result returned by the service server in response to the service asynchronous request is monitored, marking the execution end time of the service asynchronous request, including:
when the first message is monitored, generating a monitoring timer, wherein the monitoring timer is used for immediately recording the current time after being started;
and starting the monitoring timer after monitoring a request response result returned by the service server to obtain the execution end time of the service asynchronous request.
Optionally, after a request response result returned by the service server is monitored, starting the monitoring timer to obtain the execution end time of the service asynchronous request, including:
when a request response result returned by the service server is monitored, the monitoring timer is immediately executed so as to record the current time;
and marking an ending time variable after monitoring that the monitoring timer is started, and assigning the time recorded by the monitoring timer to the ending time variable to obtain the execution ending time.
Optionally, the marking the start execution time of the service asynchronous request after monitoring a first message that represents that the service asynchronous request initiated by the client is successfully initiated includes:
the upper application of the client initiates a service asynchronous request and sends the service asynchronous request to a bottom service executor;
and when the upper application receives a first message of successful initiation returned by the bottom service executor, recording the current time as the starting execution time.
Optionally, the method further comprises:
and sending the execution time of the service asynchronous request to a monitoring server so that the monitoring server sends the execution time to a corresponding monitoring client to be displayed.
In a second aspect, the present application further provides an asynchronous request monitoring apparatus, including:
the starting execution time marking module is used for marking the starting execution time of the service asynchronous request after receiving a first message which represents that the service asynchronous request initiated by the client is successfully initiated;
the execution end time marking module is used for marking the execution end time of the service asynchronous request after receiving a request response result returned by the service server end responding to the service asynchronous request;
and the computing module is used for computing the time difference between the execution ending time and the execution starting time corresponding to the service asynchronous request to obtain the execution time of the service asynchronous request.
Optionally, the execution end time marking module includes:
a timer generation submodule, configured to generate a monitoring timer when the first message is monitored, where the monitoring timer is used to record current time immediately after being started;
and the end time obtaining submodule is used for starting the monitoring timer after monitoring a request response result returned by the service server side to obtain the execution end time of the service asynchronous request.
Optionally, the end time obtaining sub-module is specifically configured to:
when a request response result returned by the service server is monitored, the monitoring timer is immediately executed so as to record the current time;
and marking an ending time variable after monitoring that the monitoring timer is started, and assigning the time recorded by the monitoring timer to the ending time variable to obtain the execution ending time.
Optionally, the start execution time marking module includes:
the asynchronous request sending submodule is used for initiating a service asynchronous request by an upper layer application of the client and sending the service asynchronous request to a bottom layer service executor;
and the return state receiving submodule is used for recording the current time as the starting execution time when the upper-layer application receives a first message of successful initiation returned by the bottom-layer service executor.
Optionally, the apparatus further comprises:
and the sending module is used for sending the execution time of the service asynchronous request to a monitoring server so that the monitoring server sends the execution time to a corresponding monitoring client for display.
According to the asynchronous request monitoring method provided by the application, after the client side initiates the service asynchronous request and monitors the first message that the service asynchronous request is successfully initiated, the current time is recorded as the starting execution time of the service asynchronous request. And marking the execution ending time of the service asynchronous request after receiving a request response result returned by the service server. And finally, calculating the time difference between the execution ending time and the execution starting time to obtain the execution time of the service asynchronous request. By using the scheme, the starting execution time of the business asynchronous request is recorded after the business asynchronous request is successfully sent to the business server, and the execution ending time is recorded after the request result information returned by the business server is received, so that the execution time of the business asynchronous request is obtained. And the developer can optimize the processing process of the asynchronous request after monitoring the execution time of the service asynchronous request.
Detailed Description
In order to make the objects, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the drawings in the embodiments of the present invention, and it is obvious that the described embodiments are some, but not all, embodiments of the present invention. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
Referring to fig. 1, a flowchart of an asynchronous request monitoring method provided by an embodiment of the present application is shown, where the method is applied to a client side, and a monitoring end (i.e., monitoring code) is deployed in the client, and the monitoring end monitors all service asynchronous requests to obtain time taken to execute the asynchronous requests, so as to further analyze performance of the asynchronous requests.
As shown in fig. 1, the method mainly comprises the following steps:
s110, after receiving a first message representing that the service asynchronous request initiated by the client is successfully initiated, marking the starting execution time of the service asynchronous request.
The client in the application can be an application program or a browser installed in the mobile terminal or a browser installed in a PC.
A user can initiate service requests at a client side, some service requests are processed in an asynchronous mode, and the method and the device mainly aim at monitoring the asynchronous requests.
When the client generates a service asynchronous request according to the operation of a user and then sends the service asynchronous request to the service server, and when the service server receives the service asynchronous request initiated by the client, a message, namely a first message, indicating that the service server successfully receives the service asynchronous request is returned to the client. And after monitoring the first message, the monitoring end records the starting execution time of the service asynchronous request.
And S120, marking the execution end time of the service asynchronous request after receiving a request response result returned by the service server end responding to the service asynchronous request.
And when the monitoring end monitors the request result information (namely the request response result) returned by the service server, recording the execution ending time of the service asynchronous request.
S130, calculating a time difference value between the execution ending time and the execution starting time corresponding to the service asynchronous request to obtain the execution time of the service asynchronous request.
And calculating the time difference between the starting execution time and the execution ending time of the service asynchronous request to obtain the execution time of the service asynchronous request.
Preferably, as shown in fig. 1, the asynchronous request monitoring method further includes:
s140, the execution time of the service asynchronous request is sent to the monitoring server, so that the monitoring server sends the execution time to the monitoring client for display.
After monitoring the obtained execution time of the asynchronous request, the monitoring terminal uploads the execution time to the monitoring server, for example, the client uploads the obtained execution time of the service asynchronous request to the monitoring server through a formulated interface.
The monitoring server receives the execution time and sends the execution time to the monitoring client so that a developer can clearly know the time required by executing the asynchronous request and the performance bottleneck of the application in the asynchronous request part. And further optimizing the execution logic of the application aiming at the monitored execution time of the asynchronous request, thereby improving the response speed of the application.
In the asynchronous request monitoring method provided in this embodiment, after a service asynchronous request is successfully sent to a service server, a start execution time of the service asynchronous request is recorded, and after request result information returned by the service server is received, an execution end time is recorded, so that the execution time of the service asynchronous request is obtained. The developer can optimize the processing process of the asynchronous request after monitoring the execution time of the service asynchronous request, so as to shorten the execution time of the asynchronous request.
Referring to fig. 2, a schematic diagram of an execution model of a JS thread inside a client according to an embodiment of the present application is shown, and as shown in fig. 2, after a service asynchronous request is initiated by an HTTP request thread inside the client, a timer is inserted into each service request by a monitoring end, where the timer is used to record a time corresponding to execution of the service asynchronous request.
The specific process of performing the asynchronous request monitoring method using the model shown in fig. 2 will be described in detail below with reference to fig. 3. As shown in fig. 3, the asynchronous request monitoring method includes the following steps:
s210, the client initiates a service asynchronous request and monitors the state of the service asynchronous request.
The upper layer application of the client initiates a service asynchronous request and puts the service asynchronous request to the bottom layer service executor.
The method needs to deploy a monitoring terminal at a client, and all service asynchronous requests can be monitored globally through the monitoring terminal. Specifically, a null object needs to be created to point to the service asynchronous request generated by the client, and an object is created for each service asynchronous request. The monitoring of each traffic asynchronous request is established by this empty object.
And S220, recording the current time as the starting execution time of the service asynchronous request when the client monitors that the return state is successful, and simultaneously generating a monitoring timer.
After monitoring the success status (i.e. the first message) returned by the bottom-layer service executor, the monitoring end in the client marks the start execution time, for example, marks a variable a, where the value of the variable a is the start execution time of the service asynchronous request.
Meanwhile, a supervision timer (i.e. the timer in fig. 2) is generated after the monitoring of the return status is successful, and is inserted into the service asynchronous request, and the supervision timer records the execution time of the service asynchronous request.
When the HTTP request thread initiates a service asynchronous request, the service asynchronous request is put into an event queue, and a monitoring timer is inserted into each service asynchronous request by a pile insertion and code printing mode by a monitoring end, namely, the monitoring timer is packaged in each service asynchronous request in the event queue. The watchdog timer is used to capture the time at which the traffic asynchronous request is executed.
In addition, the event queue adopts a first-in first-out structure, so that the first-in request is ensured to be executed first.
And S230, when the client monitors the callback of the service asynchronous request, starting a monitoring timer in the service asynchronous request to record the current time.
The watchdog timer is set to 0s before execution, in order to execute the code in the watchdog timer immediately when the HTTP request thread executes the traffic asynchronous request callback function. The code in the watchdog timer is used to record the time of the current time. Namely, the execution time (i.e., the execution end time) of the service asynchronous request is recorded by using the monitoring timer.
The callback of the service asynchronous request is used for later sending request result information of the service asynchronous request, namely a result returned by the service server after responding to the service asynchronous request.
And S240, marking an ending time variable after the client monitors that the monitoring timer is started, and assigning the time recorded by the monitoring timer to the ending time variable to obtain the execution ending time of the service asynchronous request.
After monitoring the execution of the monitoring timer, a monitoring end deployed in the client marks an end time variable b, and gives the time recorded by the monitoring timer to the end time variable b, namely the time in the end time variable b is the execution end time of the service asynchronous request.
And S250, calculating the time difference between the ending time variable and the starting time variable to obtain the corresponding execution time of the service asynchronous request.
This step calculates the difference b-a, which is the execution time of the service asynchronous request.
According to the asynchronous request monitoring method provided by the embodiment, according to the principle of single thread (that is, only one thread is in an execution state at the same time), after the execution of the service asynchronous request is monitored, a timer is added immediately, and the time for executing the service asynchronous request is captured by the timer. Therefore, the execution time of the service asynchronous request is obtained by calculating the time difference between the execution time of the monitoring timer and the execution start time of the service asynchronous request, from the execution start time of the service asynchronous request → the execution of the monitoring timer. Finally, the purpose of monitoring the execution time of the asynchronous request is achieved, so that developers can optimize the process of the asynchronous request according to the monitored execution time, and the performance of the asynchronous request is improved.
Corresponding to the embodiment of the asynchronous request monitoring method, the application also provides an embodiment of an asynchronous request monitoring device.
Referring to fig. 4, a schematic structural diagram of an asynchronous request monitoring apparatus provided in an embodiment of the present application is shown, where the apparatus includes:
the start executiontime marking module 110 is configured to mark a start execution time of the service asynchronous request after receiving a first message indicating that the service asynchronous request initiated by the client is successfully initiated.
In one embodiment of the present application, as shown in fig. 5, the start executiontime marking module 110 includes: an asynchronousrequest sending submodule 111 and a returnstatus receiving submodule 112.
The asynchronousrequest sending submodule 111 is used for initiating a service asynchronous request by an upper application of a client and sending the service asynchronous request to a bottom service executor;
and the returnstate receiving submodule 112 is configured to record the current time as the start execution time when the upper-layer application receives a first message that the initiation is successful, where the first message is returned by the lower-layer service executor.
The execution endtime marking module 120 is configured to mark the execution end time of the service asynchronous request after receiving a request response result returned by the service server in response to the service asynchronous request.
In one embodiment of the present application, as shown in fig. 6, the execution endtime stamping module 120 includes: atimer generation submodule 121 and an endtime acquisition submodule 122;
atimer generating submodule 121, configured to generate a monitoring timer when the first message is monitored, where the monitoring timer is used to record current time immediately after being started;
and the endtime obtaining submodule 122 is configured to start the monitoring timer after monitoring a request response result returned by the service server, so as to obtain the execution end time of the service asynchronous request.
In one possible implementation manner of the present application, the endtime obtaining sub-module 122 is specifically configured to:
when a request response result returned by the service server is monitored, immediately executing a monitoring timer so that the monitoring timer records the current time; and marking an ending time variable after monitoring that the monitoring timer is started, and assigning the time recorded by the monitoring timer to the ending time variable to obtain execution ending time.
The calculatingmodule 130 is configured to calculate a time difference between the execution ending time and the start execution time corresponding to the service asynchronous request, so as to obtain the execution time of the service asynchronous request.
According to the asynchronous request monitoring device, after the client initiates the service asynchronous request and monitors the first message that the service asynchronous request is successfully initiated, the current time is recorded as the starting execution time of the service asynchronous request. And marking the execution ending time of the service asynchronous request after receiving a request response result returned by the service server. And finally, calculating the time difference between the execution ending time and the execution starting time to obtain the execution time of the service asynchronous request. By using the scheme, the starting execution time of the business asynchronous request is recorded after the business asynchronous request is successfully sent to the business server, and the execution ending time is recorded after the request result information returned by the business server is received, so that the execution time of the business asynchronous request is obtained. And the developer can optimize the processing process of the asynchronous request after monitoring the execution time of the service asynchronous request.
Referring to fig. 7, a schematic structural diagram of another asynchronous request monitoring apparatus provided in the embodiment of the present application is shown, where the apparatus further includes, on the basis of the embodiment shown in fig. 4: a sendingmodule 210.
The sendingmodule 210 is configured to send the execution time of the service asynchronous request to the monitoring server, so that the monitoring server sends the execution time to a corresponding monitoring client for displaying.
After monitoring the obtained execution time of the asynchronous request, the client uploads the execution time to the monitoring server, for example, the client uploads the obtained execution time of the service asynchronous request to the monitoring server through a formulated interface.
In the asynchronous request monitoring device provided in this embodiment, after the client obtains the execution time of the asynchronous request, the client uploads the execution time to the monitoring server, and the monitoring server receives the execution time and sends the execution time to the monitoring client for display, so that a developer can clearly know the time required for executing the asynchronous request and a performance bottleneck existing in the asynchronous request part. And further optimizing the execution logic of the application aiming at the monitored execution time of the asynchronous request, thereby improving the response speed of the application.
While, for purposes of simplicity of explanation, the foregoing method embodiments have been described as a series of acts or combination of acts, it will be appreciated by those skilled in the art that the present invention is not limited by the illustrated ordering of acts, as some steps may occur in other orders or concurrently with other steps in accordance with the invention. Further, those skilled in the art should also appreciate that the embodiments described in the specification are preferred embodiments and that the acts and modules referred to are not necessarily required by the invention.
It should be noted that, in the present specification, the embodiments are all described in a progressive manner, each embodiment focuses on differences from other embodiments, and the same and similar parts among the embodiments may be referred to each other. For the device-like embodiment, since it is basically similar to the method embodiment, the description is simple, and for the relevant points, reference may be made to the partial description of the method embodiment.
The steps in the method of the embodiments of the present application may be sequentially adjusted, combined, and deleted according to actual needs.
The device and the modules and sub-modules in the terminal in the embodiments of the present application can be combined, divided and deleted according to actual needs.
In the several embodiments provided in the present application, it should be understood that the disclosed terminal, apparatus and method may be implemented in other manners. For example, the above-described terminal embodiments are merely illustrative, and for example, the division of a module or a sub-module is only one logical division, and there may be other divisions when the terminal is actually implemented, for example, a plurality of sub-modules or modules may be combined or integrated into another module, or some features may be omitted or not executed. In addition, the shown or discussed mutual coupling or direct coupling or communication connection may be an indirect coupling or communication connection through some interfaces, devices or modules, and may be in an electrical, mechanical or other form.
The modules or sub-modules described as separate parts may or may not be physically separate, and parts that are modules or sub-modules may or may not be physical modules or sub-modules, may be located in one place, or may be distributed over a plurality of network modules or sub-modules. Some or all of the modules or sub-modules can be selected according to actual needs to achieve the purpose of the solution of the present embodiment.
In addition, each functional module or sub-module in the embodiments of the present application may be integrated into one processing module, or each module or sub-module may exist alone physically, or two or more modules or sub-modules may be integrated into one module. The integrated modules or sub-modules may be implemented in the form of hardware, or may be implemented in the form of software functional modules or sub-modules.
Finally, it should also be noted that, herein, relational terms such as first and second, and the like may be used solely to distinguish one entity or action from another entity or action without necessarily requiring or implying any actual such relationship or order between such entities or actions. Also, the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or apparatus. Without further limitation, an element defined by the phrase "comprising an … …" does not exclude the presence of other identical elements in a process, method, article, or apparatus that comprises the element.
The previous description of the disclosed embodiments is provided to enable any person skilled in the art to make or use the present invention. Various modifications to these embodiments will be readily apparent to those skilled in the art, and the generic principles defined herein may be applied to other embodiments without departing from the spirit or scope of the invention. Thus, the present invention is not intended to be limited to the embodiments shown herein but is to be accorded the widest scope consistent with the principles and novel features disclosed herein.
The foregoing is only a preferred embodiment of the present invention, and it should be noted that, for those skilled in the art, various modifications and decorations can be made without departing from the principle of the present invention, and these modifications and decorations should also be regarded as the protection scope of the present invention.