1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-12-22 10:37:59 +01:00

Merge pull request #507 from guusdk/enhanced-debugger-performance

Increase performance of EnhancedDebugger
This commit is contained in:
Florian Schmaus 2021-12-22 09:16:46 +01:00 committed by GitHub
commit ae2394ea83
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -34,7 +34,13 @@ import java.io.Reader;
import java.io.Writer;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -419,37 +425,52 @@ public class EnhancedDebugger extends SmackDebugger {
// Create a special Reader that wraps the main Reader and logs data to the GUI.
ObservableReader debugReader = new ObservableReader(reader);
readerListener = new ReaderListener() {
private final PriorityBlockingQueue<String> buffer = new PriorityBlockingQueue<>();
@Override
public void read(final String str) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
public void read(final String line) {
buffer.add(line);
SwingUtilities.invokeLater(() -> {
List<String> linesToAdd = new ArrayList<>();
String data;
Instant start = Instant.now();
try {
// To reduce overhead/increase performance, try to process up to a certain amount of lines at the
// same time, when they arrive in rapid succession.
while (linesToAdd.size() < 50
&& Duration.between(start, Instant.now()).compareTo(Duration.ofMillis(100)) < 0
&& (data = buffer.poll(10, TimeUnit.MILLISECONDS)) != null) {
linesToAdd.add(data);
}
} catch (InterruptedException e) {
// Interrupted wait-for-poll. Process all data now.
}
if (linesToAdd.isEmpty()) {
return;
}
if (EnhancedDebuggerWindow.PERSISTED_DEBUGGER &&
!EnhancedDebuggerWindow.getInstance().isVisible()) {
// Do not add content if the parent is not visible
return;
}
int index = str.lastIndexOf(">");
if (index != -1) {
if (receivedText.getLineCount() >= EnhancedDebuggerWindow.MAX_TABLE_ROWS) {
// Delete lines from the top, if lines to be added will exceed the maximum.
int linesToDelete = receivedText.getLineCount() + linesToAdd.size() - EnhancedDebuggerWindow.MAX_TABLE_ROWS;
if (linesToDelete > 0) {
try {
receivedText.replaceRange("", 0, receivedText.getLineEndOffset(0));
receivedText.replaceRange("", 0, receivedText.getLineEndOffset(linesToDelete - 1));
}
catch (BadLocationException e) {
LOGGER.log(Level.SEVERE, "Error with line offset, MAX_TABLE_ROWS is set too low: " + EnhancedDebuggerWindow.MAX_TABLE_ROWS, e);
}
}
receivedText.append(str.substring(0, index + 1));
receivedText.append(NEWLINE);
if (str.length() > index) {
receivedText.append(str.substring(index + 1));
}
}
else {
receivedText.append(str);
}
}
// Add the new content.
receivedText.append(String.join(NEWLINE, linesToAdd));
});
}
};
@ -458,34 +479,53 @@ public class EnhancedDebugger extends SmackDebugger {
// Create a special Writer that wraps the main Writer and logs data to the GUI.
ObservableWriter debugWriter = new ObservableWriter(writer);
writerListener = new WriterListener() {
private final PriorityBlockingQueue<String> buffer = new PriorityBlockingQueue<>();
@Override
public void write(final String str) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
public void write(final String line) {
buffer.add(line);
SwingUtilities.invokeLater(() -> {
List<String> linesToAdd = new ArrayList<>();
String data;
Instant start = Instant.now();
try {
// To reduce overhead/increase performance, try to process up to a certain amount of lines at the
// same time, when they arrive in rapid succession.
while (linesToAdd.size() < 50
&& Duration.between(start, Instant.now()).compareTo(Duration.ofMillis(100)) < 0
&& (data = buffer.poll(10, TimeUnit.MILLISECONDS)) != null) {
linesToAdd.add(data);
}
} catch (InterruptedException e) {
// Interrupted wait-for-poll. Process all data now.
}
if (linesToAdd.isEmpty()) {
return;
}
if (EnhancedDebuggerWindow.PERSISTED_DEBUGGER &&
!EnhancedDebuggerWindow.getInstance().isVisible()) {
// Do not add content if the parent is not visible
return;
}
if (sentText.getLineCount() >= EnhancedDebuggerWindow.MAX_TABLE_ROWS) {
// Delete lines from the top, if lines to be added will exceed the maximum.
int linesToDelete = sentText.getLineCount() + linesToAdd.size() - EnhancedDebuggerWindow.MAX_TABLE_ROWS;
if (linesToDelete > 0) {
try {
sentText.replaceRange("", 0, sentText.getLineEndOffset(0));
sentText.replaceRange("", 0, sentText.getLineEndOffset(linesToDelete - 1));
}
catch (BadLocationException e) {
LOGGER.log(Level.SEVERE, "Error with line offset, MAX_TABLE_ROWS is set too low: " + EnhancedDebuggerWindow.MAX_TABLE_ROWS, e);
}
}
sentText.append(str);
if (str.endsWith(">")) {
sentText.append(NEWLINE);
}
}
// Add the new content.
sentText.append(String.join(NEWLINE, linesToAdd));
});
}
};
debugWriter.addWriterListener(writerListener);