A method, device, equipment and storage medium for detecting code coverage
By grouping and processing the code coverage information files in parallel, and using the improved Gcov4 tool to cache information, the problem of slow detection of code coverage in the prior art is solved, and efficient code coverage detection is achieved.
Patent Information
- Application Number
- CN202110001899.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-01-04
- Publication Date
- 2025-07-08
- Estimated Expiration
- 2041-01-04
AI Technical Summary
The current technology is slow to detect code coverage, which affects the process of feedback on test results and analyzing code, resulting in a degradation of overall test performance.
The code coverage information file is divided into multiple file sets, and the code coverage information is extracted from each file set in parallel. The improved Gcov4 tool caches information instead of generating intermediate files, and the extraction speed is improved through parallel processing.
It significantly improves the detection speed of code coverage, achieves 60 times acceleration, and improves test performance and user experience.
Smart Images

Figure CN114721926B_ABST
Abstract
Description
Technical Field
[0001] The embodiments of the present invention relate to the field of computer technology, and in particular, to a method, apparatus, device, and storage medium for detecting code coverage. Background Art
[0002] In the process of software development, code coverage is a means to measure the development integrity. By testing the code coverage, the uncovered code can be known. By analyzing the uncovered code, it can be inferred whether the previous code design is sufficient, and useless code in the program can also be detected, and the chaotic thinking in the code design can be inferred, so as to remind developers to pay attention to the relevant logical relationships of the code. The related technologies are slow in detecting code coverage, which affects the subsequent test result feedback and the process of analyzing the code, thus affecting the overall performance of the test. Summary of the Invention
[0003] The embodiments of the present application provide a method, apparatus, device, and storage medium for detecting code coverage, which are used to improve the speed of detecting code coverage.
[0004] On the one hand, the embodiments of the present application provide a method for detecting code coverage, and the method includes:
[0005] Compiling each source code file associated with the test object respectively to obtain a code coverage information file corresponding to each source code file;
[0006] Dividing the obtained code coverage information files into multiple file sets;
[0007] Parallelly extracting corresponding code coverage information from each code coverage information file included in the multiple file sets;
[0008] Based on the obtained code coverage information, determining the code coverage rate of the test object.
[0009] On the other hand, the embodiments of the present application provide a device for detecting code coverage, and the device includes:
[0010] A compiling module, configured to compile each source code file associated with the test object respectively to obtain a code coverage information file corresponding to each source code file;
[0011] A grouping module, configured to divide the obtained code coverage information files into multiple file sets;
[0012] An extraction module, configured to parallelly extract corresponding code coverage information from each code coverage information file included in the multiple file sets;
[0013] A test module for determining the code coverage rate of the test object based on the obtained various code coverage information.
[0014] On the one hand, an embodiment of the present application provides a computer device, including a memory, a processor, and a computer program stored on the memory and executable on the processor. When the processor executes the program, it implements the steps of the method for detecting code coverage rate described above.
[0015] On the one hand, an embodiment of the present application provides a computer-readable storage medium storing a computer program executable by a computer device. When the program runs on the computer device, the computer device is caused to execute the steps of the method for detecting code coverage rate described above.
[0016] In the embodiment of the present application, each code coverage information file is divided into multiple file sets, and then code coverage information is extracted from the code coverage information files included in each file set in parallel. By grouping and processing each code coverage information file in parallel, the speed of extracting code coverage information can be effectively improved. Further, based on the code coverage information extracted from each file set in parallel, the code coverage rate of the test object is determined, which can effectively improve the speed of detecting the code coverage rate. BRIEF DESCRIPTION OF THE DRAWINGS
[0017] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the drawings required for the description of the embodiments will be briefly introduced below. Obviously, the drawings in the following description are only some embodiments of the present invention, and those of ordinary skill in the art can obtain other drawings based on these drawings without creative efforts.
[0018] Figure 1 A schematic diagram of a system architecture applicable to the embodiment of the present application;
[0019] Figure 2 A flowchart of a method for detecting code coverage rate provided by the embodiment of the present application;
[0020] Figure 3 A flowchart of a method for merging info files provided by the embodiment of the present application;
[0021] Figure 4 A flowchart of a method for merging info files provided by the embodiment of the present application;
[0022] Figure 5 A flowchart of a method for detecting code coverage rate provided by the embodiment of the present application;
[0023] Figure 6A flowchart of a method for generating an info file provided by an embodiment of the present application;
[0024] Figure 7 A schematic diagram of an info file provided by an embodiment of the present application;
[0025] Figure 8 A schematic diagram of the structure of a device for detecting code coverage provided by an embodiment of the present application;
[0026] Figure 9 A schematic diagram of the structure of a computer device provided by an embodiment of the present application. Detailed implementation manners
[0027] In order to make the objectives, technical solutions and beneficial effects of the present invention clearer and more understandable, the present invention will be further described in detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not used to limit the present invention.
[0028] For the convenience of understanding, the terms involved in the embodiments of the present invention will be explained below.
[0029] Code coverage: The significance of code coverage lies in analyzing the uncovered code, thereby inferring whether the code design in the early stage is sufficient. It can also detect the useless code in the program, infer the chaotic thinking in the code design, and remind developers to pay attention to the relevant logical relationships of the code, so as to improve the quality of the code. Therefore, code coverage detection is exactly a good helper to help developers and testers discover problems in advance and ensure the code quality.
[0030] lcov: A program for testing code coverage.
[0031] Gcov: Gcov is a program for testing code coverage. After the program is executed, Gcda files and Gcno files are generated. Using Gcov with the GNU Compiler Collection (GCC) can make the code written more efficiently, and some basic performance statistics can be collected by optimizing the program. For example: the number of times each line of code is executed, the number of lines actually executed in each source code file, the time used for each code block execution, etc.
[0032] Gcno file: Contains the mapping relationship between code counters and source code.
[0033] Gcda file: Records the specific execution times of each piece of code.
[0034] Info File: The Info file contains code coverage information corresponding to one or more source code files. Each source file corresponds to one record, including the source code file name with full path, covered lines, execution times, etc.
[0035] The design concept of the embodiments of the present application will be introduced below.
[0036] In the related art, when detecting code coverage, for more than 500 coverage files, it takes about five minutes of computing time. This computing speed affects the subsequent processes such as coverage result merging, display, and feedback, which is not conducive to the progress of coverage testing, affects the overall test performance, and also provides a poor user experience for testers.
[0037] In view of this, the embodiments of the present application provide a method for detecting code coverage. The method specifically includes: separately compiling each source code file associated with the test object to obtain a code coverage information file corresponding to each source code file. Then, divide the obtained code coverage information files into multiple file sets, and parallelly extract the corresponding code coverage information from each code coverage information file included in the multiple file sets. After that, based on the obtained code coverage information, determine the code coverage rate of the test object.
[0038] In the embodiments of the present application, the code coverage information files are divided into multiple file sets, and then the code coverage information is parallelly extracted from the code coverage information files included in each file set. By grouping and parallelly processing each code coverage information file, the speed of extracting code coverage information can be effectively improved. Further, based on the code coverage information parallelly extracted from each file set, determining the code coverage rate of the test object can effectively improve the speed of detecting the code coverage rate.
[0039] Please refer to Figure 1 , which is a schematic structural diagram of a system architecture applicable to the embodiments of the present application. The system architecture at least includes a terminal device 101 and a server 102.
[0040] The terminal device 101 pre-installs a test application, which can be used to test the code coverage of different applications. For example, instant messaging applications, shopping applications, video applications, etc. The test application can also be used to test the code coverage of the same application in different scenarios. For example, the code coverage of the background of the instant messaging application, the code coverage of the front end of the instant messaging application, etc. The test application can be a pre-installed client application, a web version application, a mini program, etc. The terminal device 101 may include one or more processors 1011, a memory 1012, an I / O interface 1013 for interacting with the server 102, a display panel 1014, etc. The terminal device 101 can be a smart phone, a tablet computer, a laptop computer, a desktop computer, etc., but is not limited thereto.
[0041] The server 102 is the background server corresponding to the test application and provides services for the test application. The server 102 may include one or more processors 1021, a memory 1022, and an I / O interface 1023 for interacting with the terminal device 101, etc. In addition, the server 102 may also be configured with a database 1024. The server 102 can be an independent physical server, or a server cluster or distributed system composed of multiple physical servers. It can also be a cloud server providing basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communications, middleware services, domain name services, security services, Content Delivery Network (CDN), and big data and artificial intelligence platforms. The terminal device 101 and the server 102 can be directly or indirectly connected through wired or wireless communication methods, and this application does not limit this.
[0042] The method for detecting code coverage can be executed by the terminal device 101, or can be executed by the interaction between the terminal device 101 and the server 102.
[0043] In the first case, the method for detecting code coverage is executed by the terminal device 101.
[0044] The terminal device 101 runs the test application, obtains each source code file associated with the test object uploaded by the tester, and then compiles each source code file associated with the test object separately to obtain a code coverage information file corresponding to each source code file. Then, the obtained code coverage information files are divided into multiple file sets, and the corresponding code coverage information is extracted in parallel from each code coverage information file included in the multiple file sets. After that, based on the obtained code coverage information, the code coverage of the test object is determined. The terminal device 101 displays the code coverage of the test object in the test result interface of the test application.
[0045] In the second case, the method for detecting code coverage is executed through the interaction between the terminal device 101 and the server 102.
[0046] The terminal device 101 runs a test application, obtains each source code file associated with the test object uploaded by the tester, and sends each source code file to the server 102. The server 102 compiles each source code file associated with the test object respectively to obtain a code coverage information file corresponding to each source code file. Then, the obtained code coverage information files are divided into multiple file sets, and corresponding code coverage information is extracted in parallel from each code coverage information file included in the multiple file sets. After that, based on the obtained code coverage information, the code coverage rate of the test object is determined. The server 102 sends the code coverage rate of the test object to the terminal device 101, and the terminal device 101 displays the code coverage rate of the test object in the test result interface of the test application.
[0047] Based on Figure 1 the system architecture diagram shown, an embodiment of the present application provides a process of a method for detecting code coverage, as Figure 2 shown. The process of this method can be executed by a computer device, and the computer device can be Figure 1 the terminal device 101 or the server 102 shown, including the following steps:
[0048] Step S201: Compile each source code file associated with the test object respectively to obtain a code coverage information file corresponding to each source code file.
[0049] Specifically, the test object can be an application, such as an instant messaging application, a shopping application, a video application, etc. The test object can also be a part of the functions of the application under test, such as the group chat function in an instant messaging application, the payment function in a shopping application, the caching function in a video application, etc. The test object can also be different clients of the application under test, such as the mobile client of an instant messaging application, the PC client of an instant messaging application, etc. The test object can also be the background server corresponding to the application under test, such as the background server of an instant messaging application, etc. It should be noted that the test object in the embodiment of the present application is not limited to the several types exemplified above, and can also be other types, and the present application does not make specific limitations thereto.
[0050] The code coverage information file includes at least two types of files, namely the Gcno file and the Gcda file. Among them, the Gcno file includes the mapping relationship between the code counter and the source code, the Gcda file records the specific execution times of each piece of code, and there is a one-to-one correspondence between the Gcno file and the Gcda file. After compiling a source code file, the corresponding Gcno file and Gcda file of the source code file can be obtained.
[0051] Step S202: Divide each obtained code coverage information file into multiple file sets.
[0052] Specifically, each code coverage information file can be evenly divided into multiple file sets, that is, the number of code coverage information files in each file set is the same. Or each code coverage information file can be unevenly divided into multiple file sets, that is, the number of code coverage information files in each file set is not exactly the same.
[0053] Exemplarily, assume the total number of Gcno files and Gcda files is 400. The code coverage information files are evenly divided into 10 file sets, and each file set includes 40 code coverage information files. Among these 40 code coverage information files, there are 20 Gcno files and 20 Gcda files.
[0054] Exemplarily, assume the total number of Gcno files and Gcda files is 100. The code coverage information files are divided into 2 file sets. One file set includes 40 code coverage information files, namely 20 Gcno files and 20 Gcda files. The other file set includes 60 code coverage information files, namely 30 Gcno files and 30 Gcda files.
[0055] Step S203: Extract the corresponding code coverage information from each code coverage information file included in multiple file sets in parallel.
[0056] Specifically, the Gcov tool is extracted from the lcov source code and corresponding modifications are made to it. The process of lcov extracting code coverage information to generate intermediate files is removed. The modified Gcov tool is denoted as Gcov4. After Gcov4 extracts the code coverage information, it caches the code coverage information in memory instead of generating intermediate files. Therefore, subsequently, the code coverage rate can be directly calculated based on the cached code coverage information, without the need to frequently call interfaces to access intermediate files to obtain the code coverage information, thereby improving the speed of calculating the code coverage rate.
[0057] For any source code file, input the Gcno file and Gcda file corresponding to the source code file into Gcov4. Gcov4 reads the code coverage information from the Gcno file and Gcda file and caches it. The code coverage information includes function coverage information, line coverage information, etc.
[0058] Step S204: Determine the code coverage rate of the test object based on the obtained code coverage information.
[0059] Specifically, the code coverage rate can be line code coverage rate, function coverage rate, decision coverage rate, etc. When the code coverage rate to be calculated is different, the code coverage information used is also different. For example, if the line code coverage rate of the test object is to be calculated, the code coverage information used is line coverage information; if the function coverage rate of the test object is to be calculated, the code coverage information used is function coverage information.
[0060] In the embodiments of the present application, each code coverage information file is divided into multiple file sets, and then the code coverage information is extracted from the code coverage information files included in each file set in parallel. By grouping and processing each code coverage information file in parallel, the speed of extracting the code coverage information can be effectively improved. Further, based on the code coverage information extracted from each file set in parallel, the code coverage rate of the test object is determined, which can effectively improve the speed of detecting the code coverage rate.
[0061] Optionally, in the above step S202, each obtained code coverage information file can be divided into multiple file sets according to the type of the source code file corresponding to the code coverage information file.
[0062] The types of the source code files include the function type of the source code file, the format type of the source code file, etc.
[0063] Exemplarily, it is assumed that there are 100 source code files associated with the test object, among which 50 source code files are used to implement function A of the test object, 20 source code files are used to implement function B of the test object, and 30 source code files are used to implement function C of the test object. After compiling each source code file associated with the test object, 200 code information coverage files are obtained, which are divided into 100 Gcno files and 100 Gcda files. Since each source code file associated with the test object is used to implement 3 functions of the test object, the obtained code coverage information files can be divided into 3 file sets, namely the first file set, the second file set, and the third file set.
[0064] The first file set includes 100 code coverage information files, namely 50 Gcno files and 50 Gcda files, and the source code files corresponding to the 100 code coverage information files are used to implement function A.
[0065] The second file set includes 40 code coverage information files, namely 20 Gcno files and 20 Gcda files, and the source code files corresponding to the 40 code coverage information files are used to implement function B.
[0066] The third set of files includes 60 code coverage information files, namely 30 Gcno files and 30 Gcda files. The source code files corresponding to the 60 code coverage information files are used to implement function C.
[0067] When dividing the set of files according to the types of the source code files corresponding to the code coverage information files, the code coverage rates of different types of source code files can be obtained, and at the same time, all the code coverage information can be merged to obtain the overall code coverage rate of the test object, improving the applicable scope of code coverage rate detection.
[0068] It should be noted that in the embodiments of the present application, the division of the set of files is not limited to the above-mentioned one implementation manner, and other implementation manners are also possible. For example, according to the numbers of the obtained code coverage information files, the obtained code coverage information files can be divided into multiple sets of files, or the obtained code coverage information files can be randomly divided into multiple sets of files, etc. The present application does not make specific limitations on this.
[0069] Optionally, in the above step S203, for each set of files, the following operations are performed in parallel:
[0070] For a set of files, by traversing the execution links of each code coverage information file included in a set of files, basic block information and arc information are obtained from each code coverage information file, and then the code coverage information is extracted from the obtained basic block information and arc information.
[0071] In specific implementation, the code coverage information in the Gcno file and the Gcda file is represented by basic blocks (block_info) and arcs (arc_info). Therefore, when extracting the code coverage information, by traversing the execution links in the Gcno file and the Gcda file, the predecessors (pred) and successors (succ) of the basic blocks and the predecessors (pred) and successors (succ) of the arcs are judged, and the basic block information and arc information are obtained from the Gcno file and the Gcda file. Then, the code coverage rate information in dimensions such as function coverage information and line coverage information is extracted from the basic block information and arc information, and is expressed in terms of function information (function_info) and line number information (line_info).
[0072] Further, for the type of the calculated code coverage rate, the extracted code coverage information can be filtered to retain the required code coverage information. For example, if the detected code coverage rate is the line code coverage rate, the line coverage information is filtered out from the code coverage rate information in dimensions such as function coverage information and line coverage information extracted. If the detected code coverage rate is the function coverage rate, the function coverage information is filtered out from the code coverage rate information in dimensions such as function coverage information and line coverage information extracted.
[0073] After extracting the code coverage information of all dimensions from the code coverage information file and eliminating the useless code coverage information, when calculating the code coverage rate based on the code coverage information subsequently, the reading and writing of files can be reduced, thereby improving the speed of detecting the code coverage rate.
[0074] Optionally, in the above step S204, the obtained code coverage information of each item is merged to obtain the total code coverage information corresponding to the test object, and then the code coverage rate of the test object is determined according to the total code coverage information corresponding to the test object.
[0075] Specifically, when merging the code coverage information of each item to obtain the total code coverage information corresponding to the test object, the embodiments of the present application provide at least the following two implementation manners:
[0076] Implementation manner 1: For a file set, the code coverage information obtained from each code coverage information file in a file set is merged to obtain the set code coverage information corresponding to a file set. Then, the set code coverage information corresponding to each file set is merged to obtain the total code coverage information corresponding to the test object.
[0077] In specific implementation, based on the code coverage information obtained from the code coverage information file, a code coverage rate file is generated. The code coverage rate file includes the source code file name, the line number of the code in the source code file, the execution times, etc. The code coverage rate file can be a readable Info file.
[0078] The code coverage rate files corresponding to each code coverage information file in a file set are merged to obtain the set code coverage rate file corresponding to a file set. Each file set can parallelly merge the code coverage rate files corresponding to each code coverage information file in the set. The set code coverage rate files corresponding to each file set are merged to obtain the total code coverage rate file corresponding to the test object.
[0079] Exemplarily, as Figure 3 shown, it is set that the obtained code coverage information files are divided into two file sets, namely the first file set and the second file set. Among them, the first file set includes 20 code coverage information files, which are 10 Gcno files and 10 Gcda files corresponding to the 10 Gcno files respectively. The second file set includes 20 code coverage information files, which are 10 Gcno files and 10 Gcda files corresponding to the 10 Gcno files respectively.
[0080] Extract code coverage information from the code coverage information files in two sets of files in parallel. For any set of Gcno files and Gcda files, use Gcov4 to extract code coverage information from the Gcno files and Gcda files, and then generate an Info file based on the extracted code coverage information.
[0081] 10 Info files were generated in the first set of files, namely Info File 1, Info File 2, Info File 3, Info File 4, Info File 5, Info File 6, Info File 7, Info File 8, Info File 9, and Info File 10. 10 Info files were generated in the second set of files, namely Info File 11, Info File 12, Info File 13, Info File 14, Info File 15, Info File 16, Info File 17, Info File 18, Info File 19, and Info File 20.
[0082] The first set of files and the second set of files merge the Info files within the sets in parallel. Specifically, the 10 Info files in the first set of files are merged to obtain the first set of Info files corresponding to the first set of files. The 10 code coverage rate files in the second set of files are merged to obtain the second set of Info files corresponding to the second set of files. Then the first set of Info files and the second set of Info files are merged to obtain the total Info file.
[0083] Using the methods of grouped merging and concurrent merging to obtain the total code coverage rate file corresponding to the test object can effectively improve the speed of merging code coverage information, thereby improving the speed of detecting code coverage rate.
[0084] Embodiment 2: Merge the code coverage information obtained from the code coverage information files in each set of files to obtain the total code coverage information corresponding to the test object.
[0085] In specific implementation, based on the code coverage information obtained from the code coverage information file, a code coverage rate file is generated. The code coverage rate file includes the source code file name, the line number of the code in the source code file, the number of executions, etc. The code coverage rate file can be an Info file. Then all the code coverage rate files are merged to obtain the total code coverage information corresponding to the test object.
[0086] Exemplarily, such as Figure 4As shown, each obtained code coverage information file is divided into two file sets, namely the first file set and the second file set. Among them, the first file set includes 20 code coverage information files, namely 10 Gcno files and 10 Gcda files corresponding to the 10 Gcno files respectively. The second file set includes 20 code coverage information files, namely 10 Gcno files and 10 Gcda files corresponding to the 10 Gcno files respectively.
[0087] Extract code coverage information from the code coverage information files in the two file sets in parallel. For any set of Gcno files and Gcda files, use Gcov4 to extract code coverage information from the Gcno files and Gcda files, and then generate an Info file based on the extracted code coverage information.
[0088] 10 Info files are generated in the first file set, namely Info File 1, Info File 2, Info File 3, Info File 4, Info File 5, Info File 6, Info File 7, Info File 8, Info File 9, Info File 10. 10 Info files are generated in the second file set, namely Info File 11, Info File 12, Info File 13, Info File 14, Info File 15, Info File 16, Info File 17, Info File 18, Info File 19, Info File 20. Merge Info File 1 to Info File 20 to obtain the total Info file.
[0089] Furthermore, when determining the code coverage rate of the test object according to the total code coverage information corresponding to the test object, the embodiments of the present application provide at least the following two implementation manners:
[0090] Implementation Manner 1: The total code coverage information corresponding to the test object includes the total number of compiled lines of code and the total number of executed lines of code in each source code file associated with the test object. The ratio of the total number of executed lines of code in each source code file associated with the test object to the total number of compiled lines of code in each source code file associated with the test object is used as the code coverage rate of the test object.
[0091] In specific implementation, the total number of compiled lines of code in each source code file associated with the test object is the sum of the compiled lines of code in each source code file, and the total number of compiled lines of code in the source code file can be the total number of lines of code in the source code file. The total number of executed lines of code in each source code file associated with the test object is the sum of the executed lines of code in each source code file.
[0092] Exemplarily, it is assumed that the test object is associated with 3 source code files, namely source code file 1, source code file 2, and source code file 3. Among them, the number of compiled code lines in source code file 1 is m1 lines, the number of compiled code lines in source code file 2 is m2 lines, and the number of compiled code lines in source code file 3 is m3 lines. Then, the total number of compiled code lines in each source code file associated with the test object is m1 + m2 + m3.
[0093] The number of executed code lines in source code file 1 is n1 lines, the number of executed code lines in source code file 2 is n2 lines, and the number of executed code lines in source code file 3 is n3 lines. Then, the total number of executed code lines in each source code file associated with the test object is n1 + n2 + n3.
[0094] Calculate the ratio of the total number of executed code lines in each source code file associated with the test object to the total number of compiled code lines in each source code file associated with the test object, and obtain the code coverage rate of the test object as (n1 + n2 + n3) / (m1 + m2 + m3).
[0095] Embodiment 2: The total code coverage information corresponding to the test object includes the total number of compiled functions and the total number of executed functions in each source code file associated with the test object. The ratio of the total number of executed functions in each source code file associated with the test object to the total number of compiled functions in each source code file associated with the test object is used as the code coverage rate of the test object.
[0096] In specific implementation, the total number of compiled functions in each source code file associated with the test object is the sum of the number of compiled functions in each source code file. The total number of compiled functions in the source code file can be the total number of functions in the source code file. The total number of executed functions in each source code file associated with the test object is the sum of the number of executed functions in each source code file.
[0097] Exemplarily, it is assumed that the test object is associated with 3 source code files, namely source code file 1, source code file 2, and source code file 3. Among them, the number of compiled functions in source code file 1 is p1 lines, the number of compiled functions in source code file 2 is p2 lines, and the number of compiled functions in source code file 3 is p3 lines. Then, the total number of compiled functions in each source code file associated with the test object is p1 + p2 + p3.
[0098] The number of executed functions in source code file 1 is q1 lines, the number of executed functions in source code file 2 is q2 lines, and the number of executed functions in source code file 3 is q3 lines. Then, the total number of executed functions in each source code file associated with the test object is q1 + q2 + q3.
[0099] Calculate the ratio of the total number of functions executed in each source code file associated with the test object to the total number of functions compiled in each source code file associated with the test object, and obtain the code coverage rate of the test object as (q1 + q2 + q3) / (p1 + p2 + p3).
[0100] In the embodiments of the present application, each code coverage information file is divided into multiple file sets, and then the code coverage information is extracted from the code coverage information files included in each file set in parallel. By grouping and processing each code coverage information file in parallel, the speed of extracting the code coverage information can be effectively improved. Further, based on the code coverage information extracted from each file set in parallel, the code coverage rate of the test object is determined, which can effectively improve the speed of detecting the code coverage rate.
[0101] To better explain the embodiments of the present application, the following describes the process of a method for detecting code coverage rate provided by the embodiments of the present application in combination with a specific implementation scenario. This method is executed by a computer device, such as Figure 5 shown, and includes the following steps:
[0102] Compile each source code file associated with the application under test respectively to obtain the code coverage information file corresponding to each source code file. Among them, each source code file corresponds to a group of code coverage information files, and a group of code coverage information files includes a Gcno file and a Gcda file, and the Gcno file corresponds to the Gcda file. Then send each group of code coverage information files to the processing queue, and the process pool distributes each group of code coverage information to the concurrent Gcov4 for processing to obtain the Info file corresponding to each group of code coverage information files, and then merge the obtained Info files to output the total Info file. The total Info file includes the total number of compiled code lines and the total number of executed code lines in each source code file associated with the application under test. The ratio of the total number of functions executed in each source code file associated with the application under test to the total number of functions compiled in each source code file associated with the application under test is used as the code coverage rate of the application under test. In specific implementation, the size of the process pool is set according to the conditions of the computer device and the number of code coverage information files, so as to achieve the best concurrent effect.
[0103] The above process of Gcov4 processing each group of Gcno files and Gcda files includes the following steps, such as Figure 6 shown:
[0104] Pre-configure the file input path, file output path, and output file format in Gcov4 based on the incoming parameters. Among them, the format parameter -i is used to identify that the output file format is the Info format. Use Gcov4 to extract code coverage information in dimensions such as function coverage information and line coverage information from the basic block information and arc information in the Gcno file and Gcda file, and record the code coverage information in the data structure of Gcov4. Filter out the line coverage information from the code coverage information, then generate an Info file based on the filtered line coverage information, and then release the space of the data structure that records the code coverage information. The Info file is a file readable by testers. The Info file includes the source code file name with the full path, the absolute path of the source code file, the covered lines, the execution times, etc. Exemplarily, as Figure 7 shown, the Info file includes the absolute path of the source code file, the covered lines of the source code file, and the execution times. Among them, the absolute path of the source code file is the string after identifying SF, and the covered lines and execution times of the source code file are two numbers after identifying DA. end_of_record in the Info file indicates that the description information of the source code file ends here.
[0105] The process of merging the obtained Info files and outputting the total Info file includes the following steps:
[0106] Merge the individual Info files obtained in the same process to obtain a set of Info files. Then merge the set of Info files obtained in each process to obtain the total Info file of the application under test.
[0107] In the embodiments of this application, the individual code coverage information files are divided into multiple file sets, and then the code coverage information is extracted in parallel from the code coverage information files included in each file set. By processing the code coverage information files in groups in parallel, the speed of extracting the code coverage information can be effectively improved. Further, based on the code coverage information extracted in parallel from each file set, the code coverage rate of the test object is determined, which can effectively improve the speed of detecting the code coverage rate.
[0108] To verify the performance of the method for detecting the code coverage rate in the embodiments of this application, the inventors of this application tested the speed of detecting the code coverage rate using the method in this application, and compared the obtained detection speed with the current speed of detecting the code coverage rate using lcov. The comparison results are shown in Table 1:
[0109] Table 1.
[0110] Total number of Gcno files and Gcda files lcov Gcov4 Concurrent Gcov4 Acceleration effect 200 110s 3.53s 2.04s 53.9 240 127s 3.92s 2.09s 60.7
[0111] As can be seen from Table 1, compared with the current method of using lcov to detect code coverage, the method in this application can achieve an acceleration effect of 60 times when detecting code coverage, greatly improving the speed of detecting code coverage.
[0112] Based on the same technical concept, an embodiment of this application provides a device for detecting code coverage, as Figure 8 shown. The device 800 includes:
[0113] A compilation module 801, configured to separately compile each source code file associated with a test object to obtain a code coverage information file corresponding to each source code file;
[0114] A grouping module 802, configured to divide the obtained code coverage information files into multiple file sets;
[0115] An extraction module 803, configured to extract corresponding code coverage information from each code coverage information file included in the multiple file sets in parallel;
[0116] A test module 804, configured to determine the code coverage rate of the test object based on the obtained code coverage information.
[0117] Optionally, the extraction module 803 is specifically configured to:
[0118] For each file set, perform the following operations in parallel:
[0119] For a file set, by traversing the execution links of each code coverage information file included in the file set, obtain basic block information and arc information from each code coverage information file;
[0120] Extract code coverage information from the obtained basic block information and arc information.
[0121] Optionally, the test module 804 is specifically configured to:
[0122] Merge the obtained code coverage information to obtain the total code coverage information corresponding to the test object;
[0123] Determine the code coverage rate of the test object according to the total code coverage information corresponding to the test object.
[0124] Optionally, the test module 804 is specifically configured to:
[0125] For a file set, merge the code coverage information obtained from each code coverage information file in the file set to obtain the set code coverage information corresponding to the file set;
[0126] Merge the set code coverage information corresponding to each file set to obtain the total code coverage information corresponding to the test object.
[0127] Optionally, the total code coverage information corresponding to the test object includes the total number of compiled code lines and the total number of executed code lines in each source code file associated with the test object;
[0128] The test module 804 specifically uses:
[0129] Take the ratio of the total number of executed code lines in each source code file associated with the test object to the total number of compiled code lines in each source code file associated with the test object as the code coverage rate of the test object.
[0130] Optionally, the grouping module 802 specifically is used for:
[0131] Divide each obtained code coverage information file into multiple file sets according to the type of the source code file corresponding to the code coverage information file.
[0132] In the embodiments of the present application, each code coverage information file is divided into multiple file sets, and then code coverage information is extracted in parallel from the code coverage information files included in each file set. By grouping and processing each code coverage information file in parallel, the speed of extracting code coverage information can be effectively improved. Further, based on the code coverage information extracted in parallel from each file set, the code coverage rate of the test object is determined, which can effectively improve the speed of detecting the code coverage rate.
[0133] Based on the same technical concept, the embodiments of the present application provide a computer device, as Figure 9 shown, including at least one processor 901 and a memory 902 connected to at least one processor. In the embodiments of the present application, the specific connection medium between the processor 901 and the memory 902 is not limited, Figure 9 taking the example that the processor 901 and the memory 902 are connected through a bus. The bus can be divided into an address bus, a data bus, a control bus, etc.
[0134] In the embodiments of the present application, the memory 902 stores instructions executable by at least one processor 901, and at least one processor 901 can execute the steps of the method for detecting the code coverage rate by executing the instructions stored in the memory 902.
[0135] Among them, the processor 901 is the control center of the computer device. It can connect various parts of the computer device through various interfaces and circuits. By running or executing the instructions stored in the memory 902 and calling the data stored in the memory 902, the code coverage can be detected. Optionally, the processor 901 may include one or more processing units. The processor 901 may integrate an application processor and a modem processor. Among them, the application processor mainly processes the operating system, user interface, application programs, etc., and the modem processor mainly processes wireless communication. It can be understood that the above-mentioned modem processor may not be integrated into the processor 901. In some embodiments, the processor 901 and the memory 902 may be implemented on the same chip. In some embodiments, they may also be separately implemented on independent chips.
[0136] The processor 901 may be a general-purpose processor, such as a central processing unit (CPU), a digital signal processor, an application specific integrated circuit (ASIC), a field programmable gate array, or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, and can implement or execute the various methods, steps, and logic block diagrams disclosed in the embodiments of the present application. The general-purpose processor may be a microprocessor or any conventional processor, etc. The steps of the method disclosed in combination with the embodiments of the present application may be directly embodied as being executed by a hardware processor, or executed by a combination of hardware and software modules in the processor.
[0137] The memory 902 serves as a non-volatile computer-readable storage medium and can be used to store non-volatile software programs, non-volatile computer-executable programs, and modules. The memory 902 may include at least one type of storage medium. For example, it may include flash memory, hard disks, multimedia cards, card-type memories, random access memory (RAM), static random access memory (SRAM), programmable read-only memory (PROM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), magnetic memories, magnetic disks, optical disks, and so on. The memory 902 is any other medium that can be used to carry or store the desired program code in the form of instructions or data structures and can be accessed by a computer, but is not limited to this. The memory 902 in the embodiments of the present application may also be a circuit or any other device capable of implementing a storage function, for storing program instructions and / or data.
[0138] Based on the same inventive concept, embodiments of the present application provide a computer-readable storage medium storing a computer program executable by a computer device. When the program runs on the computer device, it causes the computer device to execute the steps of the above method for detecting code coverage.
[0139] Those skilled in the art should understand that the embodiments of the present invention may be provided as a method, or a computer program product. Therefore, the present invention may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware aspects. Moreover, the present invention may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk memories, CD-ROMs, optical memories, etc.) containing computer-usable program code.
[0140] The present invention is described with reference to the flowcharts and / or block diagrams of methods, apparatuses (systems), and computer program products according to embodiments of the present invention. It should be understood that each flow and / or block in the flowcharts and / or block diagrams, and the combination of flows and / or blocks in the flowcharts and / or block diagrams, can be realized by computer program instructions. These computer program instructions can be provided to the processors of general-purpose computers, special-purpose computers, embedded processors, or other programmable data processing devices to generate a machine, so that the instructions executed by the processors of the computer or other programmable data processing devices generate for realizing the flow Figure 1one process or multiple processes and / or blocks Figure 1 means for the functions specified in one block or multiple blocks.
[0141] These computer program instructions can also be stored in a computer-readable memory that can direct a computer or other programmable data processing device to work in a specific manner, such that the instructions stored in the computer-readable memory produce a manufactured article including an instruction means that implements the functions in the process Figure 1 one process or multiple processes and / or blocks Figure 1 specified in one block or multiple blocks.
[0142] These computer program instructions can also be loaded onto a computer or other programmable data processing device, such that a series of operation steps are performed on the computer or other programmable device to produce a computer-implemented process. Thus, the instructions executed on the computer or other programmable device provide steps for implementing the functions specified in the process Figure 1 one process or multiple processes and / or blocks Figure 1 specified in one block or multiple blocks.
[0143] Although the preferred embodiments of the present invention have been described, those skilled in the art can make additional changes and modifications once they learn the basic creative concept. Therefore, the appended claims are intended to be construed as including the preferred embodiments and all changes and modifications falling within the scope of the present invention.
[0144] Obviously, those skilled in the art can make various changes and variations to the present invention without departing from the spirit and scope of the present invention. Thus, if these modifications and variations of the present invention fall within the scope of the claims of the present invention and their equivalent technologies, the present invention also intends to include these modifications and variations.
Claims
1. A method for detecting code coverage, characterized by including: compiling each source code file associated with the test object respectively to obtain a code coverage information file corresponding to each source code file; dividing the obtained code coverage information files into multiple file sets; extracting corresponding code coverage information from each code coverage information file included in the multiple file sets in parallel using concurrent Gcov4 and caching the code coverage information in memory; the Gcov4 is obtained in the following manner: extracting the Gcov tool from the lcov source code and making corresponding modifications to the Gcov tool to remove the process of the lcov extracting code coverage information to generate intermediate files, thereby obtaining the Gcov4; determining the code coverage rate of the test object based on the cached code coverage information of each; 2. The method according to claim 1, characterized in that, the parallel extracting of corresponding code coverage information from each code coverage information file included in the multiple file sets includes: performing the following operations in parallel for each file set respectively: for a file set, by traversing the execution links of each code coverage information file included in the file set respectively, obtaining basic block information and arc information from each code coverage information file; extracting code coverage information from the obtained basic block information and arc information; 3. The method according to any one of claims 1 to 2, characterized in that, the determining of the code coverage rate of the test object based on the cached code coverage information of each includes: merging the cached code coverage information of each to obtain the total code coverage information corresponding to the test object; determining the code coverage rate of the test object according to the total code coverage information corresponding to the test object; 4. The method according to claim 3, wherein the merging of the cached code coverage information of each to obtain the total code coverage information corresponding to the test object includes: for a file set, merging the code coverage information obtained from each code coverage information file in the file set to obtain the set code coverage information corresponding to the file set; merging the set code coverage information corresponding to each file set to obtain the total code coverage information corresponding to the test object; 5. The method according to claim 3, wherein the total code coverage information corresponding to the test object includes the total number of compiled code lines and the total number of executed code lines in each source code file associated with the test object; the determining of the code coverage rate of the test object according to the total code coverage information corresponding to the test object includes: taking the ratio of the total number of executed code lines in each source code file associated with the test object to the total number of compiled code lines in each source code file associated with the test object as the code coverage rate of the test object; 6. The method according to claim 3, wherein the dividing of the obtained code coverage information files into multiple file sets includes: dividing the obtained code coverage information files into multiple file sets according to the type of the source code file corresponding to the code coverage information file; 7. A device for detecting code coverage, characterized in that including: a compiling module for compiling each source code file associated with the test object respectively to obtain a code coverage information file corresponding to each source code file; a grouping module for dividing the obtained code coverage information files into multiple file sets; An extraction module, configured to extract corresponding code coverage information from each code coverage information file included in the multiple file sets in parallel using concurrent Gcov4, and cache the code coverage information in memory; the Gcov4 is obtained in the following manner: extracting the Gcov tool from the lcov source code and making corresponding modifications to the Gcov tool to remove the process of the lcov extracting code coverage information to generate intermediate files, thereby obtaining the Gcov4; A test module, configured to determine the code coverage rate of the test object based on the cached code coverage information of each item.
8. The device according to claim 7, characterized in that, Specifically, the extraction module is configured to: For each file set, perform the following operations in parallel: For a file set, by traversing the execution links of each code coverage information file included in the file set respectively, obtain basic block information and arc information from each code coverage information file; Extract code coverage information from the obtained basic block information and arc information.
9. The device according to any one of claims 7 to 8, characterized in that, Specifically, the test module is configured to: Merge the cached code coverage information of each item to obtain the total code coverage information corresponding to the test object; Determine the code coverage rate of the test object according to the total code coverage information corresponding to the test object.
10. The device according to claim 9, characterized in that, Specifically, the test module is configured to: For a file set, merge the code coverage information obtained from each code coverage information file in the file set to obtain the set code coverage information corresponding to the file set; Merge the set code coverage information corresponding to each file set to obtain the total code coverage information corresponding to the test object.
11. The device according to claim 9, characterized in that, The total code coverage information corresponding to the test object includes the total number of compiled code lines and the total number of executed code lines in each source code file associated with the test object; Specifically, the test module is configured to: Use the ratio of the total number of executed code lines in each source code file associated with the test object to the total number of compiled code lines in each source code file associated with the test object as the code coverage rate of the test object.
12. The device according to claim 9, characterized in that, Specifically, the grouping module is configured to: Divide the obtained code coverage information files into multiple file sets according to the types of the source code files corresponding to the code coverage information files.
13. A computer device, comprising a memory, a processor, and a computer program stored on the memory and executable on the processor, characterized in that, When the processor executes the program, the steps of the method according to any one of claims 1 to 6 are implemented.
14. A computer-readable storage medium, characterized in that, It stores a computer program executable by a computer device. When the program runs on the computer device, the computer device is caused to execute the steps of the method according to any one of claims 1 to 6.
Citation Information
Patent Citations
Method and equipment for obtaining code coverage rate
CN103365771A
Distributed code coverage rate report generation system and method
CN111078578A