Code performance detection method and device, equipment and storage medium

By injecting performance statistics code into JavaScript code, detailed performance data of functions and statement blocks can be obtained, solving the problem of low accuracy of code performance detection results in existing technologies and achieving more accurate optimization goals.

CN110990271BActive Publication Date: 2026-02-06WEBANK (CHINA)
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN201911190460.4
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2019-11-28
Publication Date
2026-02-06
Estimated Expiration
2039-11-28

AI Technical Summary

Technical Problem

Existing JavaScript code performance testing methods cannot accurately identify the specific reasons for poor performance, resulting in low accuracy of the test results. This is especially true for medium or large programs where it is difficult to refine the analysis down to the function level.

Method used

By obtaining preset performance statistics code, the location of the target function and statement block in the source code to be tested is injected to obtain detailed performance data, including line count and execution time, and to identify the functions and statements to be optimized.

Benefits of technology

It improves the accuracy of code performance testing, enabling quick identification of specific reasons for poor performance, and is especially helpful for less experienced programmers to conduct more detailed performance analysis and optimization.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN110990271B_ABST
    Figure CN110990271B_ABST
Patent Text Reader

Abstract

The application discloses a code performance detection method, device and equipment and a storage medium. The method comprises the following steps: obtaining preset performance statistical code capable of detecting the performance of a target statement block, so that the object of code performance detection is refined to part of statements in to-be-detected source code, and the limitation that only functions in to-be-detected code can be taken as the minimum refined object in the prior art is broken; injecting the preset performance statistical code into the to-be-detected source code, so that the performance of the target statement block in the to-be-detected source code can be detected through the preset performance statistical code; obtaining first and second performance statistical data, so that the execution condition inside a function in the target code can be understood, and the target code can be subjected to more detailed performance analysis compared with the prior art, and the accuracy of the code performance detection result is improved.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application relates to the technical field of Fintech, and in particular to a code performance detection method and device, equipment and a computer readable storage medium. BACKGROUND

[0002] With the development of computer technology, more and more technologies are applied in the financial field, and the traditional financial industry is gradually changing to Fintech, but due to the security and real-time requirements of the financial industry, higher requirements are also put forward for the technology. For the front-end system of a bank, front-end development usually uses JavaScript to write code, in order to improve the code running efficiency, it is necessary to detect the performance of the code written by JavaScript, and analyze the performance of the code through the detection result, and then optimize the code with poor performance. Since JavaScript is a parsing language, in order to collect runtime information for dynamic program analysis, there are two implementation schemes in the prior art: one is to use the interface provided by the JavaScript engine to collect running data to realize the export of JavaScript code running data within a period of time; the other is to inject running data collection code into the compiled source code to collect running data. But the first implementation scheme is only suitable for large and complex project code, and the performance statistical data of the functions in the detected code is obtained, so it can only reflect the performance of the functions of the detected code; and the second implementation scheme is designed for small code fragments, so it cannot be used for performance detection of large or medium-sized program code, and the second implementation scheme can only compare the performance of the detected code fragments, and the user can only qualitatively judge the performance of the detected multiple code fragments through the detection result, and the specific reason for the poor performance of the code fragment cannot be known from the above two implementation schemes, thereby causing the problem of low accuracy of the existing code performance detection result. SUMMARY

[0003] The main purpose of the present application is to provide a code performance detection method, device, equipment and computer readable storage medium, which solves the technical problem of low accuracy of the existing code performance detection result.

[0004] To achieve the above purpose, the present application provides a code performance detection method, which comprises:

[0005] When the performance detection instruction is received, the source code to be detected and the preset performance statistical code are obtained, and the target function in the source code to be detected and the target statement block in the target function are determined;

[0006] inject the performance statistical code into a target injection position in the to-be-detected source code based on the target function and the target statement block in the target position information in the to-be-detected source code, and take the to-be-detected source code after the injection as a target code;

[0007] execute the target code, and respectively acquire first performance data of the target function and second performance data of the target statement block based on the execution statistical code and the line count statistical code in the performance statistical code;

[0008] determine a to-be-optimized function in the target function and a to-be-optimized statement in the to-be-optimized function according to the first performance data and the second performance data.

[0009] Optionally, the step of determining the target function in the to-be-detected source code and the target statement block in the target function specifically comprises:

[0010] acquire a first regular expression related to the target function and a second regular expression corresponding to the target statement block from the performance statistical code, and match the first regular expression and the second regular expression with the to-be-detected source code line by line;

[0011] when there is a first partial code matching the first regular expression in the to-be-detected source code, determine that the first partial code is an execution statement of the target function, and determine the target function according to the execution statement;

[0012] when there is a second partial code matching the second regular expression in the to-be-detected source code, determine that the second partial code is the target statement block.

[0013] Optionally, the step of injecting the performance statistical code into a target injection position in the to-be-detected source code based on the target function and the target statement block in the target position information in the to-be-detected source code, and taking the to-be-detected source code after the injection as a target code specifically comprises:

[0014] acquire first position information of the execution statement and second position information of the target statement block, and take the first position information and the second position information as the target position information;

[0015] determine a first injection position and a second injection position corresponding to the execution statistical code and the line count statistical code respectively based on the target position information, and take the first injection position and the second injection position as the target injection position;

[0016] inject the performance statistical code into the target injection position, and take the to-be-detected source code after the injection as the target code.

[0017] Optionally, after the step of obtaining the first position information of the execution statement and the second position information of the target statement block, and taking the first position information and the second position information as the target position information, the method further comprises:

[0018] determining whether the target statement block contains a specific statement;

[0019] if the target statement block contains the specific statement, taking a position of the specific statement as the second injection position, and injecting the line count statistical code into the second injection position;

[0020] if the target statement block does not contain the specific statement, adding the specific statement in the target statement block, and repeating the step of taking a position of the specific statement as the second injection position, and injecting the line count statistical code into the second injection position.

[0021] Optionally, the step of determining the function to be optimized in the target function and the statement to be optimized in the function to be optimized according to the first performance data and the second performance data comprises:

[0022] obtaining the function execution times and the function execution time in the first performance data, and determining whether the function execution times and the function execution time exceed preset first and second thresholds, respectively;

[0023] if the function execution times exceed the first threshold and the function execution time exceeds the second threshold, determining that the function corresponding to the data exceeding the first and second thresholds is the function to be optimized;

[0024] obtaining the target statement block execution times corresponding to the function to be optimized in the second performance data, and determining whether the target statement block execution times exceed a preset third threshold;

[0025] if the target statement block execution times exceed the third threshold, determining that the statement corresponding to the data exceeding the third threshold is the statement to be optimized.

[0026] Optionally, the step of obtaining the source code to be detected and the preset performance statistical code, and determining the target function in the source code to be detected and the target statement block in the target function when the performance detection instruction is received comprises:

