From bcebf58b76a7061663d930af28592c1ca9ebf2f6 Mon Sep 17 00:00:00 2001 From: Greyson Parrelli Date: Thu, 20 Sep 2018 13:24:29 -0700 Subject: [PATCH] Added a new Stopwatch class to easily log timings. --- .../securesms/util/Stopwatch.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/org/thoughtcrime/securesms/util/Stopwatch.java diff --git a/src/org/thoughtcrime/securesms/util/Stopwatch.java b/src/org/thoughtcrime/securesms/util/Stopwatch.java new file mode 100644 index 0000000000..58aa34a3c8 --- /dev/null +++ b/src/org/thoughtcrime/securesms/util/Stopwatch.java @@ -0,0 +1,58 @@ +package org.thoughtcrime.securesms.util; + +import android.support.annotation.NonNull; + +import org.thoughtcrime.securesms.logging.Log; + +import java.util.LinkedList; +import java.util.List; + +public class Stopwatch { + + private final long startTime; + private final String title; + private final List splits; + + public Stopwatch(@NonNull String title) { + this.startTime = System.currentTimeMillis(); + this.title = title; + this.splits = new LinkedList<>(); + } + + public void split(@NonNull String label) { + splits.add(new Split(System.currentTimeMillis(), label)); + } + + public void stop(@NonNull String tag) { + StringBuilder out = new StringBuilder(); + out.append("[").append(title).append("] "); + + if (splits.size() > 0) { + out.append(splits.get(0).label).append(": "); + out.append(splits.get(0).time - startTime); + out.append(" "); + } + + if (splits.size() > 1) { + for (int i = 1; i < splits.size(); i++) { + out.append(splits.get(i).label).append(": "); + out.append(splits.get(i).time - splits.get(i - 1).time); + out.append(" "); + } + + out.append("total: ").append(splits.get(splits.size() - 1).time - startTime); + } + + Log.d(tag, out.toString()); + } + + private static class Split { + final long time; + final String label; + + Split(long time, String label) { + this.time = time; + this.label = label; + } + } +}