mirror of
https://github.com/vanitasvitae/Smack.git
synced 2024-12-22 10:37:59 +01:00
Increase performance of EnhancedDebugger
When using the Enhanced Debugger, a UI application is very noticably slow, especially around establishing a new session. Profiling shows that much of the overhead is caused by XMPP-data being added to the text areas that are on the "raw sent packets" and "raw received packets" tabs of the debugger. This commit improves performance (considerably) by: - properly limiting the amount of lines in those text areas (this was intended but broken in the old implementation) - buffering data to be added in batches, to reduce the amount of invocations to JTextChat.append() As an aside: some newline-based formatting was removed. As the provided data is now already formatted, retaining that did not make much sense anymore.
This commit is contained in:
parent
b3637101ce
commit
8be1568a5d
1 changed files with 89 additions and 49 deletions
|
@ -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() {
|
||||||
@Override
|
private final PriorityBlockingQueue<String> buffer = new PriorityBlockingQueue<>();
|
||||||
public void read(final String str) {
|
|
||||||
SwingUtilities.invokeLater(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
if (EnhancedDebuggerWindow.PERSISTED_DEBUGGER &&
|
|
||||||
!EnhancedDebuggerWindow.getInstance().isVisible()) {
|
|
||||||
// Do not add content if the parent is not visible
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
int index = str.lastIndexOf(">");
|
@Override
|
||||||
if (index != -1) {
|
public void read(final String line) {
|
||||||
if (receivedText.getLineCount() >= EnhancedDebuggerWindow.MAX_TABLE_ROWS) {
|
|
||||||
try {
|
buffer.add(line);
|
||||||
receivedText.replaceRange("", 0, receivedText.getLineEndOffset(0));
|
|
||||||
}
|
SwingUtilities.invokeLater(() -> {
|
||||||
catch (BadLocationException e) {
|
List<String> linesToAdd = new ArrayList<>();
|
||||||
LOGGER.log(Level.SEVERE, "Error with line offset, MAX_TABLE_ROWS is set too low: " + EnhancedDebuggerWindow.MAX_TABLE_ROWS, e);
|
String data;
|
||||||
}
|
Instant start = Instant.now();
|
||||||
}
|
try {
|
||||||
receivedText.append(str.substring(0, index + 1));
|
// To reduce overhead/increase performance, try to process up to a certain amount of lines at the
|
||||||
receivedText.append(NEWLINE);
|
// same time, when they arrive in rapid succession.
|
||||||
if (str.length() > index) {
|
while (linesToAdd.size() < 50
|
||||||
receivedText.append(str.substring(index + 1));
|
&& Duration.between(start, Instant.now()).compareTo(Duration.ofMillis(100)) < 0
|
||||||
}
|
&& (data = buffer.poll(10, TimeUnit.MILLISECONDS)) != null) {
|
||||||
|
linesToAdd.add(data);
|
||||||
}
|
}
|
||||||
else {
|
} catch (InterruptedException e) {
|
||||||
receivedText.append(str);
|
// 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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(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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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.
|
// 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
|
|
||||||
public void run() {
|
|
||||||
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) {
|
buffer.add(line);
|
||||||
try {
|
|
||||||
sentText.replaceRange("", 0, sentText.getLineEndOffset(0));
|
|
||||||
}
|
|
||||||
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);
|
SwingUtilities.invokeLater(() -> {
|
||||||
if (str.endsWith(">")) {
|
List<String> linesToAdd = new ArrayList<>();
|
||||||
sentText.append(NEWLINE);
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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(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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add the new content.
|
||||||
|
sentText.append(String.join(NEWLINE, linesToAdd));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
debugWriter.addWriterListener(writerListener);
|
debugWriter.addWriterListener(writerListener);
|
||||||
|
|
Loading…
Reference in a new issue