[0027] when the performance detection instruction is received, obtaining the original code in the performance detection instruction, and determining whether there is a function to be executed in the original code;

[0028] If the original code contains the function to be executed, it is determined that the original code is the source code to be detected, and the performance statistics code is obtained.

[0029] According to the performance statistics code, the target function and the target statement block are determined.

[0030] Optionally, before the step of determining the function to be optimized in the target function and the statement to be optimized in the function to be optimized according to the first performance data and the second performance data, the method further comprises:

[0031] According to the preset weight, the function execution times and the function execution time of the target function, a performance reference value of each target function is generated, wherein the function execution times and the function execution time are the second performance data.

[0032] The target function name and the corresponding function execution times and function execution time are displayed in descending order of the performance reference value.

[0033] The target statement block name and the corresponding target statement block execution times are displayed in descending order of the target statement block execution times, wherein the target statement block execution times are the first performance data.

[0034] The code performance detection method, the code detection method further comprises:

[0035] The tool function in the target code and the performance statistics function in the performance statistics code are obtained, and the tool function and the performance statistics function are used as a statistical tool function.

[0036] The statistical tool function is written into a specified file, so that the performance data of the next code to be detected is counted based on the statistical tool function in the specified file.

[0037] In addition, to achieve the above object, the application also provides a code performance detection device, which comprises:

[0038] The code performance detection device comprises:

[0039] The function statement determination module is configured to, when receiving a performance detection instruction, obtain the source code to be detected and the preset performance statistics code, and determine the target function in the source code to be detected and the target statement block in the target function.

[0040] The performance code injection module is configured to, based on the target position information of the target function and the target statement block in the source code to be detected, inject the performance statistics code into the target injection position in the source code to be detected, and take the injected source code to be detected as a target code.

[0041] a performance data obtaining module, configured to execute the target code, and obtain first performance data of the target function and second performance data of the target statement block based on execution statistical code and line count statistical code in the performance statistical code respectively;

[0042] an optimization statement determining module, configured to determine a function to be optimized in the target function and a statement to be optimized in the function to be optimized according to the first performance data and the second performance data.

[0043] Optionally, the code performance detection apparatus further comprises:

[0044] a regular obtaining and matching module, configured to obtain a first regular expression related to the target function and a second regular expression corresponding to the target statement block from the performance statistical code, and match the first regular expression and the second regular expression with the source code to be detected line by line;

[0045] a first regular matching module, configured to determine a first part of code in the source code to be detected as an execution statement of the target function when the first part of code matches the first regular expression, and determine the target function according to the execution statement;

[0046] a second regular matching module, configured to determine a second part of code in the source code to be detected as the target statement block when the second part of code matches the second regular expression.

[0047] Optionally, the performance code injecting module specifically comprises:

[0048] a position information obtaining unit, configured to obtain first position information of the execution statement and second position information of the target statement block, and take the first position information and the second position information as the target position information;

[0049] an injection position determining unit, configured to determine first injection position and second injection position corresponding to the execution statistical code and the line count statistical code respectively based on the target position information, and take the first injection position and the second injection position as the target injection position;

[0050] a performance code injecting unit, configured to inject the performance statistical code into the target injection position, and take the source code to be detected after injection as the target code.

[0051] Optionally, the code performance detection apparatus further comprises:

[0052] a specific statement determining module, configured to determine whether a specific statement exists in the target statement block;

[0053] The code direct injection module is configured to, if the specific statement exists in the target statement block, take a position of the specific statement as the second injection position, and inject the line count statistics code into the second injection position.

[0054] The code indirect injection module is configured to, if the specific statement does not exist in the target statement block, add the specific statement in the target statement block, and repeatedly execute the steps of taking a position of the specific statement as the second injection position, and injecting the line count statistics code into the second injection position.

[0055] Optionally, the performance data acquisition module specifically comprises:

[0056] The function data judgment unit is configured to acquire a function execution times and a function execution time in the first performance data, and judge whether the function execution times and the function execution time exceed preset first threshold and preset second threshold respectively.

[0057] The optimized function determination unit is configured to, if the function execution times exceed the first threshold and the function execution time exceeds the second threshold, determine that a function corresponding to the data exceeding the first threshold and the second threshold is a function to be optimized.

[0058] The statement data judgment unit is configured to acquire a target statement block execution times corresponding to the function to be optimized in the second performance data, and judge whether the target statement block execution times exceed a preset third threshold.

[0059] The optimized statement determination unit is configured to, if the target statement block execution times exceed the third threshold, determine that a statement corresponding to the data exceeding the third threshold is a statement to be optimized.

[0060] In addition, to achieve the above object, the present application further provides a code performance detection device, which comprises a memory, a processor and a code performance detection program stored in the memory and executable on the processor, and the code performance detection program realizes the steps of the code performance detection method when executed by the processor.

[0061] In addition, to achieve the above object, the present application further provides a computer readable storage medium, which stores a code performance detection program, and the code performance detection program realizes the steps of the code performance detection method when executed by a processor.

[0062] This invention obtains preset performance statistics code containing information that can locate specific statements in the code and thus detect the performance of target statement blocks. This refines the object of code performance detection to a portion of the source code to be tested, overcoming the limitation of existing technologies that can only use functions in the source code as the smallest refined object. By injecting the preset performance statistics code into the source code to be tested, the performance of the target statement block in the source code can be detected through the preset performance statistics code. Based on the obtained first and second performance statistics data, users can understand the execution status inside the function in the target code and perform a more detailed performance analysis of the target code based on this execution status compared to existing solutions. By identifying the statements to be optimized in the function to be optimized, users can obtain a more precise optimization target than the entire function. Especially for inexperienced programmers, it is difficult to analyze the determining factors of the performance of the target code based on existing code performance detection results. However, this invention can directly obtain the execution status of the target statement block and quickly analyze the specific reasons for poor code performance, improving the accuracy of code performance detection results and thus solving the technical problem of low accuracy in existing code performance detection results. Attached Figure Description

[0063] Figure 1 This is a schematic diagram of the device structure of the hardware operating environment involved in the embodiments of the present invention;

[0064] Figure 2 This is a flowchart illustrating the first embodiment of the code performance testing method of the present invention.

[0065] The realization of the objective, functional features and advantages of the present invention will be further explained in conjunction with the embodiments and with reference to the accompanying drawings. Detailed Implementation

[0066] It should be understood that the specific embodiments described herein are merely illustrative of the invention and are not intended to limit the invention.

[0067] like Figure 1 As shown, Figure 1 This is a schematic diagram of the device structure of the hardware operating environment involved in the embodiments of the present invention.

[0068] The code performance testing device in this embodiment of the invention can be a PC or a server device on which a Java Virtual Machine runs.

