Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perf: use buffered streams for IO #307

Merged
merged 7 commits into from
May 10, 2018
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions sdk/src/androidTest/java/com/bugsnag/android/JsonWriterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.bugsnag.android;

import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import static org.junit.Assert.*;

public class JsonWriterTest {

@Test(expected = IOException.class)
public void testClose() throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(baos);
JsonWriter jsonWriter = new JsonWriter(writer);

jsonWriter.beginObject().endObject();
jsonWriter.flush();
assertEquals("{}", new String(baos.toByteArray(), "UTF-8"));

jsonWriter.close();
writer.write(5); // can't write to a closed stream, throws IOException
}

}
13 changes: 8 additions & 5 deletions sdk/src/main/java/com/bugsnag/android/DefaultHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Map;

class DefaultHttpClient implements ErrorReportApiClient, SessionTrackingApiClient {
Expand Down Expand Up @@ -65,15 +67,16 @@ private int makeRequest(String urlString,
conn.addRequestProperty(entry.getKey(), entry.getValue());
}

OutputStream out = null;
JsonStream stream = null;

try {
out = conn.getOutputStream();
JsonStream stream = new JsonStream(new OutputStreamWriter(out));
OutputStream out = conn.getOutputStream();
Charset charset = Charset.forName("UTF-8");
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, charset));
stream = new JsonStream(writer);
streamable.toStream(stream);
stream.close();
} finally {
IOUtils.closeQuietly(out);
IOUtils.closeQuietly(stream);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will closing the JsonStream close the OutputStream as well? There's no explicit closing of the OutputStream.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes - also answered in previous comments in a bit more depth.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, is this verifiable or testable at all? It seems like something that could change across implementations/versions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a test to check that JsonWriter closes its underlying stream - beyond that we'd just be testing the behaviour of the Java standard library.

}


Expand Down
15 changes: 8 additions & 7 deletions sdk/src/main/java/com/bugsnag/android/FileStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -83,23 +86,21 @@ String write(@NonNull T streamable) {

String filename = getFilename(streamable);

Writer out = null;
JsonStream stream = null;
lock.lock();

try {
out = new FileWriter(filename);

JsonStream stream = new JsonStream(out);
FileOutputStream fos = new FileOutputStream(filename);
Writer out = new BufferedWriter(new OutputStreamWriter(fos, "UTF-8"));
stream = new JsonStream(out);
stream.value(streamable);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not related to this PR but should we be closing this stream on the next line in the finally block?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that would make more sense than closing two separate streams. I've updated the PR so that we do this.

stream.close();

Logger.info(String.format("Saved unsent payload to disk (%s) ", filename));
return filename;
} catch (Exception exception) {
Logger.warn(String.format("Couldn't save unsent payload to disk (%s) ",
filename), exception);
} finally {
IOUtils.closeQuietly(out);
IOUtils.closeQuietly(stream);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same Q as above.

lock.unlock();
}
return null;
Expand Down
10 changes: 7 additions & 3 deletions sdk/src/main/java/com/bugsnag/android/JsonStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.Writer;

public class JsonStream extends JsonWriter {
Expand Down Expand Up @@ -57,9 +60,10 @@ public void value(@NonNull File file) throws IOException {
beforeValue(false); // add comma if in array

// Copy the file contents onto the stream
FileReader input = null;
Reader input = null;
try {
input = new FileReader(file);
FileInputStream fis = new FileInputStream(file);
input = new BufferedReader(new InputStreamReader(fis, "UTF-8"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do any of these objects also need closing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe calling BufferedReader#close also closes the InputStreamReader, which in turn closes the FileInputStream, which means that everything is automatically closed.

IOUtils.copy(input, out);
} finally {
IOUtils.closeQuietly(input);
Expand Down