Detailed Description
The embodiment of the invention provides a network performance parameter acquisition method, a terminal device and a storage medium, which are used for quickly and accurately pushing hot spot live broadcast.
In order to make the objects, features and advantages of the present invention more obvious and understandable, the technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings in the embodiments of the present invention, and it is obvious that the embodiments described below are only a part of the embodiments of the present invention, and not all of the embodiments. 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.
The first embodiment is as follows:
referring to fig. 1, a schematic flow chart of a network performance parameter acquisition method according to an embodiment of the present invention includes the following steps:
s101, defining classification of NSURLSession, and setting an exchange method of network requests in the classification;
the NSURLSession is a group of classes used for processing HTTP/HTTPS-based requests, can manage network requests and uniformly configure request tasks. The classification is used for adding a new method on the basis of the original classification, expanding the attribute and specifically representing a structural body pointer pointing to the classification. In the embodiment of the invention, classification of the system class NSURLSession is defined, and the function of NSURLSession can be expanded, thereby realizing a customized exchange method.
The exchange method is specifically a self-defined tool class, and a hookmethod is defined in the tool class and is used for replacing the original class, the original class method, the replaced class and the replaced class method. The above replacement is mainly realized by a runtime function.
S102, when a terminal starts a preset application, acquiring a proxy object in NSURLSession through the classification, and dynamically exchanging a callback method in the proxy object;
the preset application starting means that the terminal clicks and opens the APP or a certain section of program, namely, when the APP or the certain section of program is in a running state, the replacement operation is started. The proxy object is an instantiated network request and an object of NSURLSession from new, and the object can process network interaction tasks and information content. The callback method is a callback function executed by a system in a default way, and specifically comprises a method for creating NSURLSession, a call after network performance parameters are collected and a call after a network request is completed.
And if the replacement is not completed, the corresponding callback function is preset and is injected into the proxy object, so that the network request of the application program is prevented from being influenced because the replacement is unsuccessful.
Optionally, a tool class is defined, and the replacement of the nsursursion creation method and the replacement of the proxy object callback method are respectively defined in the tool class. The tool class is mainly used for realizing the replacement of the proxy object and the replacement of the callback method, the proxy object after the replacement can acquire the original callback method and then exchange the callback method, and the exchanged callback method can transmit network request information. In the embodiment of the invention, a class method for replacing the proxy object and a class method for replacing the callback method are directly defined in the tool class, and both the two class methods are executed in a runtime function, so that dynamic exchange can be ensured, and a network request is acquired on the basis of not influencing an original code.
Optionally, a class is customized, the class is implemented in a single instance, and an exchange method for implementing the classification in the class is set. The realization under the singleton mode can ensure that only one instance exists in one class, provide a unique access point and reduce the expenditure of the memory. In the embodiment of the invention, the self-defined class can be used for setting the starting of the exchange, and the extension function of the classification is realized inside the self-defined class for carrying out the dynamic exchange.
Preferably, the method further comprises creating an asynchronous serial queue inside the class, and putting all methods in the class into the asynchronous serial queue. The asynchronous serial queue is created, so that the blocking problem caused by multi-thread concurrency can be avoided, and the influence of synchronous operation is reduced.
S103, when the network request of the predetermined application is detected, the network request is stored in a predefined dictionary through an exchanged callback method.
And every time the application program initiates a network request, the request parameters are captured by the replaced callback method, the intercepted information is transmitted out and stored in a specific form, the information is the network request, and the request parameters comprise network performance parameters. In the embodiment of the invention, the callback method for replacing the proxy object by the proxy object enables the replaced callback function to acquire the content of the network request parameter, and the default callback function set by the system can be executed after the replaced callback function is executed. The replaced callback function may collect and transmit the received network performance parameters.
The dictionary is a key value storage structure, specifically, the network request task is set as a key, and the network performance parameter is stored in the dictionary as a value. Preferably, lazy loading of the dictionary is realized when the dictionary is created.
Optionally, the method further includes acquiring an erroneous request in the network request task, removing the erroneous network request task and the corresponding network request parameter from the dictionary, and converting and uploading data in the dictionary to a server.
In the steps, the proxy object is obtained to replace the callback method, so that the network request parameter is intercepted, and the system default function is executed after the parameter is acquired, so that the interference on the normal network request can be avoided, the original code is not invaded, the method is simple and efficient, and the convenient processing of the parameter information is ensured.
Example two:
on the basis of fig. 1, taking program code as an example, details a specific implementation process of network performance parameter acquisition in conjunction with fig. 2 as follows:
NSURLSession is created in the process of initiating the network request, a request task can be generated by using NSURLSession, and the request task is called back in a proxy mode. NSURLSession is typically created by the 'session WithConfiguration: delete:' method.
In S201, a tool class DYNetworkSwizzleUtil is defined, a function hookMethod of exchanging is defined in the tool class, and exchanging of class methods is realized by runtime. And defining a function hookDelegateMethod, and realizing the replacement of the callback function through runtime.
In S202, classification of system class NSURLSession is created, switzleNetwork is defined as a method for exchanging network requests, a class method of DYNETWkSwizzleUtil is called to replace 'sessionWithConfiguration: delete' with 'dysSessionWithConfiguration: delete', and then replace 'URLSsion: task:' with DYNETWkSwizzleUtil in callback method, and 'URLSedFinisshcollectingMetrics:' and 'URLSsion: task: didComWithError:'. In the above, the replacement of the proxy object in the network request and the callback method thereof is completed.
In S202, the replacement function is extended based on nsursursion, and the replacement program can be started through S203. Specifically, a DYNETwork monitor class is defined first, and single loading is realized. Defining a class method startMonitor in the class, calling a swizzleNetwork method for NSURLSession extension in the class, and defining a dictionary performanceduc in the class for storing the collected network request parameters.
In S204, the exchanged callback method calls setMetrics, forTask and setError, withTask and method in DYNetworkmonitor respectively to transmit the acquired network request parameters.
And saving the acquired parameters to the dictionary PerformanceDic in the step S205 is realized through the step S204. Specifically, the network request task is used as a key, and the request parameter (performance parameter) is used as a value. Thus, according to the request task, the parameter information of each request can be taken. Optionally, a method setError and withTask is defined, wherein the method entry is a request error and a network request task, and is implemented by taking performance parameters keyed by the network request task from the PerformanceDic and removing the performance parameters.
Preferably, the method further comprises converting and uploading the performance parameters to a server.
Preferably, an asynchronous serial queue is created in the defined DYNetworkMonitor, all methods in the class are put into the queue, and data contention caused by operating PerformanceDic is avoided when multithreading is concurrent.
In the embodiment of the invention, the callback function in the proxy object is replaced, and after the data is intercepted, the original method is called back to execute, so that the influence on the system and the application program is reduced.
Example three:
the above mainly describes a network performance parameter acquisition method, and a detailed description will be given below of a network performance acquisition terminal device.
Fig. 3 shows a schematic structural diagram of a network performance parameter acquisition terminal device, including:
the definition module 310: a classification for defining NSURLSession, and setting an exchange method of network requests in the classification;
the classification is used for adding a new method on the basis of the original class, expanding the attribute and specifically representing a structural body pointer pointing to the classification. In the embodiment of the invention, classification of the system class NSURLSession is defined, and the function of NSURLSession can be expanded, thereby realizing a customized exchange method.
The switching module 320: when the terminal starts a predetermined application, the proxy object in NSURLSession is acquired through the classification, and a callback method in the proxy object is dynamically exchanged;
the proxy object is an instantiated network request and an object of NSURLSession from new, and the object can process network interaction tasks and information content. The callback method is a callback function executed by default of the system, and specifically comprises a method for creating NSURLSession, a call after collecting network performance parameters and a call after completing a network request
Optionally, theswitching module 320 further includes:
a definition unit: the method is used for defining a tool class, and the substitution of the NSURLSession creating method and the substitution of the proxy object callback method are respectively defined in the tool class.
The tool class is mainly used for realizing the replacement of the proxy object and the replacement of the callback method, the proxy object after the replacement can acquire the original callback method and then exchange the callback method, and the exchanged callback method can transmit network request information. In the embodiment of the invention, a class method for replacing the proxy object and a class method for replacing the callback method are directly defined in the tool class, and both the two class methods are executed in a runtime function, so that dynamic exchange can be ensured, and a network request is acquired on the basis of not influencing an original code.
Optionally, the functions of theswitching module 320 further include: defining a class by user, realizing the class in a singleton mode, and setting an exchange method for realizing the classification in the class.
Through the user-defined class, when the application is set and started, the exchange method in the tool class is called, and the callback function is dynamically replaced, so that the intercepted network request can be collected after the callback method is replaced, and meanwhile, other functions in the original request can be continuously executed after the callback method after the replacement is executed, so that the interference to the normal network request is avoided.
Preferably, an asynchronous serial queue is created inside the class, and all methods in the class are placed in the asynchronous serial queue.
The storage module 330: and when the network request of the predetermined application is detected, the network request is stored into a predefined dictionary through an exchanged callback method.
By the callback method for replacing the proxy object by the proxy object, the replaced callback function can acquire the content of the network request parameter, and the default callback function set by the system can be executed after the replaced callback function is executed. The replaced callback function may collect and transmit the received network performance parameters.
Optionally, thestorage module 330 includes:
a setting unit: and the network request task is set as a key, and the network performance parameter is stored in the dictionary as a value.
Optionally, thestorage module 330 further includes:
a removing unit: and the network request task processing module is used for acquiring the wrong request in the network request task, removing the wrong network request task and the corresponding network request parameter from the dictionary, converting the data in the dictionary and uploading the converted data to a server.
Example four:
fig. 4 is a schematic diagram of a structure of a network performance parameter acquisition terminal device according to an embodiment of the present invention. The terminal device is a mobile computer device with a touch screen, and includes but is not limited to a smart phone, a smart watch, a notebook, a tablet computer, a POS machine, and even a vehicle-mounted computer. As shown in fig. 4, theserver 4 of this embodiment includes: amemory 410, aprocessor 420, and asystem bus 430, thememory 410 including anexecutable program 4101 stored thereon, it being understood by those skilled in the art that the server architecture shown in FIG. 4 does not constitute a limitation of a server and may include more or fewer components than shown, or some components in combination, or a different arrangement of components.
The following specifically describes each constituent component of the terminal device with reference to fig. 4:
thememory 410 may be used to store software programs and modules, and theprocessor 420 performs various functional applications of the terminal and data processing by operating the software programs and modules stored in thememory 410. Thememory 410 may mainly include a storage program area and a storage data area, wherein the storage program area may store an operating system, an application program required by at least one function (such as a sound playing function, an image playing function, etc.), and the like; the storage data area may store data (such as audio data, a phonebook, etc.) created according to the use of the server, and the like. Further, thememory 410 may include high speed random access memory, and may also include non-volatile memory, such as at least one magnetic disk storage device, flash memory device, or other volatile solid state storage device.
Theexecutable program 4101 of the network performance parameter collection method is contained in thememory 410, and theexecutable program 4101 may be divided into one or more modules/units, which are stored in thememory 410 and executed by theprocessor 420 to complete the transmission of the notification and obtain the notification implementation process, and may be a series of computer program instruction segments capable of performing specific functions, and the instruction segments are used for describing the execution process of thecomputer program 4101 in theserver 4. For example, thecomputer program 4101 may be divided into a definition module, a switching module, and a storage module.
Theprocessor 420 is a control center of the server, connects various parts of the entire terminal device using various interfaces and lines, performs various functions of the terminal and processes data by operating or executing software programs and/or modules stored in thememory 410 and calling data stored in thememory 410, thereby integrally monitoring the terminal. Alternatively,processor 420 may include one or more processing units; preferably, theprocessor 420 may integrate an application processor, which mainly handles operating systems, application programs, etc., and a modem processor, which mainly handles wireless communications. It will be appreciated that the modem processor described above may not be integrated intoprocessor 420.
Thesystem bus 430 is used to connect functional units inside the computer, and can transmit data information, address information, and control information, and may be, for example, a PCI bus, an ISA bus, a VESA bus, etc. The instructions of theprocessor 420 are transmitted to thememory 410 through the bus, thememory 410 feeds data back to theprocessor 420, and thesystem bus 430 is responsible for data and instruction interaction between theprocessor 420 and thememory 410. Of course, thesystem bus 430 may also access other devices such as network interfaces, display devices, and the like.
The server at least includes a CPU, a chipset, a memory, a disk system, and the like, and other components are not described herein again.
In this embodiment of the present invention, the executable program executed by theprocessor 420 included in the server specifically includes:
a network performance parameter acquisition method comprises the following steps:
defining classification of NSURLSession, and setting an exchange method of network requests in the classification;
when the terminal starts a predetermined application, acquiring a proxy object in NSURLSession through the classification, and dynamically exchanging a callback method in the proxy object;
and when the network request of the predetermined application is detected, storing the network request into a predefined dictionary through an exchanged callback method.
Further, when the terminal starts a predetermined application, the method further includes, before the step of acquiring a proxy object in nserlession by the classification and dynamically exchanging a callback in the proxy object:
and defining a tool class, wherein a class method created by replacing the NSURLSessen and a callback method of the proxy object by a runtime function are respectively defined in the tool class.
Further, the method for acquiring the proxy object in the nsursursession through the classification and dynamically exchanging the callback in the proxy object includes:
defining a class by user, loading the class by single instance, and setting an exchange method for realizing the classification in the class.
Further, the exchange method for defining a class by user, loading the class in a single instance, and setting the class to implement the classification further includes:
and creating an asynchronous serial queue in the class, and putting all methods in the class into the asynchronous serial queue.
Further, the storing the network request into a predefined dictionary by the exchanged callback method specifically includes:
and setting the network request task as a key and storing the network performance parameter as a value in the dictionary.
Further, the setting the network request task as a key and the storing the network performance parameter as a value in the dictionary further includes:
and acquiring an error request in the network request task, removing the error network request task and the corresponding network request parameter from the dictionary, and converting and uploading data in the dictionary to a server.
It is clear to those skilled in the art that, for convenience and brevity of description, the specific working processes of the above-described systems, apparatuses and units may refer to the corresponding processes in the foregoing method embodiments, and are not described herein again.
In the above embodiments, the descriptions of the respective embodiments have respective emphasis, and reference may be made to the related descriptions of other embodiments for parts that are not described or illustrated in a certain embodiment.
It is clear to those skilled in the art that, for convenience and brevity of description, the specific working processes of the above-described systems, apparatuses and units may refer to the corresponding processes in the foregoing method embodiments, and are not described herein again.
In the above embodiments, the descriptions of the respective embodiments have respective emphasis, and reference may be made to the related descriptions of other embodiments for parts that are not described or illustrated in a certain embodiment.
The above-mentioned embodiments are only used for illustrating the technical solutions of the present invention, and not for limiting the same; although the present invention has been described in detail with reference to the foregoing embodiments, it will be understood by those of ordinary skill in the art that: the technical solutions described in the foregoing embodiments may still be modified, or some technical features may be equivalently replaced; and such modifications or substitutions do not depart from the spirit and scope of the corresponding technical solutions of the embodiments of the present invention.