[0069] like Figure 1As shown in the figure, the code performance detection apparatus can include a processor 1001, such as a CPU, a network interface 1004, a programmer interface 1003, a memory 1005, and a communication bus 1002. The communication bus 1002 is used to realize the connection and communication among the components. The programmer interface 1003 can include a display screen (Display) and an input unit such as a keyboard (Keyboard). The optional programmer interface 1003 can further include a standard wired interface and a wireless interface. The network interface 1004 can optionally include a standard wired interface and a wireless interface (such as a WI-FI interface). The memory 1005 can be a high-speed RAM memory or a stable memory (non-volatile memory), such as a disk memory. The memory 1005 can optionally be a storage device independent of the aforementioned processor 1001.

[0070] Those skilled in the art can understand that, Figure 1 The device structure shown in the figure does not constitute a limitation on the device, and can include more or fewer components than the figure, or combine certain components, or different component arrangements.

[0071] As Figure 1 As shown in the figure, the memory 1005 as a computer storage medium can include an operating system, a network communication module, a programmer interface module, and a code performance detection program.

[0072] In Figure 1 In the device shown in the figure, the network interface 1004 is mainly used to connect to a background server and communicate data with the background server; the programmer interface 1003 is mainly used to connect to a client (programmer end) and communicate data with the client; and the processor 1001 can be used to call the code performance detection program stored in the memory 1005 and perform the operations in the following code performance detection method.

[0073] Based on the above hardware structure, the code performance detection method embodiment of the present application is proposed.

[0074] Referring to Figure 2 , Figure 2 The flowchart of the first embodiment of the code performance detection method of the present application is shown in the figure. The code performance detection method includes:

[0075] Step S10, when the performance detection instruction is received, the source code to be detected and the preset performance statistical code are acquired, and the target function in the source code to be detected and the target statement block in the target function are determined;

[0076] At present, for the code written in JavaScript, to collect runtime information for dynamic program analysis, there are two implementation schemes in the prior art: one is to realize the export of JavaScript code running data within a period of time through the interface provided by the JavaScript engine for collecting running data; the other is to inject running data collection code into the compiled source code to collect running data. But the first implementation scheme is only applicable to large and complex project code, and the performance statistical data of the functions in the detected code is obtained, so only the performance advantages and disadvantages of the functions of the detected code can be reflected; and the second implementation scheme is designed for small code fragments, so it cannot be used for performance detection of large or medium-sized program code, and only the performance advantages and disadvantages of the detected code fragments can be compared by using the second implementation scheme, and the user can only qualitatively judge the performance advantages and disadvantages of the multiple code fragments detected by the detection result, and cannot know the specific reasons for the poor performance of the code fragments, thereby causing the problem of low accuracy of the existing code performance detection result. For example, the current source code to be detected is a medium-sized program code containing a large function coupled with multiple functions, if v8-profiler is used to perform performance analysis on this source code, a long time will be consumed to obtain the execution statistical data of this source code, and in the execution statistical data, the CPU occupancy rate and other data are not helpful for the performance analysis of small and medium-sized program codes, and the execution data about the functions in the program in the execution statistical data can only qualitatively judge the performance advantages and disadvantages of the functions. At this time, the programmer knows from the detection result that the performance of this large function is poor, which seriously affects the performance of the entire program, but from the detection result, more detailed information cannot be obtained to determine the specific reasons for the poor performance, and the programmer needs to spend a long time to analyze which key statements in the large function can be changed.

[0077] In the embodiment, to solve the above problems, the application provides a code performance detection method, that is, the application obtains preset performance statistical code containing the target statement block performance that can be detected, so that the object of code performance detection is refined to part of the statements in the source code to be detected, breaking the limitation in the prior art that only functions in the code to be detected can be taken as the minimum refined object; the preset performance statistical code is injected into the source code to be detected, so that the performance of the target statement block in the source code to be detected can be detected through the preset performance statistical code; the first and second performance statistical data are obtained, so that the user can know the execution of the functions in the target code, and the target code can be analyzed in a more detailed performance compared with the prior art; the to-be-optimized statement in the to-be-optimized function is determined, so that the user can obtain a more accurate optimization target than the entire function, especially for programmers with insufficient experience, it is difficult to analyze the determining factors of the performance of the target code through the existing code performance detection result, but the execution of the target statement block can be directly obtained through the application, and the specific reason causing the poor code performance can be quickly analyzed, the accuracy of the code performance detection result is improved, and the technical problem of low accuracy of the existing code performance detection result is solved. The code performance detection method is applied to a terminal equipped with Node.js (a JavaScript runtime environment based on Chrome V8 engine).

[0078] The performance detection instruction can be initiated by the programmer importing the source code to be detected and the preset performance statistical code, or automatically initiated by the terminal according to the preset instruction. The preset performance statistical code includes line counting statistical code, execution statistical code and related regular expressions, is used for matching the target statement block and the target function in the source code to be detected and detecting the performance thereof, and is a performance statistical code for most codes that is written in advance; for a small part of complex source code, the preset performance statistical code can be optimized in advance and then used. The target statement block includes but is not limited to loop statements, selection statements, judgment statements, return statements and other key statements. The target function is all functions that need to be executed in the source code to be detected. Specifically, the user initiates a code performance detection instruction to a computer equipped with Node.js by importing a current medium program code as the source code to be detected and the general performance statistical code. The computer receives the code performance detection instruction, obtains the source code to be detected and the preset performance statistical code in the performance detection instruction. The computer obtains the regular expression from the preset performance statistical code, and matches the source code to be detected line by line through the regular expression, so as to determine the target function in the source code to be detected and the key statements in the target function.

[0079] Step S20, based on the target function and the target sentence block in the target position information in the source code to be detected, the performance statistics code is injected into the target injection position in the source code to be detected, and the injected source code to be detected is taken as the target code;

[0080] The target position information includes first position information of the target function in the source code to be detected and second position information of the target sentence block in the source code to be detected. The target injection position includes a first injection position of the execution statistics code in the source code to be detected and a second injection position of the line counting statistics code in the source code to be detected.

