mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Improve performance of repeated getTodayValue calls
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -21,3 +21,4 @@ docs/
|
|||||||
gen/
|
gen/
|
||||||
local.properties
|
local.properties
|
||||||
crowdin.yaml
|
crowdin.yaml
|
||||||
|
local
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ import android.content.*;
|
|||||||
import android.os.*;
|
import android.os.*;
|
||||||
import android.support.annotation.*;
|
import android.support.annotation.*;
|
||||||
import android.support.test.*;
|
import android.support.test.*;
|
||||||
|
import android.util.*;
|
||||||
|
|
||||||
import org.isoron.uhabits.models.*;
|
import org.isoron.uhabits.models.*;
|
||||||
import org.isoron.uhabits.preferences.*;
|
import org.isoron.uhabits.preferences.*;
|
||||||
@@ -31,6 +32,7 @@ import org.isoron.uhabits.tasks.*;
|
|||||||
import org.isoron.uhabits.utils.*;
|
import org.isoron.uhabits.utils.*;
|
||||||
import org.junit.*;
|
import org.junit.*;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
|
|
||||||
@@ -132,4 +134,18 @@ public class BaseAndroidTest
|
|||||||
fail();
|
fail();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void startTracing()
|
||||||
|
{
|
||||||
|
File dir = FileUtils.getFilesDir(targetContext, "Profile");
|
||||||
|
assertNotNull(dir);
|
||||||
|
String tracePath = dir.getAbsolutePath() + "/performance.trace";
|
||||||
|
Log.d("PerformanceTest", String.format("Saving trace file to %s", tracePath));
|
||||||
|
Debug.startMethodTracingSampling(tracePath, 0, 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void stopTracing()
|
||||||
|
{
|
||||||
|
Debug.stopMethodTracing();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,50 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2017 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
* option) any later version.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.isoron.uhabits.performance;
|
||||||
|
|
||||||
|
import android.support.test.filters.*;
|
||||||
|
|
||||||
|
import org.isoron.uhabits.*;
|
||||||
|
import org.isoron.uhabits.models.*;
|
||||||
|
import org.junit.*;
|
||||||
|
|
||||||
|
@MediumTest
|
||||||
|
public class PerformanceTest extends BaseAndroidTest
|
||||||
|
{
|
||||||
|
private Habit habit;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setUp()
|
||||||
|
{
|
||||||
|
super.setUp();
|
||||||
|
habit = fixtures.createLongHabit();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(timeout = 1000)
|
||||||
|
public void testRepeatedGetTodayValue()
|
||||||
|
{
|
||||||
|
for (int i = 0; i < 100000; i++)
|
||||||
|
{
|
||||||
|
habit.getScores().getTodayValue();
|
||||||
|
habit.getCheckmarks().getTodayValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -108,7 +108,7 @@ public abstract class CheckmarkList
|
|||||||
*
|
*
|
||||||
* @return value of today's checkmark
|
* @return value of today's checkmark
|
||||||
*/
|
*/
|
||||||
public final int getTodayValue()
|
public int getTodayValue()
|
||||||
{
|
{
|
||||||
Checkmark today = getToday();
|
Checkmark today = getToday();
|
||||||
if (today != null) return today.getValue();
|
if (today != null) return today.getValue();
|
||||||
|
|||||||
@@ -24,7 +24,6 @@ import android.support.annotation.*;
|
|||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
|
|
||||||
import com.activeandroid.*;
|
import com.activeandroid.*;
|
||||||
import com.activeandroid.query.*;
|
|
||||||
|
|
||||||
import org.isoron.uhabits.models.*;
|
import org.isoron.uhabits.models.*;
|
||||||
import org.isoron.uhabits.models.sqlite.records.*;
|
import org.isoron.uhabits.models.sqlite.records.*;
|
||||||
@@ -38,38 +37,54 @@ import java.util.*;
|
|||||||
*/
|
*/
|
||||||
public class SQLiteCheckmarkList extends CheckmarkList
|
public class SQLiteCheckmarkList extends CheckmarkList
|
||||||
{
|
{
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private HabitRecord habitRecord;
|
private HabitRecord habitRecord;
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
private final SQLiteUtils<CheckmarkRecord> sqlite;
|
private final SQLiteUtils<CheckmarkRecord> sqlite;
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private Integer todayValue;
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
private final SQLiteStatement invalidateStatement;
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
private final SQLiteStatement addStatement;
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
private final SQLiteDatabase db;
|
||||||
|
|
||||||
|
private static final String ADD_QUERY =
|
||||||
|
"insert into Checkmarks(habit, timestamp, value) values (?,?,?)";
|
||||||
|
|
||||||
|
private static final String INVALIDATE_QUERY =
|
||||||
|
"delete from Checkmarks where habit = ? and timestamp >= ?";
|
||||||
|
|
||||||
public SQLiteCheckmarkList(Habit habit)
|
public SQLiteCheckmarkList(Habit habit)
|
||||||
{
|
{
|
||||||
super(habit);
|
super(habit);
|
||||||
sqlite = new SQLiteUtils<>(CheckmarkRecord.class);
|
sqlite = new SQLiteUtils<>(CheckmarkRecord.class);
|
||||||
|
|
||||||
|
db = Cache.openDatabase();
|
||||||
|
addStatement = db.compileStatement(ADD_QUERY);
|
||||||
|
invalidateStatement = db.compileStatement(INVALIDATE_QUERY);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void add(List<Checkmark> checkmarks)
|
public void add(List<Checkmark> checkmarks)
|
||||||
{
|
{
|
||||||
check(habit.getId());
|
check(habit.getId());
|
||||||
|
|
||||||
String query =
|
|
||||||
"insert into Checkmarks(habit, timestamp, value) values (?,?,?)";
|
|
||||||
|
|
||||||
SQLiteDatabase db = Cache.openDatabase();
|
|
||||||
db.beginTransaction();
|
db.beginTransaction();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
SQLiteStatement statement = db.compileStatement(query);
|
|
||||||
|
|
||||||
for (Checkmark c : checkmarks)
|
for (Checkmark c : checkmarks)
|
||||||
{
|
{
|
||||||
statement.bindLong(1, habit.getId());
|
addStatement.bindLong(1, habit.getId());
|
||||||
statement.bindLong(2, c.getTimestamp());
|
addStatement.bindLong(2, c.getTimestamp());
|
||||||
statement.bindLong(3, c.getValue());
|
addStatement.bindLong(3, c.getValue());
|
||||||
statement.execute();
|
addStatement.execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
db.setTransactionSuccessful();
|
db.setTransactionSuccessful();
|
||||||
@@ -115,12 +130,10 @@ public class SQLiteCheckmarkList extends CheckmarkList
|
|||||||
@Override
|
@Override
|
||||||
public void invalidateNewerThan(long timestamp)
|
public void invalidateNewerThan(long timestamp)
|
||||||
{
|
{
|
||||||
new Delete()
|
todayValue = null;
|
||||||
.from(CheckmarkRecord.class)
|
invalidateStatement.bindLong(1, habit.getId());
|
||||||
.where("habit = ?", habit.getId())
|
invalidateStatement.bindLong(2, timestamp);
|
||||||
.and("timestamp >= ?", timestamp)
|
invalidateStatement.execute();
|
||||||
.execute();
|
|
||||||
|
|
||||||
observable.notifyListeners();
|
observable.notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -179,4 +192,11 @@ public class SQLiteCheckmarkList extends CheckmarkList
|
|||||||
for (CheckmarkRecord r : records) checkmarks.add(r.toCheckmark());
|
for (CheckmarkRecord r : records) checkmarks.add(r.toCheckmark());
|
||||||
return checkmarks;
|
return checkmarks;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getTodayValue()
|
||||||
|
{
|
||||||
|
if(todayValue == null) todayValue = super.getTodayValue();
|
||||||
|
return todayValue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,12 +19,12 @@
|
|||||||
|
|
||||||
package org.isoron.uhabits.models.sqlite;
|
package org.isoron.uhabits.models.sqlite;
|
||||||
|
|
||||||
import android.database.DatabaseUtils;
|
import android.database.*;
|
||||||
import android.database.sqlite.SQLiteDatabase;
|
import android.database.sqlite.*;
|
||||||
import android.support.annotation.*;
|
import android.support.annotation.*;
|
||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
|
|
||||||
import com.activeandroid.Cache;
|
import com.activeandroid.*;
|
||||||
import com.activeandroid.query.*;
|
import com.activeandroid.query.*;
|
||||||
|
|
||||||
import org.isoron.uhabits.models.*;
|
import org.isoron.uhabits.models.*;
|
||||||
@@ -43,10 +43,16 @@ public class SQLiteRepetitionList extends RepetitionList
|
|||||||
@Nullable
|
@Nullable
|
||||||
private HabitRecord habitRecord;
|
private HabitRecord habitRecord;
|
||||||
|
|
||||||
|
private SQLiteStatement addStatement;
|
||||||
|
|
||||||
public SQLiteRepetitionList(@NonNull Habit habit)
|
public SQLiteRepetitionList(@NonNull Habit habit)
|
||||||
{
|
{
|
||||||
super(habit);
|
super(habit);
|
||||||
sqlite = new SQLiteUtils<>(RepetitionRecord.class);
|
sqlite = new SQLiteUtils<>(RepetitionRecord.class);
|
||||||
|
|
||||||
|
SQLiteDatabase db = Cache.openDatabase();
|
||||||
|
String addQuery = "insert into repetitions(habit, timestamp) values (?,?)";
|
||||||
|
addStatement = db.compileStatement(addQuery);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -61,11 +67,9 @@ public class SQLiteRepetitionList extends RepetitionList
|
|||||||
public void add(Repetition rep)
|
public void add(Repetition rep)
|
||||||
{
|
{
|
||||||
check(habit.getId());
|
check(habit.getId());
|
||||||
|
addStatement.bindLong(1, habit.getId());
|
||||||
RepetitionRecord record = new RepetitionRecord();
|
addStatement.bindLong(2, rep.getTimestamp());
|
||||||
record.copyFrom(rep);
|
addStatement.execute();
|
||||||
record.habit = habitRecord;
|
|
||||||
record.save();
|
|
||||||
observable.notifyListeners();
|
observable.notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -24,7 +24,6 @@ import android.support.annotation.*;
|
|||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
|
|
||||||
import com.activeandroid.*;
|
import com.activeandroid.*;
|
||||||
import com.activeandroid.query.*;
|
|
||||||
|
|
||||||
import org.isoron.uhabits.models.*;
|
import org.isoron.uhabits.models.*;
|
||||||
import org.isoron.uhabits.models.sqlite.records.*;
|
import org.isoron.uhabits.models.sqlite.records.*;
|
||||||
@@ -37,12 +36,30 @@ import java.util.*;
|
|||||||
*/
|
*/
|
||||||
public class SQLiteScoreList extends ScoreList
|
public class SQLiteScoreList extends ScoreList
|
||||||
{
|
{
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private HabitRecord habitRecord;
|
private HabitRecord habitRecord;
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
private final SQLiteUtils<ScoreRecord> sqlite;
|
private final SQLiteUtils<ScoreRecord> sqlite;
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private Integer todayValue;
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
private final SQLiteStatement invalidateStatement;
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
private final SQLiteStatement addStatement;
|
||||||
|
|
||||||
|
public static final String ADD_QUERY =
|
||||||
|
"insert into Score(habit, timestamp, score) values (?,?,?)";
|
||||||
|
|
||||||
|
public static final String INVALIDATE_QUERY =
|
||||||
|
"delete from Score where habit = ? " + "and timestamp >= ?";
|
||||||
|
|
||||||
|
private final SQLiteDatabase db;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a new ScoreList associated with the given habit.
|
* Constructs a new ScoreList associated with the given habit.
|
||||||
*
|
*
|
||||||
@@ -52,28 +69,25 @@ public class SQLiteScoreList extends ScoreList
|
|||||||
{
|
{
|
||||||
super(habit);
|
super(habit);
|
||||||
sqlite = new SQLiteUtils<>(ScoreRecord.class);
|
sqlite = new SQLiteUtils<>(ScoreRecord.class);
|
||||||
|
|
||||||
|
db = Cache.openDatabase();
|
||||||
|
addStatement = db.compileStatement(ADD_QUERY);
|
||||||
|
invalidateStatement = db.compileStatement(INVALIDATE_QUERY);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void add(List<Score> scores)
|
public void add(List<Score> scores)
|
||||||
{
|
{
|
||||||
check(habit.getId());
|
check(habit.getId());
|
||||||
String query =
|
|
||||||
"insert into Score(habit, timestamp, score) values (?,?,?)";
|
|
||||||
|
|
||||||
SQLiteDatabase db = Cache.openDatabase();
|
|
||||||
db.beginTransaction();
|
db.beginTransaction();
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
SQLiteStatement statement = db.compileStatement(query);
|
|
||||||
|
|
||||||
for (Score s : scores)
|
for (Score s : scores)
|
||||||
{
|
{
|
||||||
statement.bindLong(1, habit.getId());
|
addStatement.bindLong(1, habit.getId());
|
||||||
statement.bindLong(2, s.getTimestamp());
|
addStatement.bindLong(2, s.getTimestamp());
|
||||||
statement.bindLong(3, s.getValue());
|
addStatement.bindLong(3, s.getValue());
|
||||||
statement.execute();
|
addStatement.execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
db.setTransactionSuccessful();
|
db.setTransactionSuccessful();
|
||||||
@@ -126,12 +140,10 @@ public class SQLiteScoreList extends ScoreList
|
|||||||
@Override
|
@Override
|
||||||
public void invalidateNewerThan(long timestamp)
|
public void invalidateNewerThan(long timestamp)
|
||||||
{
|
{
|
||||||
new Delete()
|
todayValue = null;
|
||||||
.from(ScoreRecord.class)
|
invalidateStatement.bindLong(1, habit.getId());
|
||||||
.where("habit = ?", habit.getId())
|
invalidateStatement.bindLong(2, timestamp);
|
||||||
.and("timestamp >= ?", timestamp)
|
invalidateStatement.execute();
|
||||||
.execute();
|
|
||||||
|
|
||||||
getObservable().notifyListeners();
|
getObservable().notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -204,4 +216,11 @@ public class SQLiteScoreList extends ScoreList
|
|||||||
for (ScoreRecord r : records) scores.add(r.toScore());
|
for (ScoreRecord r : records) scores.add(r.toScore());
|
||||||
return scores;
|
return scores;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getTodayValue()
|
||||||
|
{
|
||||||
|
if (todayValue == null) todayValue = super.getTodayValue();
|
||||||
|
return todayValue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,10 +19,11 @@
|
|||||||
|
|
||||||
package org.isoron.uhabits.models.sqlite;
|
package org.isoron.uhabits.models.sqlite;
|
||||||
|
|
||||||
|
import android.database.sqlite.*;
|
||||||
import android.support.annotation.*;
|
import android.support.annotation.*;
|
||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
|
|
||||||
import com.activeandroid.query.*;
|
import com.activeandroid.*;
|
||||||
|
|
||||||
import org.isoron.uhabits.models.*;
|
import org.isoron.uhabits.models.*;
|
||||||
import org.isoron.uhabits.models.sqlite.records.*;
|
import org.isoron.uhabits.models.sqlite.records.*;
|
||||||
@@ -41,10 +42,17 @@ public class SQLiteStreakList extends StreakList
|
|||||||
@NonNull
|
@NonNull
|
||||||
private final SQLiteUtils<StreakRecord> sqlite;
|
private final SQLiteUtils<StreakRecord> sqlite;
|
||||||
|
|
||||||
|
private final SQLiteStatement invalidateStatement;
|
||||||
|
|
||||||
public SQLiteStreakList(Habit habit)
|
public SQLiteStreakList(Habit habit)
|
||||||
{
|
{
|
||||||
super(habit);
|
super(habit);
|
||||||
sqlite = new SQLiteUtils<>(StreakRecord.class);
|
sqlite = new SQLiteUtils<>(StreakRecord.class);
|
||||||
|
|
||||||
|
SQLiteDatabase db = Cache.openDatabase();
|
||||||
|
String invalidateQuery = "delete from Streak where habit = ? " +
|
||||||
|
"and end >= ?";
|
||||||
|
invalidateStatement = db.compileStatement(invalidateQuery);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -73,12 +81,9 @@ public class SQLiteStreakList extends StreakList
|
|||||||
@Override
|
@Override
|
||||||
public void invalidateNewerThan(long timestamp)
|
public void invalidateNewerThan(long timestamp)
|
||||||
{
|
{
|
||||||
new Delete()
|
invalidateStatement.bindLong(1, habit.getId());
|
||||||
.from(StreakRecord.class)
|
invalidateStatement.bindLong(2, timestamp - DateUtils.millisecondsInOneDay);
|
||||||
.where("habit = ?", habit.getId())
|
invalidateStatement.execute();
|
||||||
.and("end >= ?", timestamp - DateUtils.millisecondsInOneDay)
|
|
||||||
.execute();
|
|
||||||
|
|
||||||
observable.notifyListeners();
|
observable.notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user