summaryrefslogtreecommitdiff
path: root/chapter21/src/test/java/com/seaofnodes/simple/SpillStats.java
blob: 318a6e334fc84db96981a5691c02c679abc6ee0d (plain)
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
package com.seaofnodes.simple;

import com.seaofnodes.simple.codegen.CodeGen;
import java.util.Map;
import java.util.TreeMap;
import org.junit.runner.*;
import org.junit.runner.notification.*;

// Run existing tests and sum actual allocations, including ones without goldens.
// Helpers call record after allocation; ordinary JUnit runs collect nothing.
public class SpillStats extends RunListener {
    private static SpillStats ACTIVE;
    private Description _test;
    private int _qualityFailures;
    private final Map<String,long[]> _totals = new TreeMap<>();

    @Override public void testStarted(Description test) { _test = test; }
    public static void record(CodeGen code, String cohort, String cpu, String abi) {
        if( ACTIVE==null ) return;
        String test = ACTIVE._test.getTestClass().getSimpleName()+"."+ACTIVE._test.getMethodName();
        for( String key : new String[]{cohort+","+cpu+","+abi,cohort+",ALL,ALL"} ) {
            long[] sum = ACTIVE._totals.computeIfAbsent(key,k -> new long[3]);
            sum[0]++; sum[1] += code._regAlloc._spills; sum[2] += code._regAlloc._spillScaled;
        }
        System.out.println("allocation,"+cohort+","+test+","+cpu+","+abi+","+
                           code._regAlloc._spills+","+code._regAlloc._spillScaled);
    }

    // Keep running every CPU and native result check when measuring quality.
    // A changed golden still makes spill-stats fail at the end.
    public static void checkSpills(int expected, int actual) {
        if( expected==-1 ) return;
        try { org.junit.Assert.assertEquals("Expect spills:",expected,actual,Math.max(1,expected>>3)); }
        catch( AssertionError error ) {
            if( ACTIVE==null ) throw error;
            ACTIVE._qualityFailures++;
            System.err.println(ACTIVE._test+": "+error.getMessage());
        }
    }

    public static void main(String[] args) throws ClassNotFoundException {
        if( args.length==0 ) args = new String[]{"Chapter20Test","Chapter21Test","Chapter21AllocTest","BrainFuckTest","MergeSortTest"};
        Class<?>[] tests = new Class<?>[args.length];
        for( int i=0; i<args.length; i++ )
            tests[i] = Class.forName("com.seaofnodes.simple."+args[i]);
        ACTIVE = new SpillStats();
        JUnitCore junit = new JUnitCore();
        junit.addListener(ACTIVE);
        Result result = junit.run(tests);
        boolean passed = result.wasSuccessful() && ACTIVE._qualityFailures==0;
        if( !passed ) System.out.println("FAILED test run: totals are not a complete comparison.");
        System.out.println("cohort,cpu,abi,allocations,splits,scaled");
        for( var row : ACTIVE._totals.entrySet() ) {
            long[] sum = row.getValue();
            System.out.println(row.getKey()+","+sum[0]+","+sum[1]+","+sum[2]);
        }
        for( Failure failure : result.getFailures() ) System.err.println(failure.getTrace());
        System.out.println("Tests: "+result.getRunCount()+", failures: "+result.getFailureCount());
        System.out.println("Spill golden failures: "+ACTIVE._qualityFailures);
        ACTIVE = null;
        if( !passed ) System.exit(1);
    }
}