[0081] In the embodiment, the terminal equipped with Node.js injects the performance statistics code into the corresponding position of the source code to be detected. It should be noted that the semantics of the source code to be detected will not change before and after the performance statistics code is injected. Specifically, the performance statistics code includes line counting statistics code and execution statistics code. The line counting statistics code records the number of statement executions by injecting a global object count in the execution context, taking the statement type type + source line number line + source column number column as the object key key, and taking the execution frequency as the value value. The statement type includes if, else if, else, for, while, return, and other key statements. When counting for the first time, the key-value pair is initialized to 1, and subsequent values are continuously stacked. The execution statistics code obtains the start time and end time of the system before and after the function by injecting performance.now() statements before and after the function. The execution time of the corresponding function can be obtained by subtracting the two values, and each time the execution time is obtained, it indicates that the function is executed once. Thus, the function execution time and execution frequency can be obtained. After obtaining the execution time and execution frequency, the global variable profile records the function execution frequency and execution time. The computer encapsulates the line counting statistics code into the lineCounter function and receives three parameters type (statement type), line (source line number), and column (source column number). This function returns the undefined parameter. The execution statistics code is also encapsulated into the functionProfiler function, which receives two parameters: functionName (function name) and time (function execution time). It should be noted that in order to prevent naming conflicts, the built-in functions of JavaScript will be added with the corresponding object name. For example, Math.sqrt will be converted to Math_sqrt, and anonymous functions will be replaced with _selfExecution. The computer injects the line counting statistics code into the corresponding position of the key statements such as loop statements, judgment statements, and return statements in the source code to be detected, and injects the execution statistics code into the corresponding position before and after the execution function in the source code to be detected.

[0082] In more embodiments, the injection manner of the preset performance statistical code can also be to analyze the source code to be detected into an abstract syntax tree, then inject the corresponding target statement block into the preset performance statistical code, and finally generate target code; or this function can be sunk to a JavaScript parsing engine, and the JavaScript parsing engine outputs the corresponding line count result. However, the parsing process of the abstract syntax tree needs to spend manpower and material resources, and the cost is relatively high, and the JavaScript parsing engine needs to embed the target code into the engine, analyze the code online, and may affect the performance of the code.

[0083] In step S30, the target code is executed, and first performance data of the target function and second performance data of the target statement block are respectively obtained based on the execution statistical code and the line count statistical code in the performance statistical code.

[0084] The execution statistical code is used to count the first performance data of the target function in the source code to be detected, and the line count statistical code is used to count the second performance data of the target statement block in the target function. The first performance data can be the execution times and execution time of the target statement block. The second performance data includes but is not limited to the execution times, total execution time and average execution time of the target function.

[0085] In this embodiment, the terminal executes the target code based on the running environment of Node.js, and obtains the performance statistical data in the execution result. Specifically, in the execution process of the target code, the computer reads the data of the global object count in the target code, parses the execution times, line number and column number of the target statement block, that is, the value value and the key key line and column, and writes the execution times in front of the corresponding line of the target code. Then the data of the global object profile in the target code is read, the total execution time and execution times of the corresponding function are parsed, and the average execution time corresponding to the function is obtained through the total execution time and the execution times. It should be noted that in the present application, only the performance of the entire function and the target statement block, that is, the key statement, is detected, and the performance of all statements in the function is not detected, because the detection of all statements takes a long time, and because the detection of all statements obtains most of the detection data which is not helpful for the analysis of the problem, and may interfere with the analysis of the problem.

[0086] In step S40, according to the first performance data and the second performance data, the function to be optimized in the target function and the statement to be optimized in the function to be optimized are determined.

[0087] In the embodiment, the function to be optimized and the statement to be optimized can be determined by the terminal after analyzing the first performance data and the second performance data, or can be determined by the user after the first performance data and the second performance data are displayed to the user. Specifically, the computer analyzes the first performance data, determines the function corresponding to the data satisfying the preset condition as the function to be optimized, obtains the data corresponding to the function to be optimized in the second performance data, and analyzes the data corresponding to the function to be optimized, and determines the statement corresponding to the data exceeding the preset threshold in the target statement block as the statement to be optimized.

[0088] The preset performance statistical code containing the specific statement capable of being positioned to the code and further detecting the performance of the target statement block is obtained, so that the object of the code performance detection is refined to part of the statements in the source code to be detected, and the limitation that the function in the code to be detected can only be taken as the minimum refined object in the prior art is broken. The preset performance statistical code is injected into the source code to be detected, so that the performance of the target statement block in the source code to be detected can be detected through the preset performance statistical code. According to the obtained first and second performance statistical data, the user can understand the execution of the function in the target code, and can perform more detailed performance analysis on the target code according to the execution. The statement to be optimized in the function to be optimized is determined, so that the user can obtain a more accurate optimization target than the entire function. Especially for programmers with insufficient experience, it is difficult to analyze the determining factors of the performance of the target code according to the existing code performance detection result. According to the present application, the execution of the target statement block can be directly obtained, and the specific reason causing poor code performance can be quickly analyzed, the accuracy of the code performance detection result is improved, and the technical problem of low accuracy of the existing code performance detection result is solved.

[0089] Not shown in the figure, based on the above Figure 2 The first embodiment is shown, and the second embodiment of the code performance detection method of the present application is proposed.

[0090] In the embodiment, step S10 specifically includes:

[0091] Step a, obtaining the first regular expression related to the target function and the second regular expression corresponding to the target statement block from the performance statistical code, and matching the first regular expression and the second regular expression with the source code to be detected line by line;

[0092] In the embodiment, the programmer writes the first regular expression corresponding to the function execution code in the source code to be detected and the second regular expression corresponding to the key statement by analyzing the semantic environment in which the target statement block is located, and inputs the regular expressions into the terminal. The terminal acquires the first and second regular expressions, and matches each line of code by using the first and second regular expressions in the process of executing the target code. Specifically, taking the selection statement if as an example, the if statement has two forms in the code, one is if(){} and the other is if(){}. The programmer writes the regular expression corresponding to the if statement, that is, / $\s+if\s+?\(*.\) / , and the computer matches each line of code by using the regular expression to find the if statement in the source code to be detected.

[0093] Step b, when the first part of code matching the first regular expression exists in the source code to be detected, determining that the first part of code is the execution statement of the target function, and determining the target function according to the execution statement.

[0094] In the embodiment, if the matching process ends and the result shows that the first part of code matching the first regular expression exists in the source code to be detected, the terminal can determine that the first part of code is the execution statement of the function in the source code to be detected. Specifically, if the matching result shows that six codes matching the first regular expression exist in the source code to be detected, it is determined that the six codes are all execution statements of the function, that is, six functions to be executed exist in the source code to be detected.

[0095] Step c, when the second part of code matching the second regular expression exists in the source code to be detected, determining that the second part of code is the target statement block.

[0096] In the embodiment, if the matching process ends and the result shows that the second part of code matching the regular expression exists in the source code to be detected, the terminal can determine that the second part of code is the target statement block to be found. Specifically, the computer matches each line of code in the source code to be detected by using / $\s+if\s+?\(*.\) / , and the matching result shows that five codes matching the regular expression exist in the source code to be detected, so it is determined that the five codes are all codes of the if statement.

[0097] Further, in the embodiment, after step c, the method further includes:

[0098] Step d, acquiring the first position information of the execution statement and the second position information of the target statement block, and taking the first position information and the second position information as the target position information.

