Created
October 20, 2015 06:25
-
-
Save Antarix/3915f4fb804be64d8a32 to your computer and use it in GitHub Desktop.
A simple class that can track how long code takes to execute.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.util.Log; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.concurrent.TimeUnit; | |
/** | |
* @author Aidan Follestad (afollestad) | |
*/ | |
public class LagTracker { | |
private static LagTracker mSingleton; | |
private static Map<String, Long> mMap; | |
private boolean mEnabled = true; | |
private LagTracker() { | |
mMap = new HashMap<>(); | |
} | |
public static LagTracker get() { | |
if (mSingleton == null) | |
mSingleton = new LagTracker(); | |
return mSingleton; | |
} | |
public LagTracker enable() { | |
mEnabled = true; | |
return this; | |
} | |
public LagTracker disable() { | |
mEnabled = false; | |
return this; | |
} | |
public void start(String key) { | |
final long start = System.nanoTime(); | |
if (!mEnabled) { | |
if (!mMap.isEmpty()) | |
mMap.clear(); | |
return; | |
} | |
mMap.put(key, start); | |
} | |
public void end(String key) { | |
final long end = System.nanoTime(); | |
if (!mEnabled) { | |
if (!mMap.isEmpty()) | |
mMap.clear(); | |
return; | |
} | |
if (!mMap.containsKey(key)) | |
throw new IllegalStateException("No start time found for " + key); | |
long start = mMap.get(key); | |
long diff = end - start; | |
print(key, diff); | |
mMap.remove(key); | |
} | |
private void print(String key, long diff) { | |
long ms = TimeUnit.NANOSECONDS.toMillis(diff); | |
long s = TimeUnit.NANOSECONDS.toSeconds(diff); | |
Log.d("LagTracker", "[" + key + " completed in]: " + diff + " ns (" + ms + "ms, " + s + "s)"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TrackerExample extends Activity { | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
// Start tracking a task by the name in the string parameter | |
// You can track multiple tasks at the same time using different IDs | |
LagTracker.get().start("RandomTaskNameOrID"); | |
// Simulate taking time to execute | |
for(int i = 0; i < 50; i++) { | |
try { | |
Thread.sleep(10); | |
} catch(Exception ignored) { } | |
} | |
// End tracking a task by the name in the string parameter | |
LagTracker.get().end("RandomTaskNameOrID"); | |
/** | |
* If you were to check the LogCat now, you'd see a log message indicating how long execution took | |
**/ | |
// You can also disable the tracker to stop logging | |
LagTracker.get().disable(); | |
// And of couse, re-enable it if necessary | |
LagTracker.get().enable(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks