-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathexecution_visualiser.html
204 lines (191 loc) · 7.58 KB
/
execution_visualiser.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="Asciidoctor 2.0.18">
<title>Visualising Executions</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:300,300italic,400,400italic,600,600italic%7CNoto+Serif:400,400italic,700,700italic%7CDroid+Sans+Mono:400,700">
<link rel="stylesheet" href="./asciidoctor.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prettify/r298/prettify.min.css">
</head>
<body class="article toc2 toc-left">
<div id="header">
<div id="toc" class="toc2">
<div id="toctitle">Table of Contents</div>
<ul class="sectlevel2">
<li><a href="#_visualising_executions">Visualising Executions</a></li>
</ul>
</div>
</div>
<div id="content">
<div class="sect2">
<h3 id="_visualising_executions">Visualising Executions</h3>
<div class="paragraph">
<p>TestNG allows you to visualise the test method executions via the interface <a href="https://javadoc.io/static/org.testng/testng/7.9.0/org/testng/IExecutionVisualiser.html">IExecutionVisualiser</a>.</p>
</div>
<div class="paragraph">
<p>TestNG passes metadata about the current execution state in <a href="https://graphviz.org/doc/info/lang.html">dot</a> representation.</p>
</div>
<div class="paragraph">
<p>Color coding is done as below:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>tests that are yet to run are colored in <strong>yellow</strong>.</p>
</li>
<li>
<p>tests that are currently running are colored in <strong>green</strong>.</p>
</li>
<li>
<p>tests that have completed execution are colored in <strong>grey</strong>.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>This listener should be declared, as explained in the section about <a href="testng_listeners.html">TestNG listeners</a>.</p>
</div>
<div class="paragraph">
<p>Let’s a sample.</p>
</div>
<div class="sect3">
<h4 id="_setup">Setup</h4>
<div class="ulist">
<ul>
<li>
<p>Follow the instructions detailed in <a href="https://graphviz.org/download/">graphviz</a> website to install <code>graphviz</code>. This binary will help us convert the <code>dot</code> format into <code>png</code> files.</p>
</li>
<li>
<p>Add a dependency to a library such as <a href="https://github.com/square/gifencoder/">gifencoder</a> so that we can convert a bunch of <code>png</code> files into a <code>gif</code> file.</p>
</li>
</ul>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="xml"><dependency>
<groupId>com.squareup</groupId>
<artifactId>gifencoder</artifactId>
<version>0.10.1</version>
</dependency></code></pre>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_code_sample">Code sample</h4>
<div class="paragraph">
<p>Here’s how a sample implementation could look like:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="java">import com.squareup.gifencoder.FloydSteinbergDitherer;
import com.squareup.gifencoder.GifEncoder;
import com.squareup.gifencoder.ImageOptions;
import org.testng.IExecutionVisualiser;
import org.testng.IReporter;
import org.testng.ISuite;
import org.testng.xml.XmlSuite;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
public class SampleVisualiser implements IExecutionVisualiser, IReporter {
private final AtomicInteger counter = new AtomicInteger(1);
private final Path tmpdir;
private final List<String> pngFiles = new ArrayList<>();
public SampleVisualiser() throws IOException {
tmpdir = Files.createTempDirectory(Paths.get("target"), "dot-");
}
@Override
public void consumeDotDefinition(String dotDefinition) {
String filePrefix = counter.getAndIncrement() + "-" + UUID.randomUUID();
Path input = Path.of(tmpdir.toFile().getAbsolutePath(), filePrefix + "-input.dot");
try {
Files.writeString(input, dotDefinition);
Path output = Path.of(tmpdir.toFile().getAbsolutePath(), filePrefix + "-output.png");
if (generatePngFileFromDotContent(input, output)) {
pngFiles.add(output.toFile().getAbsolutePath());
}
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
}
@Override
public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
//The GIF image will be created with file name "animation.gif"
File writeTo = new File(tmpdir.toFile().getAbsolutePath(), "animation.gif");
try (FileOutputStream outputStream = new FileOutputStream(writeTo)) {
ImageOptions options = new ImageOptions();
//Set 500ms between each frame
options.setDelay(500, TimeUnit.MILLISECONDS);
//Use Floyd Steinberg dithering as it yields the best quality
options.setDitherer(FloydSteinbergDitherer.INSTANCE);
GifEncoder encoder = new GifEncoder(outputStream, 1600, 1200, 0);
for (String pngFile : pngFiles) {
encoder.addImage(convertImageToArray(new File(pngFile)), options);
}
encoder.finishEncoding();
} catch (IOException e) {
throw new RuntimeException(e);
}
System.err.println("The gif has been generated in :" + writeTo.getAbsolutePath());
}
private int[][] convertImageToArray(File file) throws IOException {
BufferedImage bufferedImage = ImageIO.read(file);
int[][] rgbArray = new int[bufferedImage.getHeight()][bufferedImage.getWidth()];
for (int i = 0; i < bufferedImage.getHeight(); i++) {
for (int j = 0; j < bufferedImage.getWidth(); j++) {
rgbArray[i][j] = bufferedImage.getRGB(j, i);
}
}
return rgbArray;
}
private static boolean generatePngFileFromDotContent(Path input, Path output) throws IOException, InterruptedException {
String[] cmds = {
"dot",
"-Tpng",
input.toFile().getAbsolutePath(),
"-o",
output.toFile().getAbsolutePath()
};
Process process = new ProcessBuilder().command(cmds).start();
handleInputErrorFrom(process);
int exitCode = process.waitFor();
System.err.println("PNG file written to " + output.toFile().getAbsolutePath());
return exitCode == 0;
}
private static void handleInputErrorFrom(Process process) throws IOException {
processStream(process.getInputStream());
processStream(process.getErrorStream());
}
private static void processStream(InputStream stream) throws IOException {
if (stream == null) {
return;
}
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
}</code></pre>
</div>
</div>
</div>
</div>
</div>
<div id="footer">
<div id="footer-text">
Last updated 2025-02-27 18:51:48 UTC
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prettify/r298/run_prettify.min.js"></script>
</body>
</html>