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.io.Writer;
import java.net.URL; import java.net.URL;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Date; 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.Level;
import java.util.logging.Logger; 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. // Create a special Reader that wraps the main Reader and logs data to the GUI.
ObservableReader debugReader = new ObservableReader(reader); ObservableReader debugReader = new ObservableReader(reader);
readerListener = new ReaderListener() { readerListener = new ReaderListener() {
private final PriorityBlockingQueue<String> buffer = new PriorityBlockingQueue<>();
@Override @Override
public void read(final String str) { public void read(final String line) {
SwingUtilities.invokeLater(new Runnable() {
@Override buffer.add(line);
public void run() {
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 && if (EnhancedDebuggerWindow.PERSISTED_DEBUGGER &&
!EnhancedDebuggerWindow.getInstance().isVisible()) { !EnhancedDebuggerWindow.getInstance().isVisible()) {
// Do not add content if the parent is not visible // Do not add content if the parent is not visible
return; return;
} }
int index = str.lastIndexOf(">"); // Delete lines from the top, if lines to be added will exceed the maximum.
if (index != -1) { int linesToDelete = receivedText.getLineCount() + linesToAdd.size() - EnhancedDebuggerWindow.MAX_TABLE_ROWS;
if (receivedText.getLineCount() >= EnhancedDebuggerWindow.MAX_TABLE_ROWS) { if (linesToDelete > 0) {
try { try {
receivedText.replaceRange("", 0, receivedText.getLineEndOffset(0)); receivedText.replaceRange("", 0, receivedText.getLineEndOffset(linesToDelete - 1));
} }
catch (BadLocationException e) { catch (BadLocationException e) {
LOGGER.log(Level.SEVERE, "Error with line offset, MAX_TABLE_ROWS is set too low: " + EnhancedDebuggerWindow.MAX_TABLE_ROWS, 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); // Add the new content.
if (str.length() > index) { receivedText.append(String.join(NEWLINE, linesToAdd));
receivedText.append(str.substring(index + 1));
}
}
else {
receivedText.append(str);
}
}
}); });
} }
}; };
@ -458,34 +479,53 @@ public class EnhancedDebugger extends SmackDebugger {
// Create a special Writer that wraps the main Writer and logs data to the GUI. // Create a special Writer that wraps the main Writer and logs data to the GUI.
ObservableWriter debugWriter = new ObservableWriter(writer); ObservableWriter debugWriter = new ObservableWriter(writer);
writerListener = new WriterListener() { writerListener = new WriterListener() {
private final PriorityBlockingQueue<String> buffer = new PriorityBlockingQueue<>();
@Override @Override
public void write(final String str) { public void write(final String line) {
SwingUtilities.invokeLater(new Runnable() {
@Override buffer.add(line);
public void run() {
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 && if (EnhancedDebuggerWindow.PERSISTED_DEBUGGER &&
!EnhancedDebuggerWindow.getInstance().isVisible()) { !EnhancedDebuggerWindow.getInstance().isVisible()) {
// Do not add content if the parent is not visible // Do not add content if the parent is not visible
return; 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 { try {
sentText.replaceRange("", 0, sentText.getLineEndOffset(0)); sentText.replaceRange("", 0, sentText.getLineEndOffset(linesToDelete - 1));
} }
catch (BadLocationException e) { catch (BadLocationException e) {
LOGGER.log(Level.SEVERE, "Error with line offset, MAX_TABLE_ROWS is set too low: " + EnhancedDebuggerWindow.MAX_TABLE_ROWS, e); LOGGER.log(Level.SEVERE, "Error with line offset, MAX_TABLE_ROWS is set too low: " + EnhancedDebuggerWindow.MAX_TABLE_ROWS, e);
} }
} }
sentText.append(str); // Add the new content.
if (str.endsWith(">")) { sentText.append(String.join(NEWLINE, linesToAdd));
sentText.append(NEWLINE);
}
}
}); });
} }
}; };
debugWriter.addWriterListener(writerListener); debugWriter.addWriterListener(writerListener);