Skip to content

evosuite 代码研读

源码分析

master/EvoSuite/main
master/EvoSuite/parseCommandLine
master/executionmode.TestGeneration/executeTestGeneration
master/executionmode.TestGeneration/generateTests // Create MasterNode and ClientNodes, start to search
master/statistics.SearchStatistics/getInstance // evolutionary search
--> results
    addTestGenerationResult


writeStatistics // Write result to disk using selected backend

涉及到了 master-work 设计模式,先看这个设计模式。

master 中有 MasterNodeImpl 类,实现了 client 中的 MasterNodeRemote (RMI reference used to communicate with the master node.) 接口。然后 client 中的 ClientNodeImpl 类通过 java.rmi 的方式使用到了 MasterNodeImpl 类中的方法。

master 中有 SearchStatistics(A singleton of SearchStatistics collects all the data values reported by a single client node.)MasterNodeImpl 类调用了 SearchStatistics 类中的方法。

// client -> org.evosuite.rmi.service.ClientNodeImpl -> init()
private MasterNodeRemote masterNode;
masterNode = (MasterNodeRemote) registry.lookup(MasterNodeRemote.RMI_SERVICE_NAME); // 使用了 java.rmi.registry
registry 在 master 中创建并指定端口号,client 获取端口号后得到 registry 的引用。master 在 MasterServices 中创建 MasterNode 并注册,client 在 ClientServices 中创建 ClientNode 并注册。端口保存在 client 的 Properties 中,运行时动态赋值。

MasterNode: 
(TestGeneration) handle.openServer() -> MasterSevices.getInstance().registerServices()
ClientNode:
(TestGeneration) handler.startProcessGroup() -> (ExternalProcessGroupHandler) startProcessGroup() -> (ExternalProcessGroupHandler) startProcess() ClientProcess.main() -> process.run() -> ClientServices.getInstance().registerServices()

执行结果

"C:\Program Files\Java\jdk1.8.0_181\bin\java.exe" -Dmaven.multiModuleProjectDirectory=D:\IdeaProjects\EvosuiteTest "-Dmaven.home=D:\JetBrains\IntelliJ IDEA 2020.2.1\plugins\maven\lib\maven3" "-Dclassworlds.conf=D:\JetBrains\IntelliJ IDEA 2020.2.1\plugins\maven\lib\maven3\bin\m2.conf" "-Dmaven.ext.class.path=D:\JetBrains\IntelliJ IDEA 2020.2.1\plugins\maven\lib\maven-event-listener.jar" "-javaagent:D:\JetBrains\IntelliJ IDEA 2020.2.1\lib\idea_rt.jar=9331:D:\JetBrains\IntelliJ IDEA 2020.2.1\bin" -Dfile.encoding=UTF-8 -classpath "D:\JetBrains\IntelliJ IDEA 2020.2.1\plugins\maven\lib\maven3\boot\plexus-classworlds-2.6.0.jar;D:\JetBrains\IntelliJ IDEA 2020.2.1\plugins\maven\lib\maven3\boot\plexus-classworlds.license" org.codehaus.classworlds.Launcher -Didea.version=2020.2.1 evosuite:generate
[INFO] Scanning for projects...
[INFO] 
[INFO] ----------------------< org.example:EvosuiteTest >----------------------
[INFO] Building EvosuiteTest 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] >>> evosuite-maven-plugin:1.0.6:generate (default-cli) > compile @ EvosuiteTest >>>
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ EvosuiteTest ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ EvosuiteTest ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] <<< evosuite-maven-plugin:1.0.6:generate (default-cli) < compile @ EvosuiteTest <<<
[INFO] 
[INFO] 
[INFO] --- evosuite-maven-plugin:1.0.6:generate (default-cli) @ EvosuiteTest ---
[INFO] Going to generate tests with EvoSuite
[INFO] Total memory: 800mb
[INFO] Time per class: 2 minutes
[INFO] Number of used cores: 1
[INFO] Target: D:\IdeaProjects\EvosuiteTest\target\classes
[INFO] Basedir: D:\IdeaProjects\EvosuiteTest
[INFO] Started spawn process manager on port 9335
[INFO] SLF4J: Class path contains multiple SLF4J bindings.
[INFO] SLF4J: Found binding in [jar:file:/C:/Users/17199/.m2/repository/org/evosuite/evosuite-master/1.0.6/evosuite-master-1.0.6.jar!/org/slf4j/impl/StaticLoggerBinder.class]
[INFO] SLF4J: Found binding in [jar:file:/C:/Users/17199/.m2/repository/ch/qos/logback/logback-classic/1.1.3/logback-classic-1.1.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
[INFO] SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
[INFO] SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
[INFO] * EvoSuite 1.0.6
[INFO] Registered remote process from /127.0.0.1:9336
[INFO] Going to execute 1 jobs
[INFO] Estimated completion time: 2 minutes, by 2021-12-01T18:24:26.491
[INFO] Going to start job for: hello. Expected to end in 120 seconds, by 2021-12-01T18:24:27.058
[INFO] Registered remote process from /127.0.0.1:9337
[INFO] Registered remote process from /127.0.0.1:9343
[INFO] Completed job. Left: 0
[INFO] * Updating database to hello
[INFO] === CTG run results ===
[INFO] Removed test suites: 0
[INFO] New test suites: 1
[INFO] Stopping spawn process manager
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  16.000 s
[INFO] Finished at: 2021-12-01T18:22:38+08:00
[INFO] ------------------------------------------------------------------------