[0099] In the embodiment, the terminal acquires first position information of the target statement block in the source code to be detected, where the first position information is usually the line number and column number of the function execution statement. The terminal also acquires second position information of the target statement block in the source code to be detected, where the second position information is usually the line number and column number of the target statement block.

[0100] In step e, based on the target position information, the terminal determines a first injection position and a second injection position corresponding to the execution statistical code and the line count statistical code respectively, and takes the first injection position and the second injection position as the target injection position.

[0101] In the embodiment, the first injection position is different in different types of key statements, and the second injection position is before and after the function execution statement.

[0102] In the embodiment, the terminal determines the first injection position of the line count statistical code and the second injection position of the execution statistical code in the source code to be detected according to the first position information and the second position information. Specifically, the computer matches the source code to be detected line by line using the first regular expression, determines the function execution code corresponding to the function according to the matching result, and injects the statement const__start = performance.now() before the function execution code and the statement const__end = performance.now() after the function execution code. The positions before and after the function execution code are the first injection positions. Taking the judgment statement if as an example, the computer acquires the line number and column number of the judgment statement if(), and replaces the first "(" in the if() statement with the lineCounter function according to the line number and column number. The position is the second injection position, and the injected code is written into the js_profile_code.js file. In more embodiments, the judgment statements else if, for, and while all have "()" to determine whether the judgment statement is executed. Therefore, the injection method is the same as that of the if statement. For the return statement, the computer directly inserts the lineCounter function before the matching and determined return statement to complete the injection.

[0103] In step f, the terminal injects the performance statistical code into the target injection position, and takes the injected source code to be detected as the target code.

[0104] In the embodiment, the terminal injects the execution statistical code into the determined first injection position, and injects the line count statistical code into the determined second injection position. After completing the injection operation, the terminal takes the source code to be detected injected with the execution statistical code and the line count statistical code as the target code to be run.

[0105] Further, after step d, further comprising:

[0106] Step g, judging whether the target sentence block has a specific sentence;

[0107] In the embodiment, the terminal judges whether the code determined as the target sentence block has the specific sentence through detection and matching. The specific sentence is the sentence included by the else sentence of the key sentence.

[0108] Step h, if the target sentence block has the specific sentence, taking the position of the specific sentence as the first injection position, and injecting the line counting code into the first injection position;

[0109] In the embodiment, if the terminal judges that the target sentence block has the specific sentence, the line counting code is injected into the first injection position of the specific sentence. Specifically, if it is judged that the else sentence has the sentence included by the “{}”, the computer injects the lineCounter function into the sentence, and the position is the first injection position.

[0110] Step i, if the target sentence block does not have the specific sentence, adding the specific sentence in the target sentence block, and repeating the step of taking the position of the specific sentence as the first injection position, and injecting the line counting code into the first injection position.

[0111] In the embodiment, if the terminal judges that the target sentence block does not have the specific sentence, the specific sentence is added into the target sentence block, and the line counting code is injected into the first injection position of the specific sentence. Specifically, if it is judged that the else sentence does not have the sentence included by the “{}”, the computer adds the specific sentence into the else sentence, and injects the line counting code into the first injection position of the specific sentence.

[0112] The application provides a code performance detection method, which further matches the execution sentence of the function and the target sentence block in the to-be-detected source code through the regular expression, improves the efficiency and accuracy of identifying the function and the target sentence block in the code, injects the line counting code and the execution counting code into the corresponding positions of the to-be-detected source code, enables the line counting code and the execution counting code to normally run and count the performance data of the target sentence block and the function, realizes more refined code performance detection, identifies the specific sentence, accurately determines the injection position of the line counting code in the else sentence, and improves the accuracy of code injection.

[0113] Not shown in the figure, based on the above Figure 2The first embodiment shown proposes a third embodiment of the code performance detection method of the present application.

[0114] In the present embodiment, step S40 specifically comprises:

[0115] Step j, obtaining the function execution times and function execution times in the first performance data, and determining whether the function execution times and function execution times exceed the preset first threshold and the preset second threshold, respectively;

[0116] The function execution time includes function total execution time and function average execution time. The preset first threshold is a value for determining the performance of the function execution times, and the preset second threshold is a time value for determining the performance of the function execution times. In the present embodiment, the preset second threshold can be set to two different values, i.e., the preset average execution time threshold and the preset total execution time threshold. The three thresholds can be flexibly set according to the specific situation, and the present embodiment does not make specific limitation.

[0117] In the present embodiment, after the terminal executes the statistical code into the corresponding position of the source code to be detected, the target code is run to obtain the execution times of all functions executed in the source code to be detected and the total execution time, and the average execution time of each function is calculated. These data are compared with the preset first threshold, the preset average execution time threshold and the preset total execution time threshold. Specifically, the computer obtains and displays the second performance data: the execution times of the main function is 1, the total execution time is 18.17299ms, and the average execution time is 18.17299ms; the execution times of the prime function is 999, the total execution time is 14.78644ms, and the average execution time is 0.01480ms; the execution times of the Math_sqrt function is 999, the total execution time is 0.41335ms, and the average execution time is 0.00041ms. Taking the preset average execution time threshold of 1ms, the preset first threshold of 500 and the preset total execution time threshold of 10ms as examples, it is determined whether the above data exceeds the threshold.

[0118] Step k, if the function execution times exceed the first threshold and the function execution times exceed the second threshold, the function corresponding to the data exceeding the first threshold and the second threshold is determined as the function to be optimized;

[0119] In the embodiment, the data in the above specific embodiments is used. From the data, it is determined that the function execution times, the function total execution time and the function average execution time in the main function exceed the threshold value, and the main function is determined as the function to be optimized; the function execution times and the function total execution time in the prime function exceed the threshold value, and the average execution time does not exceed the threshold value, and the prime function is determined as the function to be optimized; only the function execution times in the Math_sqrt function exceed the threshold value, and the function execution time and the function average execution time do not exceed the threshold value, and it is determined that the Math_sqrt function does not need to be optimized.

[0120] Step l, obtaining the target statement block execution times corresponding to the functions to be optimized in the second performance data, and determining whether the target statement block execution times exceed a preset third threshold value;

[0121] The preset third threshold value is a value for determining the execution times of the functions to be optimized, and is flexibly set according to specific conditions.

[0122] In the embodiment, the terminal obtains the execution times of the key statements corresponding to each function to be optimized according to the functions to be optimized determined in the target function. The terminal compares the preset third threshold value with the execution times of the key statements. Specifically, the above embodiment is used. It is set that the computer obtains that in the main function, the if statement is executed for 300 times, the for statement is executed for 50 times, and the return statement is executed for 50 times; in the prime function, the for statement is executed for 200 times, and the return statement is executed for 30 times; in the Math_sqrt function, the if statement is executed for 20 times, and the return statement is executed for 30 times. It is set that the preset third threshold value is 100, and the computer determines whether the above data exceeds the preset third threshold value.

