Skip to content

Commit

Permalink
Merge pull request #31 from editor-syntax/patch-1
Browse files Browse the repository at this point in the history
patch: Improvements to Exception Handling in ExceptionWrappingBenchmark
  • Loading branch information
fluentfuture authored Nov 29, 2023
2 parents a5b0650 + 6e8e487 commit af97dbc
Showing 1 changed file with 33 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@
import com.google.common.util.concurrent.Futures;
import com.google.mu.util.Maybe;

public class ExceptionWrappingBenchmark {
/**
* Benchmarks for checking and throwing exceptions.
*/
public class ExceptionBenchmark {

@Benchmark
// Benchmark for Futures#getChecked.
void futuresGetChecked(int n) {
IOException exception = new IOException();
CompletableFuture<?> future = new CompletableFuture<>();
Expand All @@ -33,35 +37,47 @@ void futuresGetChecked(int n) {
try {
Futures.getChecked(future, IOException.class);
throw new AssertionError();
} catch (IOException expected) {}
} catch (IOException rethrow) {
throw new AssertionError(rethrow); // rethrow the exception with a more descriptive message
} catch (Exception e) {
throw new AssertionError(e); // handle other exceptions in some other way
}
}
}

// Benchmark for Maybe#orElseThrow.
@Benchmark
void maybeGet(int n) {
IOException exception = new IOException();
for (int i = 0; i < n; i++) {
try {
Maybe.except(exception).orElseThrow();
throw new AssertionError();
} catch (IOException expected) {}
} catch (IOException rethrow) {
throw new AssertionError(rethrow); // rethrow the exception with a more descriptive message
} catch (Exception e) {
throw new AssertionError(e); // handle other exceptions in some other way
}
}
}

@Benchmark
void reserializeException(int n) {
void serializeException(int n) {
// Benchmark for serializing an exception.
IOException exception = new IOException();
for (int i = 0; i < n; i++) {
SerializableTester.reserialize(exception);
}
}

@Benchmark
void reserializeString(int n) {
String string = new String("abc");
void serializeString(int n) {
// Benchmark for serializing a string.
String string = "abc";
for (int i = 0; i < n; i++) {
SerializableTester.reserialize(string);
}
// Benchmark for wrapping an exception.
}

@Benchmark
Expand All @@ -71,18 +87,27 @@ void manualWrapper(int n) {
try {
Maybe.except(exception).orElseThrow(IOException::new);
throw new AssertionError();
} catch (IOException expected) {}
} catch (IOException rethrow) {
throw new AssertionError(rethrow); // rethrow the exception with a more descriptive message
} catch (Exception e) {
throw new AssertionError(e); // handle other exceptions in some other way
}
}
}

// Benchmark for not wrapping an exception.
@Benchmark
void noWrapper(int n) {
IOException exception = new IOException();
for (int i = 0; i < n; i++) {
try {
Maybe.except(exception).orElseThrow(e -> e);
throw new AssertionError();
} catch (IOException expected) {}
} catch (IOException rethrow) {
throw new AssertionError(rethrow); // rethrow the exception with a more descriptive message
} catch (Exception e) {
throw new AssertionError(e); // handle other exceptions in some other way
}
}
}
}

0 comments on commit af97dbc

Please sign in to comment.