mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Speed up CreateHabitCommand and CreateRepetitionCommand
This commit is contained in:
@@ -19,16 +19,19 @@
|
|||||||
|
|
||||||
package org.isoron.uhabits.performance;
|
package org.isoron.uhabits.performance;
|
||||||
|
|
||||||
|
import androidx.test.ext.junit.runners.*;
|
||||||
import androidx.test.filters.*;
|
import androidx.test.filters.*;
|
||||||
import androidx.test.runner.*;
|
|
||||||
|
|
||||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
|
||||||
|
|
||||||
import org.isoron.uhabits.*;
|
import org.isoron.uhabits.*;
|
||||||
|
import org.isoron.uhabits.core.commands.*;
|
||||||
|
import org.isoron.uhabits.core.database.*;
|
||||||
import org.isoron.uhabits.core.models.*;
|
import org.isoron.uhabits.core.models.*;
|
||||||
|
import org.isoron.uhabits.core.models.sqlite.*;
|
||||||
import org.junit.*;
|
import org.junit.*;
|
||||||
import org.junit.runner.*;
|
import org.junit.runner.*;
|
||||||
|
|
||||||
|
import static org.isoron.uhabits.core.models.Timestamp.*;
|
||||||
|
|
||||||
@RunWith(AndroidJUnit4.class)
|
@RunWith(AndroidJUnit4.class)
|
||||||
@MediumTest
|
@MediumTest
|
||||||
public class PerformanceTest extends BaseAndroidTest
|
public class PerformanceTest extends BaseAndroidTest
|
||||||
@@ -42,7 +45,7 @@ public class PerformanceTest extends BaseAndroidTest
|
|||||||
habit = fixtures.createLongHabit();
|
habit = fixtures.createLongHabit();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(timeout = 4000)
|
@Test(timeout = 5000)
|
||||||
public void testRepeatedGetTodayValue()
|
public void testRepeatedGetTodayValue()
|
||||||
{
|
{
|
||||||
for (int i = 0; i < 100000; i++)
|
for (int i = 0; i < 100000; i++)
|
||||||
@@ -52,4 +55,32 @@ public class PerformanceTest extends BaseAndroidTest
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test(timeout = 1000)
|
||||||
|
public void benchmarkCreateHabitCommand()
|
||||||
|
{
|
||||||
|
Database db = ((SQLModelFactory) modelFactory).db;
|
||||||
|
db.beginTransaction();
|
||||||
|
for (int i = 0; i < 1_000; i++)
|
||||||
|
{
|
||||||
|
Habit model = modelFactory.buildHabit();
|
||||||
|
new CreateHabitCommand(modelFactory, habitList, model).execute();
|
||||||
|
}
|
||||||
|
db.setTransactionSuccessful();
|
||||||
|
db.endTransaction();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(timeout = 5000)
|
||||||
|
public void benchmarkCreateRepetitionCommand()
|
||||||
|
{
|
||||||
|
Database db = ((SQLModelFactory) modelFactory).db;
|
||||||
|
db.beginTransaction();
|
||||||
|
Habit habit = fixtures.createEmptyHabit();
|
||||||
|
for (int i = 0; i < 5_000; i++)
|
||||||
|
{
|
||||||
|
Timestamp timestamp = new Timestamp(i * DAY_LENGTH);
|
||||||
|
new CreateRepetitionCommand(habitList, habit, timestamp, 1).execute();
|
||||||
|
}
|
||||||
|
db.setTransactionSuccessful();
|
||||||
|
db.endTransaction();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -60,6 +60,7 @@ public class CreateRepetitionCommand extends Command
|
|||||||
RepetitionList reps = habit.getRepetitions();
|
RepetitionList reps = habit.getRepetitions();
|
||||||
previousValue = reps.getValue(timestamp);
|
previousValue = reps.getValue(timestamp);
|
||||||
reps.setValue(timestamp, value);
|
reps.setValue(timestamp, value);
|
||||||
|
habitList.resort();
|
||||||
}
|
}
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
|
|||||||
@@ -260,7 +260,7 @@ public abstract class HabitList implements Iterable<Habit>
|
|||||||
csv.close();
|
csv.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public abstract void resort();
|
||||||
|
|
||||||
public enum Order
|
public enum Order
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -79,8 +79,6 @@ public class MemoryHabitList extends HabitList
|
|||||||
if (id == null) habit.setId((long) list.size());
|
if (id == null) habit.setId((long) list.size());
|
||||||
list.addLast(habit);
|
list.addLast(habit);
|
||||||
resort();
|
resort();
|
||||||
|
|
||||||
getObservable().notifyListeners();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -133,7 +131,6 @@ public class MemoryHabitList extends HabitList
|
|||||||
this.primaryOrder = order;
|
this.primaryOrder = order;
|
||||||
this.comparator = getComposedComparatorByOrder(this.primaryOrder, this.secondaryOrder);
|
this.comparator = getComposedComparatorByOrder(this.primaryOrder, this.secondaryOrder);
|
||||||
resort();
|
resort();
|
||||||
getObservable().notifyListeners();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -142,7 +139,6 @@ public class MemoryHabitList extends HabitList
|
|||||||
this.secondaryOrder = order;
|
this.secondaryOrder = order;
|
||||||
this.comparator = getComposedComparatorByOrder(this.primaryOrder, this.secondaryOrder);
|
this.comparator = getComposedComparatorByOrder(this.primaryOrder, this.secondaryOrder);
|
||||||
resort();
|
resort();
|
||||||
getObservable().notifyListeners();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Comparator<Habit> getComposedComparatorByOrder(Order firstOrder, Order secondOrder)
|
private Comparator<Habit> getComposedComparatorByOrder(Order firstOrder, Order secondOrder)
|
||||||
@@ -265,7 +261,6 @@ public class MemoryHabitList extends HabitList
|
|||||||
public synchronized void update(List<Habit> habits)
|
public synchronized void update(List<Habit> habits)
|
||||||
{
|
{
|
||||||
resort();
|
resort();
|
||||||
getObservable().notifyListeners();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void throwIfHasParent()
|
private void throwIfHasParent()
|
||||||
@@ -284,8 +279,9 @@ public class MemoryHabitList extends HabitList
|
|||||||
resort();
|
resort();
|
||||||
}
|
}
|
||||||
|
|
||||||
private synchronized void resort()
|
public synchronized void resort()
|
||||||
{
|
{
|
||||||
if (comparator != null) Collections.sort(list, comparator);
|
if (comparator != null) Collections.sort(list, comparator);
|
||||||
|
getObservable().notifyListeners();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -66,12 +66,20 @@ public class SQLiteHabitList extends HabitList
|
|||||||
|
|
||||||
list.removeAll();
|
list.removeAll();
|
||||||
List<HabitRecord> records = repository.findAll("order by position");
|
List<HabitRecord> records = repository.findAll("order by position");
|
||||||
|
|
||||||
|
int expectedPosition = 0;
|
||||||
|
boolean shouldRebuildOrder = false;
|
||||||
for (HabitRecord rec : records)
|
for (HabitRecord rec : records)
|
||||||
{
|
{
|
||||||
|
if (rec.position != expectedPosition) shouldRebuildOrder = true;
|
||||||
|
expectedPosition++;
|
||||||
|
|
||||||
Habit h = modelFactory.buildHabit();
|
Habit h = modelFactory.buildHabit();
|
||||||
rec.copyTo(h);
|
rec.copyTo(h);
|
||||||
list.add(h);
|
list.add(h);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(shouldRebuildOrder) rebuildOrder();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -84,7 +92,6 @@ public class SQLiteHabitList extends HabitList
|
|||||||
record.copyFrom(habit);
|
record.copyFrom(habit);
|
||||||
repository.save(record);
|
repository.save(record);
|
||||||
habit.id = record.id;
|
habit.id = record.id;
|
||||||
rebuildOrder();
|
|
||||||
|
|
||||||
list.add(habit);
|
list.add(habit);
|
||||||
getObservable().notifyListeners();
|
getObservable().notifyListeners();
|
||||||
@@ -166,11 +173,17 @@ public class SQLiteHabitList extends HabitList
|
|||||||
private synchronized void rebuildOrder()
|
private synchronized void rebuildOrder()
|
||||||
{
|
{
|
||||||
List<HabitRecord> records = repository.findAll("order by position");
|
List<HabitRecord> records = repository.findAll("order by position");
|
||||||
repository.executeAsTransaction(() -> {
|
repository.executeAsTransaction(() ->
|
||||||
|
{
|
||||||
int pos = 0;
|
int pos = 0;
|
||||||
for (HabitRecord r : records) {
|
for (HabitRecord r : records)
|
||||||
r.position = pos++;
|
{
|
||||||
repository.save(r);
|
if (r.position != pos)
|
||||||
|
{
|
||||||
|
r.position = pos;
|
||||||
|
repository.save(r);
|
||||||
|
}
|
||||||
|
pos++;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -179,6 +192,9 @@ public class SQLiteHabitList extends HabitList
|
|||||||
public synchronized void remove(@NonNull Habit habit)
|
public synchronized void remove(@NonNull Habit habit)
|
||||||
{
|
{
|
||||||
loadRecords();
|
loadRecords();
|
||||||
|
|
||||||
|
reorder(habit, list.getByPosition(size() - 1));
|
||||||
|
|
||||||
list.remove(habit);
|
list.remove(habit);
|
||||||
|
|
||||||
HabitRecord record = repository.find(habit.getId());
|
HabitRecord record = repository.find(habit.getId());
|
||||||
@@ -189,7 +205,6 @@ public class SQLiteHabitList extends HabitList
|
|||||||
repository.remove(record);
|
repository.remove(record);
|
||||||
});
|
});
|
||||||
|
|
||||||
rebuildOrder();
|
|
||||||
getObservable().notifyListeners();
|
getObservable().notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -267,6 +282,13 @@ public class SQLiteHabitList extends HabitList
|
|||||||
getObservable().notifyListeners();
|
getObservable().notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void resort()
|
||||||
|
{
|
||||||
|
list.resort();
|
||||||
|
getObservable().notifyListeners();
|
||||||
|
}
|
||||||
|
|
||||||
public synchronized void reload()
|
public synchronized void reload()
|
||||||
{
|
{
|
||||||
loaded = false;
|
loaded = false;
|
||||||
|
|||||||
Reference in New Issue
Block a user