[0123] Step m, if the target statement block execution times exceed the third threshold value, determining the statement corresponding to the data exceeding the third threshold value as the statement to be optimized.

[0124] In the embodiment, the terminal determines the statement corresponding to the data exceeding the preset third threshold value in the target statement block execution times as the statement to be optimized. Specifically, the data in the above specific embodiment is used. It is determined that the execution times of the if statement in the main function exceed the preset third threshold value 100, and the if statement in the main function is determined as the key statement to be optimized; the execution times of the for statement in the prime function exceed the preset third threshold value 100, and the for statement in the prime function is determined as the key statement to be optimized.

[0125] Further, in the embodiment, step S10 specifically includes:

[0126] Step n, upon receiving the performance detection instruction, obtaining the original code in the performance detection instruction, and determining whether the original code contains a function to be executed;

[0127] In this embodiment, a user initiates a performance detection instruction by importing an original code. The terminal receives the performance detection instruction, obtains the original code in the performance detection instruction, and determines whether the original code contains a function to be executed by recognizing characters or statements of the function. It should be noted that most program codes contain functions to be executed, and only some small code fragments may contain only a number of statements and do not contain functions to be executed.

[0128] Step o, if the original code contains the function to be executed, determining that the original code is the source code to be detected, and obtaining the performance statistical code;

[0129] In this embodiment, if the terminal determines that the original code contains a function to be executed, the original code is regarded as the source code to be detected, and the original code is detected for performance by the scheme in the present application. If the original code does not contain a function to be executed, it can be determined that the original code is a small code fragment composed of a number of statements.

[0130] Step p, determining the target function and the target statement block according to the performance statistical code.

[0131] In this embodiment, the terminal obtains a first regular expression and a second regular expression from the preset performance statistical code, and matches the source code to be detected with the first and second regular expressions line by line to determine the target function and the target statement block in the source code to be detected.

[0132] Further, in this embodiment, step S40 further includes:

[0133] Step q, generating a performance reference value of each target function according to a preset weight, a function execution frequency of the target function, and a function execution time, wherein the function execution frequency and the function execution time are the second performance data.

[0134] The preset weight includes a first weight corresponding to the function execution frequency and a second weight corresponding to the function execution total time. The first weight and the second weight can be flexibly set according to specific conditions.

[0135] In the embodiment, the terminal sorts the products of the first weight and the execution times of each function in descending order, obtains the first serial number of each target function, takes the first serial number as the numerator and the number of target functions as the denominator, and obtains the first weight score corresponding to each target function. The terminal sorts the products of the second weight and the total execution time of each function in descending order, obtains the second serial number of each target function, takes the second serial number as the numerator and the number of target functions as the denominator, and obtains the second weight score corresponding to each target function. The terminal takes the sum of the first weight score and the second weight score as the performance reference value of each target function.

[0136] Step r, displaying the target function name and the corresponding execution times and execution times according to the performance reference value from high to low;

[0137] In the embodiment, the terminal displays the function name of the function to be optimized in the current all execution functions, and the corresponding total execution time, execution times and average execution time according to the performance advantage and disadvantage weight of each target function generated after the determination from high to low. The user analyzes the performance of the target function through the above display result, and determines the function to be optimized in the target function.

[0138] Step s, displaying the target statement block name and the corresponding target statement block execution times in descending order of target statement block execution times, wherein the target statement block execution times are the first performance data.

[0139] In the embodiment, the terminal sorts the target statement blocks corresponding to each function according to the numerical value, and preferentially displays the target statement blocks with more execution times in each function and the execution times of each target statement block. After obtaining the above display result, the user analyzes each target statement block and determines the statement to be optimized in the target statement block. Specifically, if the execution times of the if statement in the main function are 300 times, the execution times of the else statement are 250 times, the execution times of the for statement are 200 times, and the execution times of the return statement are 400 times, the terminal displays in the order of return statement, main statement, if statement, else statement and for statement. The user can analyze the performance of the target statement according to the above display data.

[0140] Further, the code performance detection method further comprises:

[0141] Step t, obtaining the tool function in the target code and the performance statistical function in the performance statistical code, and taking the tool function and the performance statistical function as the statistical tool function;

[0142] The tool function is a method for processing the data after statistics, and the performance statistics function is a line count function and an execution statistics function.

[0143] In the embodiment, the terminal acquires the tool function in the target code and the line count function and the execution statistics function in the performance statistics code, and takes the tool function, the line count function and the execution statistics function as the statistical tool function.

[0144] In step u, the statistical tool function is written into a specified file, so that the performance data of the next code to be detected is counted based on the statistical tool function in the specified file.

[0145] In the embodiment, the terminal writes the statistical tool function into a file named js_profile_code.js, so that the statistical tool function can be directly called from the file js_profile_code.js when the function and the target statement block in the code are detected again.

[0146] The present application provides a code performance detection method, which further directly acquires the execution times of the target statement block in the function to be optimized, accurately locates the several statements that reduce the performance of the function to be optimized, improves the code performance analysis efficiency and saves the code performance analysis time; determines whether the original code is the code with the function to be executed, determines the best application range of the present application; preferentially displays the function and the target statement block with low performance, so that the programmer can more intuitively acquire the information of the related code with the longest time consumption and the most execution times, more quickly locates the specific function that causes the poor code performance, further locates the target statement block in the function to be optimized, further improves the code performance optimization efficiency and improves the user experience; the statistical tool function is summarized and written into a specific file, so that the related function can be directly called from the specific file when needed, and the working efficiency of the computer for the performance detection of the code is improved.

[0147] The present application also provides a code performance detection device, which comprises:

[0148] A function statement determination module is configured to acquire the source code to be detected and the preset performance statistics code when the performance detection instruction is received, and determine the target function in the source code to be detected and the target statement block in the target function.

[0149] A performance code injection module is configured to inject the performance statistics code into the target injection position in the source code to be detected based on the target position information of the target function and the target statement block in the source code to be detected, and take the injected source code to be detected as the target code.

[0150] a performance data obtaining module, configured to execute the target code, and obtain first performance data of the target function and second performance data of the target statement block based on execution statistical code and line count statistical code in the performance statistical code respectively;

[0151] an optimization statement determining module, configured to determine a function to be optimized in the target function and a statement to be optimized in the function to be optimized according to the first performance data and the second performance data.

[0152] The code performance detection apparatus further comprises:

[0153] a regular obtaining and matching module, configured to obtain a first regular expression related to the target function and a second regular expression corresponding to the target statement block from the performance statistical code, and match the first regular expression and the second regular expression with the source code to be detected line by line;

