Use System.nanoTime() for timing measurements.

This commit is contained in:
akwizgran
2018-06-15 12:04:12 +01:00
parent ccee1febbc
commit 08931e64cb
23 changed files with 136 additions and 102 deletions

View File

@@ -4,11 +4,11 @@ import android.net.TrafficStats;
import android.os.Process;
import org.briarproject.bramble.api.lifecycle.Service;
import org.briarproject.bramble.api.lifecycle.ServiceException;
import java.util.logging.Logger;
import static java.util.logging.Level.INFO;
import static org.briarproject.bramble.util.TimeUtils.now;
class NetworkUsageLogger implements Service {
@@ -18,17 +18,17 @@ class NetworkUsageLogger implements Service {
private volatile long startTime, rxBytes, txBytes;
@Override
public void startService() throws ServiceException {
startTime = System.currentTimeMillis();
public void startService() {
startTime = now();
int uid = Process.myUid();
rxBytes = TrafficStats.getUidRxBytes(uid);
txBytes = TrafficStats.getUidTxBytes(uid);
}
@Override
public void stopService() throws ServiceException {
public void stopService() {
if (LOG.isLoggable(INFO)) {
long sessionDuration = System.currentTimeMillis() - startTime;
long sessionDuration = now() - startTime;
int uid = Process.myUid();
long rx = TrafficStats.getUidRxBytes(uid) - rxBytes;
long tx = TrafficStats.getUidTxBytes(uid) - txBytes;

View File

@@ -34,6 +34,7 @@ import javax.annotation.Nullable;
import static java.util.logging.Level.FINE;
import static java.util.logging.Level.WARNING;
import static org.briarproject.bramble.util.TimeUtils.now;
import static org.briarproject.briar.util.HtmlUtils.ARTICLE;
@MethodsNotNullByDefault
@@ -109,20 +110,20 @@ abstract class BaseControllerImpl extends DbControllerImpl
}
Collection<BlogPostItem> loadItems(GroupId groupId) throws DbException {
long now = System.currentTimeMillis();
long start = now();
Collection<BlogPostHeader> headers =
blogManager.getPostHeaders(groupId);
long duration = System.currentTimeMillis() - now;
long duration = now() - start;
if (LOG.isLoggable(FINE))
LOG.fine("Loading headers took " + duration + " ms");
Collection<BlogPostItem> items = new ArrayList<>(headers.size());
now = System.currentTimeMillis();
start = now();
for (BlogPostHeader h : headers) {
headerCache.put(h.getId(), h);
BlogPostItem item = getItem(h);
items.add(item);
}
duration = System.currentTimeMillis() - now;
duration = now() - start;
if (LOG.isLoggable(FINE))
LOG.fine("Loading bodies took " + duration + " ms");
return items;
@@ -140,9 +141,9 @@ abstract class BaseControllerImpl extends DbControllerImpl
}
runOnDbThread(() -> {
try {
long now = System.currentTimeMillis();
long start = now();
BlogPostItem item = getItem(header);
long duration = System.currentTimeMillis() - now;
long duration = now() - start;
if (LOG.isLoggable(FINE))
LOG.fine("Loading body took " + duration + " ms");
handler.onResult(item);
@@ -166,10 +167,10 @@ abstract class BaseControllerImpl extends DbControllerImpl
}
runOnDbThread(() -> {
try {
long now = System.currentTimeMillis();
long start = now();
BlogPostHeader header1 = getPostHeader(g, m);
BlogPostItem item = getItem(header1);
long duration = System.currentTimeMillis() - now;
long duration = now() - start;
if (LOG.isLoggable(FINE))
LOG.fine("Loading post took " + duration + " ms");
handler.onResult(item);

View File

@@ -37,6 +37,7 @@ import javax.inject.Inject;
import static java.util.logging.Level.FINE;
import static java.util.logging.Level.WARNING;
import static org.briarproject.bramble.util.TimeUtils.now;
@MethodsNotNullByDefault
@ParametersNotNullByDefault
@@ -154,13 +155,13 @@ class BlogControllerImpl extends BaseControllerImpl
if (groupId == null) throw new IllegalStateException();
runOnDbThread(() -> {
try {
long now = System.currentTimeMillis();
long start = now();
LocalAuthor a = identityManager.getLocalAuthor();
Blog b = blogManager.getBlog(groupId);
boolean ours = a.getId().equals(b.getAuthor().getId());
boolean removable = blogManager.canBeRemoved(b);
BlogItem blog = new BlogItem(b, ours, removable);
long duration = System.currentTimeMillis() - now;
long duration = now() - start;
if (LOG.isLoggable(FINE))
LOG.fine("Loading blog took " + duration + " ms");
handler.onResult(blog);
@@ -177,10 +178,10 @@ class BlogControllerImpl extends BaseControllerImpl
if (groupId == null) throw new IllegalStateException();
runOnDbThread(() -> {
try {
long now = System.currentTimeMillis();
long start = now();
Blog b = blogManager.getBlog(groupId);
blogManager.removeBlog(b);
long duration = System.currentTimeMillis() - now;
long duration = now() - start;
if (LOG.isLoggable(FINE))
LOG.fine("Removing blog took " + duration + " ms");
handler.onResult(null);

View File

@@ -28,6 +28,7 @@ import javax.inject.Inject;
import static java.util.logging.Level.FINE;
import static java.util.logging.Level.WARNING;
import static org.briarproject.bramble.util.TimeUtils.now;
import static org.briarproject.briar.api.blog.BlogManager.CLIENT_ID;
@MethodsNotNullByDefault
@@ -99,7 +100,7 @@ class FeedControllerImpl extends BaseControllerImpl
ResultExceptionHandler<Collection<BlogPostItem>, DbException> handler) {
runOnDbThread(() -> {
try {
long now = System.currentTimeMillis();
long start = now();
Collection<BlogPostItem> posts = new ArrayList<>();
for (Blog b : blogManager.getBlogs()) {
try {
@@ -109,7 +110,7 @@ class FeedControllerImpl extends BaseControllerImpl
LOG.log(WARNING, e.toString(), e);
}
}
long duration = System.currentTimeMillis() - now;
long duration = now() - start;
if (LOG.isLoggable(FINE))
LOG.fine("Loading all posts took " + duration + " ms");
handler.onResult(posts);
@@ -125,10 +126,10 @@ class FeedControllerImpl extends BaseControllerImpl
ResultExceptionHandler<Blog, DbException> handler) {
runOnDbThread(() -> {
try {
long now = System.currentTimeMillis();
long start = now();
Author a = identityManager.getLocalAuthor();
Blog b = blogManager.getPersonalBlog(a);
long duration = System.currentTimeMillis() - now;
long duration = now() - start;
if (LOG.isLoggable(FINE))
LOG.fine("Loading blog took " + duration + " ms");
handler.onResult(b);

View File

@@ -142,11 +142,11 @@ public class WriteBlogPostActivity extends BriarActivity
private void storePost(String body) {
runOnDbThread(() -> {
long now = System.currentTimeMillis();
long timestamp = System.currentTimeMillis();
try {
LocalAuthor author = identityManager.getLocalAuthor();
BlogPost p = blogPostFactory
.createBlogPost(groupId, now, null, author, body);
.createBlogPost(groupId, timestamp, null, author, body);
blogManager.addLocalPost(p);
postPublished();
} catch (DbException | GeneralSecurityException

View File

@@ -61,6 +61,7 @@ import static android.support.v4.app.ActivityOptionsCompat.makeSceneTransitionAn
import static android.support.v4.view.ViewCompat.getTransitionName;
import static java.util.logging.Level.FINE;
import static java.util.logging.Level.WARNING;
import static org.briarproject.bramble.util.TimeUtils.now;
import static org.briarproject.briar.android.contact.ConversationActivity.CONTACT_ID;
@MethodsNotNullByDefault
@@ -194,7 +195,7 @@ public class ContactListFragment extends BaseFragment implements EventListener {
int revision = adapter.getRevision();
listener.runOnDbThread(() -> {
try {
long now = System.currentTimeMillis();
long start = now();
List<ContactListItem> contacts = new ArrayList<>();
for (Contact c : contactManager.getActiveContacts()) {
try {
@@ -208,7 +209,7 @@ public class ContactListFragment extends BaseFragment implements EventListener {
// Continue
}
}
long duration = System.currentTimeMillis() - now;
long duration = now() - start;
if (LOG.isLoggable(FINE))
LOG.fine("Full load took " + duration + " ms");
displayContacts(revision, contacts);

View File

@@ -107,6 +107,7 @@ import static android.widget.Toast.LENGTH_SHORT;
import static java.util.logging.Level.FINE;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING;
import static org.briarproject.bramble.util.TimeUtils.now;
import static org.briarproject.briar.android.activity.RequestCodes.REQUEST_INTRODUCTION;
import static org.briarproject.briar.android.settings.SettingsFragment.SETTINGS_NAMESPACE;
import static org.briarproject.briar.android.util.UiUtils.getAvatarTransitionName;
@@ -291,13 +292,13 @@ public class ConversationActivity extends BriarActivity
private void loadContactDetailsAndMessages() {
runOnDbThread(() -> {
try {
long now = System.currentTimeMillis();
long start = now();
if (contactName == null || contactAuthorId == null) {
Contact contact = contactManager.getContact(contactId);
contactName = contact.getAuthor().getName();
contactAuthorId = contact.getAuthor().getId();
}
long duration = System.currentTimeMillis() - now;
long duration = now() - start;
if (LOG.isLoggable(FINE))
LOG.fine("Loading contact took " + duration + " ms");
loadMessages();
@@ -341,7 +342,7 @@ public class ConversationActivity extends BriarActivity
int revision = adapter.getRevision();
runOnDbThread(() -> {
try {
long now = System.currentTimeMillis();
long start = now();
Collection<PrivateMessageHeader> headers =
messagingManager.getMessageHeaders(contactId);
Collection<IntroductionMessage> introductions =
@@ -358,7 +359,7 @@ public class ConversationActivity extends BriarActivity
invitations.addAll(forumInvitations);
invitations.addAll(blogInvitations);
invitations.addAll(groupInvitations);
long duration = System.currentTimeMillis() - now;
long duration = now() - start;
if (LOG.isLoggable(FINE))
LOG.fine("Loading messages took " + duration + " ms");
displayMessages(revision, headers, introductions, invitations);
@@ -439,9 +440,9 @@ public class ConversationActivity extends BriarActivity
private void loadMessageBody(MessageId m) {
runOnDbThread(() -> {
try {
long now = System.currentTimeMillis();
long start = now();
String body = messagingManager.getMessageBody(m);
long duration = System.currentTimeMillis() - now;
long duration = now() - start;
if (LOG.isLoggable(FINE))
LOG.fine("Loading body took " + duration + " ms");
displayMessageBody(m, body);
@@ -690,9 +691,9 @@ public class ConversationActivity extends BriarActivity
private void storeMessage(PrivateMessage m, String body) {
runOnDbThread(() -> {
try {
long now = System.currentTimeMillis();
long start = now();
messagingManager.addLocalMessage(m);
long duration = System.currentTimeMillis() - now;
long duration = now() - start;
if (LOG.isLoggable(FINE))
LOG.fine("Storing message took " + duration + " ms");
Message message = m.getMessage();
@@ -817,9 +818,9 @@ public class ConversationActivity extends BriarActivity
private void markMessageRead(GroupId g, MessageId m) {
runOnDbThread(() -> {
try {
long now = System.currentTimeMillis();
long start = now();
messagingManager.setReadFlag(g, m, true);
long duration = System.currentTimeMillis() - now;
long duration = now() - start;
if (LOG.isLoggable(FINE))
LOG.fine("Marking read took " + duration + " ms");
} catch (DbException e) {

View File

@@ -30,6 +30,7 @@ import static android.view.View.VISIBLE;
import static android.widget.Toast.LENGTH_LONG;
import static java.util.logging.Level.FINE;
import static java.util.logging.Level.WARNING;
import static org.briarproject.bramble.util.TimeUtils.now;
import static org.briarproject.briar.api.forum.ForumConstants.MAX_FORUM_NAME_LENGTH;
@MethodsNotNullByDefault
@@ -122,9 +123,9 @@ public class CreateForumActivity extends BriarActivity {
private void storeForum(String name) {
runOnDbThread(() -> {
try {
long now = System.currentTimeMillis();
long start = now();
Forum f = forumManager.addForum(name);
long duration = System.currentTimeMillis() - now;
long duration = now() - start;
if (LOG.isLoggable(FINE))
LOG.fine("Storing forum took " + duration + " ms");
displayForum(f);

View File

@@ -46,6 +46,7 @@ import javax.inject.Inject;
import static android.support.design.widget.Snackbar.LENGTH_INDEFINITE;
import static java.util.logging.Level.FINE;
import static java.util.logging.Level.WARNING;
import static org.briarproject.bramble.util.TimeUtils.now;
import static org.briarproject.briar.api.forum.ForumManager.CLIENT_ID;
@MethodsNotNullByDefault
@@ -157,7 +158,7 @@ public class ForumListFragment extends BaseEventFragment implements
int revision = adapter.getRevision();
listener.runOnDbThread(() -> {
try {
long now = System.currentTimeMillis();
long start = now();
Collection<ForumListItem> forums = new ArrayList<>();
for (Forum f : forumManager.getForums()) {
try {
@@ -168,7 +169,7 @@ public class ForumListFragment extends BaseEventFragment implements
// Continue
}
}
long duration = System.currentTimeMillis() - now;
long duration = now() - start;
if (LOG.isLoggable(FINE))
LOG.fine("Full load took " + duration + " ms");
displayForums(revision, forums);
@@ -194,9 +195,9 @@ public class ForumListFragment extends BaseEventFragment implements
private void loadAvailableForums() {
listener.runOnDbThread(() -> {
try {
long now = System.currentTimeMillis();
long start = now();
int available = forumSharingManager.getInvitations().size();
long duration = System.currentTimeMillis() - now;
long duration = now() - start;
if (LOG.isLoggable(FINE))
LOG.fine("Loading available took " + duration + " ms");
displayAvailableForums(available);

View File

@@ -18,6 +18,7 @@ import java.util.logging.Logger;
import javax.inject.Inject;
import static java.util.logging.Level.FINE;
import static org.briarproject.bramble.util.TimeUtils.now;
@NotNullByDefault
public class PasswordControllerImpl extends ConfigControllerImpl
@@ -86,9 +87,9 @@ public class PasswordControllerImpl extends ConfigControllerImpl
@CryptoExecutor
String encryptDatabaseKey(SecretKey key, String password) {
long now = System.currentTimeMillis();
long start = now();
byte[] encrypted = crypto.encryptWithPassword(key.getBytes(), password);
long duration = System.currentTimeMillis() - now;
long duration = now() - start;
if (LOG.isLoggable(FINE))
LOG.fine("Key derivation took " + duration + " ms");
return StringUtils.toHexString(encrypted);

View File

@@ -38,6 +38,7 @@ import javax.inject.Inject;
import static java.util.logging.Level.FINE;
import static java.util.logging.Level.WARNING;
import static org.briarproject.bramble.util.TimeUtils.now;
import static org.briarproject.briar.api.privategroup.PrivateGroupManager.CLIENT_ID;
@MethodsNotNullByDefault
@@ -147,7 +148,7 @@ class GroupListControllerImpl extends DbControllerImpl
ResultExceptionHandler<Collection<GroupItem>, DbException> handler) {
runOnDbThread(() -> {
try {
long now = System.currentTimeMillis();
long start = now();
Collection<PrivateGroup> groups =
groupManager.getPrivateGroups();
List<GroupItem> items = new ArrayList<>(groups.size());
@@ -161,7 +162,7 @@ class GroupListControllerImpl extends DbControllerImpl
// Continue
}
}
long duration = System.currentTimeMillis() - now;
long duration = now() - start;
if (LOG.isLoggable(FINE))
LOG.fine("Loading groups took " + duration + " ms");
handler.onResult(items);
@@ -176,9 +177,9 @@ class GroupListControllerImpl extends DbControllerImpl
public void removeGroup(GroupId g, ExceptionHandler<DbException> handler) {
runOnDbThread(() -> {
try {
long now = System.currentTimeMillis();
long start = now();
groupManager.removePrivateGroup(g);
long duration = System.currentTimeMillis() - now;
long duration = now() - start;
if (LOG.isLoggable(FINE))
LOG.fine("Removing group took " + duration + " ms");
} catch (DbException e) {

View File

@@ -65,6 +65,7 @@ import static java.util.logging.Level.WARNING;
import static org.briarproject.bramble.api.plugin.BluetoothConstants.PREF_BT_ENABLE;
import static org.briarproject.bramble.api.plugin.TorConstants.PREF_TOR_NETWORK;
import static org.briarproject.bramble.api.plugin.TorConstants.PREF_TOR_NETWORK_ALWAYS;
import static org.briarproject.bramble.util.TimeUtils.now;
import static org.briarproject.briar.android.TestingConstants.IS_DEBUG_BUILD;
import static org.briarproject.briar.android.activity.RequestCodes.REQUEST_RINGTONE;
import static org.briarproject.briar.android.navdrawer.NavDrawerActivity.INTENT_SIGN_OUT;
@@ -243,12 +244,12 @@ public class SettingsFragment extends PreferenceFragmentCompat
private void loadSettings() {
listener.runOnDbThread(() -> {
try {
long now = System.currentTimeMillis();
long start = now();
settings = settingsManager.getSettings(SETTINGS_NAMESPACE);
Settings btSettings = settingsManager.getSettings(BT_NAMESPACE);
Settings torSettings =
settingsManager.getSettings(TOR_NAMESPACE);
long duration = System.currentTimeMillis() - now;
long duration = now() - start;
if (LOG.isLoggable(FINE))
LOG.fine("Loading settings took " + duration + " ms");
boolean btSetting =
@@ -436,9 +437,9 @@ public class SettingsFragment extends PreferenceFragmentCompat
try {
Settings s = new Settings();
s.putInt(PREF_TOR_NETWORK, torSetting);
long now = System.currentTimeMillis();
long start = now();
settingsManager.mergeSettings(s, TOR_NAMESPACE);
long duration = System.currentTimeMillis() - now;
long duration = now() - start;
if (LOG.isLoggable(FINE))
LOG.fine("Merging settings took " + duration + " ms");
} catch (DbException e) {
@@ -452,9 +453,9 @@ public class SettingsFragment extends PreferenceFragmentCompat
try {
Settings s = new Settings();
s.putBoolean(PREF_BT_ENABLE, btSetting);
long now = System.currentTimeMillis();
long start = now();
settingsManager.mergeSettings(s, BT_NAMESPACE);
long duration = System.currentTimeMillis() - now;
long duration = now() - start;
if (LOG.isLoggable(FINE))
LOG.fine("Merging settings took " + duration + " ms");
} catch (DbException e) {
@@ -466,9 +467,9 @@ public class SettingsFragment extends PreferenceFragmentCompat
private void storeSettings(Settings settings) {
listener.runOnDbThread(() -> {
try {
long now = System.currentTimeMillis();
long start = now();
settingsManager.mergeSettings(settings, SETTINGS_NAMESPACE);
long duration = System.currentTimeMillis() - now;
long duration = now() - start;
if (LOG.isLoggable(FINE))
LOG.fine("Merging settings took " + duration + " ms");
} catch (DbException e) {

View File

@@ -26,6 +26,7 @@ import java.util.logging.Logger;
import static java.util.logging.Level.FINE;
import static java.util.logging.Level.WARNING;
import static org.briarproject.bramble.util.TimeUtils.now;
@MethodsNotNullByDefault
@ParametersNotNullByDefault
@@ -95,9 +96,9 @@ public abstract class InvitationControllerImpl<I extends InvitationItem>
ResultExceptionHandler<Collection<I>, DbException> handler) {
runOnDbThread(() -> {
try {
long now = System.currentTimeMillis();
long start = now();
Collection<I> invitations = new ArrayList<>(getInvitations());
long duration = System.currentTimeMillis() - now;
long duration = now() - start;
if (LOG.isLoggable(FINE))
LOG.fine("Loading invitations took " + duration + " ms");
handler.onResult(invitations);

View File

@@ -37,6 +37,7 @@ import java.util.logging.Logger;
import static java.util.logging.Level.FINE;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING;
import static org.briarproject.bramble.util.TimeUtils.now;
@MethodsNotNullByDefault
@ParametersNotNullByDefault
@@ -132,9 +133,9 @@ public abstract class ThreadListControllerImpl<G extends NamedGroup, I extends T
checkGroupId();
runOnDbThread(() -> {
try {
long now = System.currentTimeMillis();
long start = now();
G groupItem = loadNamedGroup();
long duration = System.currentTimeMillis() - now;
long duration = now() - start;
if (LOG.isLoggable(FINE))
LOG.fine("Loading group took " + duration + " ms");
handler.onResult(groupItem);
@@ -156,21 +157,21 @@ public abstract class ThreadListControllerImpl<G extends NamedGroup, I extends T
runOnDbThread(() -> {
try {
// Load headers
long now = System.currentTimeMillis();
long start = now();
Collection<H> headers = loadHeaders();
long duration = System.currentTimeMillis() - now;
long duration = now() - start;
if (LOG.isLoggable(FINE))
LOG.fine("Loading headers took " + duration + " ms");
// Load bodies into cache
now = System.currentTimeMillis();
start = now();
for (H header : headers) {
if (!bodyCache.containsKey(header.getId())) {
bodyCache.put(header.getId(),
loadMessageBody(header));
}
}
duration = System.currentTimeMillis() - now;
duration = now() - start;
if (LOG.isLoggable(FINE))
LOG.fine("Loading bodies took " + duration + " ms");
@@ -198,11 +199,11 @@ public abstract class ThreadListControllerImpl<G extends NamedGroup, I extends T
public void markItemsRead(Collection<I> items) {
runOnDbThread(() -> {
try {
long now = System.currentTimeMillis();
long start = now();
for (I i : items) {
markRead(i.getId());
}
long duration = System.currentTimeMillis() - now;
long duration = now() - start;
if (LOG.isLoggable(FINE))
LOG.fine("Marking read took " + duration + " ms");
} catch (DbException e) {
@@ -218,10 +219,10 @@ public abstract class ThreadListControllerImpl<G extends NamedGroup, I extends T
ResultExceptionHandler<I, DbException> resultHandler) {
runOnDbThread(() -> {
try {
long now = System.currentTimeMillis();
long start = now();
H header = addLocalMessage(msg);
bodyCache.put(msg.getMessage().getId(), body);
long duration = System.currentTimeMillis() - now;
long duration = now() - start;
if (LOG.isLoggable(FINE))
LOG.fine("Storing message took " + duration + " ms");
resultHandler.onResult(buildItem(header, body));
@@ -239,10 +240,10 @@ public abstract class ThreadListControllerImpl<G extends NamedGroup, I extends T
public void deleteNamedGroup(ExceptionHandler<DbException> handler) {
runOnDbThread(() -> {
try {
long now = System.currentTimeMillis();
long start = now();
G groupItem = loadNamedGroup();
deleteNamedGroup(groupItem);
long duration = System.currentTimeMillis() - now;
long duration = now() - start;
if (LOG.isLoggable(FINE))
LOG.fine("Removing group took " + duration + " ms");
} catch (DbException e) {