-
Notifications
You must be signed in to change notification settings - Fork 1
/
copper.groovy
279 lines (249 loc) · 9.18 KB
/
copper.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper
import abs.*
//import java.nio.file.Paths
//import abs.callgraph.BFS
//import abs.callgraph.BenchmarkFinderImpl
//import abs.callgraph.InterfaceImplementerAll
//import abs.callgraph.StaticWalker
//import abs.callgraph.TypeSolverFactory
//TODO: switch dependency as soon as version 0.5.3 comes out. This is a hacky solution that needs manually changing the grape/ivy repository
// @Grab(group='com.github.javaparser', module='java-symbol-solver-core', version='0.5.2-cl')
def writeToLog(def logfile, def run, def name, def result) {
result.each { benchmark, data ->
data.each { item ->
logfile.append("$run;$name;$benchmark;$item\n")
}
}
}
def defrostResultsFromLog(def filename) {
def results = [:]
new File(filename).splitEachLine(";") { fields ->
def run = fields[0] as int
def method = fields[1]
def benchmark = fields[2]
def val = fields[3] as double
if(!results.containsKey(run))
results[run] = [:]
if(!results[run].containsKey(method))
results[run][method] = [:]
if(!results[run][method].containsKey(benchmark))
results[run][method][benchmark] = []
results[run][method][benchmark] << val
}
return results
}
def do_run(def runnr) {
BenchmarkRunner runner =
(config.build_system && config.build_system == 'gradle') ?
new GradleJMHBenchmarkRunner(config.project, config.benchmarks, config.benchmark_jar, config.custom_benchmark_config, config.gradle_target) :
new MvnJMHBenchmarkRunner(config.project, config.benchmarks, config.benchmark_jar, config.custom_benchmark_config)
if(config.benchmarks_to_execute) {
def parsed = parseBenchmarksToExecute(config.benchmarks_to_execute)
runner.setBenchmarksToExecute(parsed)
}
// config sanity check
config.files.each { file ->
file.methods.each { method ->
RegressionInducer changer = new PerfRegressionInducer("${config.project}/${file.test_file}",
method.name, method.params, config.degree_of_violation as double)
changer.doUpdate()
changer.resetChanges()
}
}
println "##### Config seems ok #####"
new File("codedumps/$runnr").mkdir()
println "##### Baseline Run $runnr #####"
// baseline run
def baselineResult = runner.run(new EmptyRegressionInducer(), "")
writeToLog(logfile, runnr, "Baseline", baselineResult)
println "##### Baseline Run $runnr Finished #####"
// test runs
def results = [:]
config.files.each { file ->
if (file == null || file.test_file == null) {
println "##### Empty file, not executing"
return
}
println "##### Started Running $runnr for ${file.test_file} #####"
def testfile = file.test_file
def dumpFileName = testfile.replaceAll("/",".").replaceAll(".java","")
new File("codedumps/$runnr/$dumpFileName").mkdir()
file.methods.each { method ->
println "##### Test run $runnr for ${method.name} #####"
RegressionInducer changer = new PerfRegressionInducer("${config.project}/$testfile",
method.name, method.params, config.degree_of_violation as double)
def testResult = runner.run(changer, "codedumps/$runnr/$dumpFileName/${method.name}")
def fullname = "$testfile.${method.name}(${method.params})"
results[fullname] = testResult
writeToLog(logfile, runnr, fullname, testResult)
}
}
println "##### Finished $runnr #####"
}
def parseBenchmarksToExecute(def listOfConfigs) {
listOfConfigs.collect{ config ->
new BenchmarkToExecute(pattern:config.pattern, params:config.params)
}
}
def detectableSlowdown(def allresults, def method, def run) {
def runresults = allresults[run]
def baselineresults = collectBaselineResults(allresults)
def testresults = runresults.subMap([method])
def tester = new TTester()
def pVals = tester.testForChanges(baselineresults, testresults[method])
// Bonferroni correction
def correctedAlpha = Double.parseDouble(config.confidence) / pVals.size()
def activeTests = pVals.findAll{ _, entry ->
entry["p"] < correctedAlpha && entry["dm"] > (config.min_effect_size as double)
}
println "For $method in run $run, ${activeTests.size()} benchmarks showed a difference (of ${pVals.size()})"
if(activeTests.size() > 0) {
println "Indicating benchmarks:"
activeTests.each{m, r -> println " $m" }
}
return activeTests
}
def collectBaselineResults(def all) {
def maps = all.collect{_, run ->
run['Baseline']
}
def collectedMaps = [:]
maps[0].each{ key, val ->
collectedMaps[key] = maps.collect{ it[key] }.flatten()
}
return collectedMaps
}
def buildProject(config) {
if (!config.scg.compile) {
return
}
procStr = ""
if (config.build_system && config.build_system == "gradle") {
procStr = "./gradlew ${config.gradle_target}"
} else {
// assume maven
procStr = "mvn clean install -DskipTests"
}
def proc = procStr.execute(null, new File(config.project))
proc.in.eachLine { line -> println line }
proc.out.close()
proc.waitFor()
}
def parseArgs(args) {
def cli = new CliBuilder(usage: 'copper')
cli.d('run dynamic ptc')
cli.s('run static callgraph ptc')
cli.c('config file', required:true, args:1)
def options = cli.parse(args)
if (options == null) {
return null
}
if (!options.getProperty('d') && !options.getProperty('s')) {
println("error: Missing required option: either d or s")
cli.usage()
return null
}
return options
}
def options = parseArgs(this.args)
if (options == null) {
return
}
def configPath = options.getProperty('c')
println("# load config file: $configPath")
def slurper = new JsonSlurper()
config = slurper.parse(new File(configPath))
// dynamic approach
if (options.getProperty('d')) {
logfile = new File((String)config.log)
logfile.write("")
repeats = config.repeats as int
println "##### Creating directory for code dumps #####"
if(new File("codedumps").exists()) {
new File("codedumps").deleteDir()
}
new File("codedumps").mkdir()
repeats.times { run ->
do_run(run)
}
allResults = defrostResultsFromLog((String)config.log)
// allResults = defrostResultsFromLog("/Users/philipp/Downloads/results/trial_run_1/result_run3_0.5_0.4_3.csv")
runs = allResults.keySet()
methods = []
allResults.each {run, results ->
results.each { method, _ ->
methods << method
}
}
methods = methods.unique() - "Baseline"
slowdownDetections = methods.count { method ->
def slowdownsDetected = runs.collect { run ->
detectableSlowdown(allResults, method, run)
}
def benchmarks = allResults[0]["Baseline"].collect{ it.key }
def allDetected = benchmarks.any{benchmark ->
slowdownsDetected.every{run ->
run.keySet().contains(benchmark)
}
}
return allDetected
}
ptc = slowdownDetections / (double)methods.size()
println "Dynamic coverage value was ${ptc}"
}
// // static approach
// if (options.getProperty('s')) {
// // compile project
// buildProject(config)
// // get jars
// def filePattern = config.scg.filePattern
// if (filePattern == null || filePattern == "") {
// filePattern = ".*"
// }
//
// def jars = Project.jars(config.project, config.scg.jars, filePattern)
// def typeSolver = TypeSolverFactory.get(jars)
//
// def bf = new BenchmarkFinderImpl(typeSolver)
// def benchmarkMethods = bf.all(Paths.get(config.project, config.benchmarks).toString())
//
// // run call graph walker
// def scg = new StaticWalker(config.project, config.scg.lib, config.scg.qualifiedPathPrefix, config.files, benchmarkMethods)
// jars.each { jar ->
// def path = jar.getAbsolutePath()
// println("add jar '${path}'")
// scg.addJar(path)
// }
// scg.addInterfaceToClassEdges(new InterfaceImplementerAll(config.project, typeSolver))
// def finder = new BFS()
// def rms = scg.reachableMethods(benchmarkMethods, finder)
//
// def found = new HashSet()
// rms.forEach({ _, fs ->
// fs.each { f ->
// found.add(f)
// }
// })
// // calculate static performance test coverage
// def sum = 0
// config.files.each { f ->
// sum += f.methods.size()
// }
// def ptc = 0
// if (sum != 0) {
// ptc = found.size()*1.0 / sum
// }
// println("Core Method Finder: ${finder.noExceptions} successful searches; ${finder.exceptions} exceptions")
// println("Static call graph coverage value was ${ptc}")
//
// // print results
// String outPath = config.scg.out
// if (outPath != null && outPath != "") {
// def outValues = new HashMap()
// outValues.put("coverage", ptc)
// outValues.put("methods", rms)
// def out = new File(outPath)
// out.write(new JsonBuilder(outValues).toPrettyString())
// }
// }