[0154] a first regular matching module, configured to determine a first part of code matched with the first regular expression in the source code to be detected as an execution statement of the target function, and determine the target function according to the execution statement;

[0155] a second regular matching module, configured to determine a second part of code matched with the second regular expression in the source code to be detected as the target statement block.

[0156] The performance code injecting module specifically comprises:

[0157] a position information obtaining unit, configured to obtain first position information of the execution statement and second position information of the target statement block, and take the first position information and the second position information as the target position information;

[0158] an injection position determining unit, configured to determine first injection position and second injection position corresponding to the execution statistical code and the line count statistical code respectively based on the target position information, and take the first injection position and the second injection position as the target injection position;

[0159] a performance code injecting unit, configured to inject the performance statistical code into the target injection position, and take the source code to be detected after injection as the target code.

[0160] The code performance detection apparatus further comprises:

[0161] a specific statement judging module, configured to judge whether a specific statement exists in the target statement block;

[0162] The code direct injection module is configured to, if the specific statement exists in the target statement block, take a position of the specific statement as the second injection position, and inject the line count statistical code into the second injection position.

[0163] The code indirect injection module is configured to, if the specific statement does not exist in the target statement block, add the specific statement in the target statement block, and repeatedly execute the steps of taking a position of the specific statement as the second injection position, and injecting the line count statistical code into the second injection position.

[0164] The performance data acquisition module specifically comprises:

[0165] The function data judgment unit is configured to acquire a function execution times and a function execution time in the first performance data, and judge whether the function execution times and the function execution time exceed preset first threshold and preset second threshold respectively.

[0166] The optimization function determination unit is configured to, if the function execution times exceed the first threshold and the function execution time exceeds the second threshold, determine that a function corresponding to the data exceeding the first threshold and the second threshold is a function to be optimized.

[0167] The statement data judgment unit is configured to acquire a target statement block execution times corresponding to the function to be optimized in the second performance data, and judge whether the target statement block execution times exceed a preset third threshold.

[0168] The optimization statement determination unit is configured to, if the target statement block execution times exceed the third threshold, determine that a statement corresponding to the data exceeding the third threshold is a statement to be optimized.

[0169] The function statement determination module specifically comprises:

[0170] The original code judgment unit is configured to, when receiving the performance detection instruction, acquire original code in the performance detection instruction, and judge whether a function to be executed exists in the original code.

[0171] The original code determination unit is configured to, if the function to be executed exists in the original code, determine that the original code is the source code to be detected, and acquire the performance statistical code.

[0172] The function statement determination unit is configured to determine the target function and the target statement block according to the performance statistical code.

[0173] The code performance detection device further comprises:

[0174] The performance weight generation module is configured to generate a performance reference value of each target function according to a preset weight, a function execution times and a function execution time of the target function, wherein the function execution times and the function execution time are the second performance data.

[0175] The function data display module is configured to display target function names and corresponding function execution times and function execution times in descending order of the performance reference values.

[0176] The statement data display module is configured to display target statement block names and corresponding target statement block execution times in descending order of the target statement block execution times, wherein the target statement block execution times are the first performance data.

[0177] The code performance detection device further comprises:

[0178] The tool function acquisition module is configured to acquire tool functions in the target code and performance statistical functions in the performance statistical code, and take the tool functions and the performance statistical functions as statistical tool functions.

[0179] The tool function writing module is configured to write the statistical tool functions into a designated file, so as to perform performance data statistics on a next code to be detected based on the statistical tool functions in the designated file.

[0180] The method performed by each program module can refer to each embodiment of the code performance detection method, and will not be described here again.

[0181] The application further provides a code performance detection device.

[0182] The code performance detection device comprises a processor, a memory, and a code performance detection program stored in the memory and executable on the processor, wherein when the code performance detection program is executed by the processor, the steps of the code performance detection method are implemented.

[0183] The method implemented when the code performance detection program is executed can refer to each embodiment of the code performance detection method, and will not be described here again.

[0184] The application further provides a computer readable storage medium.

[0185] The code performance detection program is stored in the computer readable storage medium, and when the code performance detection program is executed by a processor, the steps of the code performance detection method are implemented.

[0186] The method implemented when the code performance detection program is executed can refer to each embodiment of the code performance detection method, and will not be described here again.

[0187] It should be noted that the terms "comprising", "including", or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or system that comprises a list of elements does not include only those elements but can include other elements not expressly listed or inherent to such process, method, article, or system. Without further limitation, an element defined by the statement "comprising a" does not exclude the presence of additional identical elements in the process, method, article, or system that includes the element.

[0188] The above-mentioned embodiment numbers of the application are only for description, and do not represent the advantages and disadvantages of the embodiments.

[0189] From the above description of the embodiments, those skilled in the art can clearly understand that the above-mentioned embodiment methods can be realized by means of software and the necessary general hardware platform, of course, they can also be realized by hardware, but in many cases the former is a better embodiment. Based on such understanding, the technical solutions of the present application can be embodied in the form of a software product, which is stored in a storage medium (such as a ROM / RAM, a magnetic disk, an optical disk) as described above, and includes a number of instructions for making a terminal device (which can be a mobile phone, a computer, a server, an air conditioner, or a network device, etc.) execute the methods described in the various embodiments of the present application.

[0190] The above is only the preferred embodiment of the present application, and does not limit the patent scope of the present application, and any equivalent structure or equivalent process transformation, or direct or indirect application in other related technical fields, which is made by using the content of the specification and drawings of the present application, is also included in the patent protection scope of the present application.

Claims

1. A method of code performance detection, characterized by, The code performance detection method comprises: Upon receiving a performance detection instruction, a preset performance statistical code and a to-be-detected source code are acquired, and a regular expression in the preset performance statistical code is matched with the to-be-detected source code line by line to determine a target function in the to-be-detected source code and a target statement block in the target function; the to-be-detected source code is a JavaScript code; the preset performance statistical code comprises an execution statistical code and a line count statistical code; First position information of an execution statement of the target function and second position information of the target statement block are acquired, and the first position information and the second position information are taken as target position information; It is determined whether a specific statement exists in the target statement block; the specific statement is a statement included in " {} " of an else statement in a key statement; If the specific statement exists in the target statement block, a position where the specific statement is located is taken as a second injection position, and the line count statistical code is injected into the second injection position; If the specific statement does not exist in the target statement block, the specific statement is added in the target statement block, and the step of taking a position where the specific statement is located as the second injection position and injecting the line count statistical code into the second injection position is repeatedly executed; Based on the target position information, a first injection position and a second injection position corresponding to the execution statistical code and the line count statistical code respectively are determined, and the first injection position and the second injection position are taken as target injection positions; The preset performance statistical code is injected into the target injection positions, and a to-be-detected source code after injection is taken as a target code; The target code is executed, and first performance data of the target function and second performance data of the target statement block are respectively acquired based on the execution statistical code and the line count statistical code in the preset performance statistical code; Based on the first performance data and the second performance data, a to-be-optimized function in the target function and a to-be-optimized statement in the to-be-optimized function are determined.

2. The method of claim 1, wherein, The step of determining the target function in the to-be-detected source code and the target statement block in the target function comprises: A first regular expression related to the target function and a second regular expression corresponding to the target statement block are acquired from the preset performance statistical code, and the first regular expression and the second regular expression are matched with the to-be-detected source code line by line; When a first partial code matching the first regular expression exists in the to-be-detected source code, the first partial code is determined to be an execution statement of the target function, and the target function is determined according to the execution statement; When a second partial code matching the second regular expression exists in the to-be-detected source code, the second partial code is determined to be the target statement block.

3. The method of claim 1, wherein the code performance detection method is characterized by, The step of determining the to-be-optimized function in the target function and the to-be-optimized statement in the to-be-optimized function based on the first performance data and the second performance data comprises: acquire the function execution times and function execution times in the first performance data, and determine whether the function execution times and function execution times exceed preset first threshold and preset second threshold respectively; if the function execution times exceed the first threshold and the function execution times exceed the second threshold, determine that the function corresponding to the data exceeding the first threshold and the second threshold is a function to be optimized; acquire the target statement block execution times corresponding to the function to be optimized in the second performance data, and determine whether the target statement block execution times exceed a preset third threshold; if the target statement block execution times exceed the third threshold, determine that the statement corresponding to the data exceeding the third threshold is a statement to be optimized.

4. The method of claim 1, wherein the code performance detection method is characterized by, When the performance detection instruction is received, the steps of acquiring the source code to be detected and the preset performance statistical code, and determining the target function in the source code to be detected and the target statement block in the target function include: When the performance detection instruction is received, acquire the original code in the performance detection instruction, and determine whether there is a function to be executed in the original code; if the original code contains the function to be executed, determine that the original code is the source code to be detected, and acquire the preset performance statistical code; determine the target function and the target statement block according to the preset performance statistical code.

5. The method of claim 1, wherein the code performance detection method is characterized by, Before the steps of determining the function to be optimized in the target function and the statement to be optimized in the function to be optimized according to the first performance data and the second performance data, further include: generate a performance reference value of each target function according to a preset weight, function execution times and function execution times of the target function, wherein the function execution times and function execution times are the first performance data; display the target function name and the corresponding function execution times and function execution times in descending order of the performance reference value; display the target statement block name and the corresponding target statement block execution times in descending order of the target statement block execution times, wherein the target statement block execution times are the second performance data.

6. The method of claim 1-5, wherein, The code performance detection method further includes: acquire the tool function in the target code and the performance statistical function in the preset performance statistical code, and take the tool function and the performance statistical function as statistical tool functions; write the statistical tool functions into a file to perform performance data statistics on the next code to be detected based on the statistical tool functions in the file.

7. A code performance detection apparatus characterized by comprising: The code performance detection device includes: a function statement determination module, configured to acquire a source code to be detected and a preset performance statistical code when a performance detection instruction is received, and determine a target function in the source code to be detected and a target statement block in the target function according to a regular expression in the preset performance statistical code by matching the source code to be detected line by line; the source code to be detected is a JavaScript code; the preset performance statistical code includes execution statistical code and line count statistical code; The performance code injection module is configured to inject the preset performance statistical code into a target injection position in the to-be-detected source code based on the target function and the target position information of the target statement block in the to-be-detected source code, and to take the injected to-be-detected source code as a target code. The performance data acquisition module is configured to execute the target code, and to acquire first performance data of the target function and second performance data of the target statement block based on the execution statistical code and the line count statistical code in the preset performance statistical code, respectively. The optimization statement determination module is configured to determine a to-be-optimized function in the target function and a to-be-optimized statement in the to-be-optimized function according to the first performance data and the second performance data. The position information acquisition unit is configured to acquire first position information of an execution statement of the target function and second position information of the target statement block, and to take the first position information and the second position information as the target position information. The injection position determination unit is configured to determine a first injection position and a second injection position corresponding to the execution statistical code and the line count statistical code based on the target position information, and to take the first injection position and the second injection position as the target injection position. The specific statement judgment module is configured to judge whether a specific statement exists in the target statement block, wherein the specific statement is a statement included in " {} " of an else statement in a key statement. The code direct injection module is configured to, if the specific statement exists in the target statement block, take a position of the specific statement as the second injection position, and inject the line count statistical code into the second injection position. The code indirect injection module is configured to, if the specific statement does not exist in the target statement block, add the specific statement in the target statement block, and repeatedly execute the steps of taking a position of the specific statement as the second injection position, and injecting the line count statistical code into the second injection position.

8. The code performance detection apparatus of claim 7, wherein The code performance detection device further comprises: The regular acquisition and matching module is configured to acquire a first regular expression related to the target function and a second regular expression corresponding to the target statement block from the preset performance statistical code, and to match the first regular expression and the second regular expression with the to-be-detected source code line by line. The first regular matching module is configured to, when a first partial code matching the first regular expression exists in the to-be-detected source code, determine that the first partial code is an execution statement of the target function, and determine the target function according to the execution statement. The second regular matching module is configured to, when a second partial code matching the second regular expression exists in the to-be-detected source code, determine that the second partial code is the target statement block.

9. The code performance detection apparatus of claim 7 or 8, wherein The performance data acquisition module specifically comprises: The function data judgment unit is configured to acquire a function execution frequency and a function execution time in the first performance data, and to judge whether the function execution frequency and the function execution time exceed a preset first threshold value and a preset second threshold value, respectively. The optimization function determining unit is configured to determine the function corresponding to the data exceeding the first threshold and the second threshold as a function to be optimized if the function execution times exceed the first threshold and the function execution time exceeds the second threshold. The statement data judging unit is configured to acquire target statement block execution times corresponding to the function to be optimized in the second performance data, and judge whether the target statement block execution times exceed a preset third threshold. The optimization statement determining unit is configured to determine the statement corresponding to the data exceeding the third threshold as an optimization statement if the target statement block execution times exceed the third threshold.

10. A code performance detection device, characterized by, The code performance detection device includes a memory, a processor, and a code performance detection program stored in the memory and executable on the processor. The code performance detection program, when executed by the processor, implements the steps of the code performance detection method according to any one of claims 1 to 6.

11. A computer readable storage medium, characterized in that, The computer readable storage medium stores a code performance detection program. The code performance detection program, when executed by the processor, implements the steps of the code performance detection method according to any one of claims 1 to 6.

Citation Information

Patent Citations

  • Application program performance test method, device and system

    CN106354644A