-
Notifications
You must be signed in to change notification settings - Fork 11
/
TestParseApp.java
153 lines (124 loc) · 5 KB
/
TestParseApp.java
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
/*******************************************************************************
* Copyright (c) 2008, 2019 SAP AG and IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* SAP AG - initial API and implementation
* Andrew Johnson (IBM Corporation) - Xmx and thread numbers
*******************************************************************************/
package org.eclipse.mat.tests.regression;
import java.io.File;
import java.io.PrintStream;
import org.eclipse.mat.internal.snapshot.SnapshotQueryContext;
import org.eclipse.mat.report.Spec;
import org.eclipse.mat.report.TestSuite;
import org.eclipse.mat.snapshot.ISnapshot;
import org.eclipse.mat.snapshot.SnapshotFactory;
import org.eclipse.mat.util.IProgressListener;
@SuppressWarnings("restriction")
public class TestParseApp
{
private File snapshotFile;
private Spec report;
private boolean showMem;
public TestParseApp(File snapshotFile, Spec report, boolean showMem)
{
this.snapshotFile = snapshotFile;
this.report = report;
this.showMem = showMem;
}
public void run() throws Exception
{
IProgressListener listener = new ClockedProgressListener(snapshotFile, System.out, showMem);
ISnapshot snapshot = null;
try
{
snapshot = SnapshotFactory.openSnapshot(snapshotFile, listener);
TestSuite suite = new TestSuite.Builder(report) //
.build(new SnapshotQueryContext(snapshot));
suite.execute(listener);
}
finally
{
if (snapshot != null) SnapshotFactory.dispose(snapshot);
// If there was an error then this will still show the last message
listener.done();
}
}
private static class ClockedProgressListener implements IProgressListener
{
private PrintStream out;
private int count = 0;
private String absolutePath;
private String fileName;
private boolean showMem;
private long timestamp = 0;
private String lastMessage = null;
/* package */ClockedProgressListener(File snapshot, PrintStream out, boolean showMemUsage)
{
this.out = out;
this.timestamp = System.currentTimeMillis();
this.absolutePath = snapshot.getParentFile().getAbsolutePath();
this.fileName = snapshot.getName();
int p = this.fileName.lastIndexOf('.');
if (p >= 0)
this.fileName = this.fileName.substring(0, p);
this.showMem = showMemUsage;
}
public void beginTask(String name, int totalWork)
{
subTask(name);
}
public void subTask(String name)
{
long now = System.currentTimeMillis();
if (lastMessage != null)
{
int p = lastMessage.indexOf(absolutePath);
if (p >= 0)
lastMessage = lastMessage.substring(0, p) + "DIR"
+ lastMessage.substring(p + absolutePath.length());
p = lastMessage.indexOf(fileName);
if (p >= 0)
lastMessage = lastMessage.substring(0, p) + "DUMP" + lastMessage.substring(p + fileName.length());
if (showMem)
{
long freeMem = Runtime.getRuntime().freeMemory();
long totalMem = Runtime.getRuntime().totalMemory();
long maxMem = Runtime.getRuntime().maxMemory();
long usedMem = totalMem - freeMem;
out.println(String.format("Task: [%02d] %s %s ms used %d free %d total %d max %d", ++count, lastMessage, String
.valueOf(now - timestamp), usedMem, freeMem, totalMem, maxMem));
}
else
{
out.println(String.format("Task: [%02d] %s %s ms", ++count, lastMessage, String
.valueOf(now - timestamp)));
}
}
this.lastMessage = name;
this.timestamp = System.currentTimeMillis();
}
public void done()
{
subTask(null);
}
public boolean isCanceled()
{
return false;
}
public void sendUserMessage(Severity severity, String message, Throwable exception)
{
System.out.println(message);
if (exception != null)
exception.printStackTrace(System.out);
}
public void setCanceled(boolean value)
{}
public void worked(int work)
{}
}
}