From 9ca55c3a96ddb942666fedf4d8bb6e9dc36ead67 Mon Sep 17 00:00:00 2001 From: xcesco Date: Sat, 25 Nov 2017 11:18:56 +0100 Subject: [PATCH 1/2] add Kripton test --- Kripton/build.gradle | 32 +++ .../performance/room/PerfTestKripton.java | 204 ++++++++++++++++++ Kripton/src/main/AndroidManifest.xml | 9 + .../performance/kripton/AppDataSource.java | 9 + .../kripton/IndexedStringEntity.java | 30 +++ .../kripton/IndexedStringEntityDao.java | 22 ++ .../kripton/SimpleEntityNotNull.java | 104 +++++++++ .../kripton/SimpleEntityNotNullDao.java | 26 +++ settings.gradle | 1 + 9 files changed, 437 insertions(+) create mode 100644 Kripton/build.gradle create mode 100644 Kripton/src/androidTest/java/de/greenrobot/performance/room/PerfTestKripton.java create mode 100644 Kripton/src/main/AndroidManifest.xml create mode 100644 Kripton/src/main/java/de/greenrobot/performance/kripton/AppDataSource.java create mode 100644 Kripton/src/main/java/de/greenrobot/performance/kripton/IndexedStringEntity.java create mode 100644 Kripton/src/main/java/de/greenrobot/performance/kripton/IndexedStringEntityDao.java create mode 100644 Kripton/src/main/java/de/greenrobot/performance/kripton/SimpleEntityNotNull.java create mode 100644 Kripton/src/main/java/de/greenrobot/performance/kripton/SimpleEntityNotNullDao.java diff --git a/Kripton/build.gradle b/Kripton/build.gradle new file mode 100644 index 0000000..31f33ce --- /dev/null +++ b/Kripton/build.gradle @@ -0,0 +1,32 @@ +buildscript { + dependencies { + classpath dep.androidPlugin + } +} + +apply plugin: 'com.android.application' + +android { + buildToolsVersion rootProject.ext.buildToolsVersion + compileSdkVersion rootProject.ext.compileSdkVersion + + defaultConfig { + applicationId "de.greenrobot.performance.kripton" + minSdkVersion rootProject.ext.targetSdkVersion + targetSdkVersion rootProject.ext.targetSdkVersion + + testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" + } + + lintOptions { + // some Room classes are restricted to internal use within library + // but are used in generated code, ignore warning + disable 'RestrictedApi' + } +} + +dependencies { + androidTestCompile project(':Common') + compile "com.abubusoft:kripton-android-library:3.0.1" + annotationProcessor "com.abubusoft:kripton-processor:3.0.1" +} diff --git a/Kripton/src/androidTest/java/de/greenrobot/performance/room/PerfTestKripton.java b/Kripton/src/androidTest/java/de/greenrobot/performance/room/PerfTestKripton.java new file mode 100644 index 0000000..e464213 --- /dev/null +++ b/Kripton/src/androidTest/java/de/greenrobot/performance/room/PerfTestKripton.java @@ -0,0 +1,204 @@ +package de.greenrobot.performance.room; + +import android.database.sqlite.SQLiteDatabase; + +import com.abubusoft.kripton.android.KriptonLibrary; +import com.abubusoft.kripton.android.sqlite.DataSourceOptions; +import com.abubusoft.kripton.android.sqlite.DatabaseLifecycleHandler; +import com.abubusoft.kripton.android.sqlite.TransactionResult; +import com.abubusoft.kripton.common.One; + +import java.util.ArrayList; +import java.util.List; + +import de.greenrobot.performance.BasePerfTestCase; +import de.greenrobot.performance.Benchmark; +import de.greenrobot.performance.StringGenerator; +import de.greenrobot.performance.kripton.BindAppDaoFactory; +import de.greenrobot.performance.kripton.BindAppDataSource; +import de.greenrobot.performance.kripton.IndexedStringEntity; +import de.greenrobot.performance.kripton.IndexedStringEntityDaoImpl; +import de.greenrobot.performance.kripton.SimpleEntityNotNull; +import de.greenrobot.performance.kripton.SimpleEntityNotNullDaoImpl; + +/** + * https://developer.android.com/topic/libraries/architecture/room.html + */ +public class PerfTestKripton extends BasePerfTestCase { + + private static final String DB_NAME = "kripton-test.db"; + + private BindAppDataSource db; + private IndexedStringEntityDaoImpl indexedStringEntityDao; + private SimpleEntityNotNullDaoImpl simpleEntityNotNullDao; + + @Override + public void setUp() throws Exception { + KriptonLibrary.init(getTargetContext()); + + db = BindAppDataSource.build(DataSourceOptions.builder().log(false).build()); + + indexedStringEntityDao = db.getIndexedStringEntityDao(); + simpleEntityNotNullDao = db.getSimpleEntityNotNullDao(); + + db.openWritableDatabase(); + } + + @Override + public void tearDown() throws Exception { + db.close(); + getTargetContext().deleteDatabase(DB_NAME); + } + + @Override + protected void doOneByOneCrudRun(final int count) throws Exception { + final List list = new ArrayList<>(); + for (long i = 0; i < count; i++) { + list.add(createSimpleEntityNotNull(i)); + } + + startClock(); + // final reference to entity to save + for (int i = 0; i < count; i++) { + simpleEntityNotNullDao.insert(list.get(i)); + } + stopClock(Benchmark.Type.ONE_BY_ONE_CREATE); + + startClock(); + for (int i = 0; i < count; i++) { + simpleEntityNotNullDao.update(list.get(i)); + } + stopClock(Benchmark.Type.ONE_BY_ONE_UPDATE); + + deleteAll(); + } + + @SuppressWarnings("ResultOfMethodCallIgnored") + @Override + protected void doBatchCrudRun(final int count) throws Exception { + final List list = new ArrayList<>(); + for (long i = 0; i < count; i++) { + list.add(createSimpleEntityNotNull(i)); + } + + startClock(); + db.execute( new BindAppDataSource.Transaction() { + @Override + public TransactionResult onExecute(BindAppDaoFactory daoFactory) { + SimpleEntityNotNullDaoImpl dao = daoFactory.getSimpleEntityNotNullDao(); + + for (int i=0; i reloaded = simpleEntityNotNullDao.getAll(); + stopClock(Benchmark.Type.BATCH_READ); + + startClock(); + for (int i = 0; i < reloaded.size(); i++) { + SimpleEntityNotNull entity = reloaded.get(i); + entity.getId(); + entity.getSimpleBoolean(); + entity.getSimpleByte(); + entity.getSimpleShort(); + entity.getSimpleInt(); + entity.getSimpleLong(); + entity.getSimpleFloat(); + entity.getSimpleDouble(); + entity.getSimpleString(); + entity.getSimpleByteArray(); + } + stopClock(Benchmark.Type.BATCH_ACCESS); + + startClock(); + deleteAll(); + stopClock(Benchmark.Type.BATCH_DELETE); + + } + + @Override + protected void doIndexedStringEntityQueries() throws Exception { + for (int i = 0; i < RUNS; i++) { + log("----Run " + (i + 1) + " of " + RUNS); + indexedStringEntityQueriesRun(getBatchSize()); + } + } + + @SuppressWarnings("ResultOfMethodCallIgnored") + private void indexedStringEntityQueriesRun(final int count) { + // create entities + final List entities = new ArrayList<>(count); + final String[] fixedRandomStrings = StringGenerator.createFixedRandomStrings(count); + for (int i = 0; i < count; i++) { + IndexedStringEntity entity = new IndexedStringEntity(); + entity.setId((long) i); + entity.setIndexedString(fixedRandomStrings[i]); + entities.add(entity); + } + log("Built entities."); + + // query for entities by indexed string at random + int[] randomIndices = StringGenerator.getFixedRandomIndices(getQueryCount(), count - 1); + + startClock(); + for (int i = 0; i < getQueryCount(); i++) { + int nextIndex = randomIndices[i]; + + List result = indexedStringEntityDao + .withIndexedString(fixedRandomStrings[nextIndex]); + for (int j = 0, resultSize = result.size(); j < resultSize; j++) { + IndexedStringEntity entity = result.get(j); + entity.getId(); + entity.getIndexedString(); + } + } + stopClock(Benchmark.Type.QUERY_INDEXED); + + // delete all entities + indexedStringEntityDao.deleteAll(); + log("Deleted all entities."); + } + + private static SimpleEntityNotNull createSimpleEntityNotNull(Long key) { + if (key == null) { + return null; + } + SimpleEntityNotNull entity = new SimpleEntityNotNull(); + entity.setId(key); + entity.setSimpleBoolean(true); + entity.setSimpleByte(Byte.MAX_VALUE); + entity.setSimpleShort(Short.MAX_VALUE); + entity.setSimpleInt(Integer.MAX_VALUE); + entity.setSimpleLong(Long.MAX_VALUE); + entity.setSimpleFloat(Float.MAX_VALUE); + entity.setSimpleDouble(Double.MAX_VALUE); + entity.setSimpleString("greenrobot greenDAO"); + byte[] bytes = {42, -17, 23, 0, 127, -128}; + entity.setSimpleByteArray(bytes); + return entity; + } + + private void deleteAll() { + simpleEntityNotNullDao.deleteAll(); + } +} diff --git a/Kripton/src/main/AndroidManifest.xml b/Kripton/src/main/AndroidManifest.xml new file mode 100644 index 0000000..8d403c5 --- /dev/null +++ b/Kripton/src/main/AndroidManifest.xml @@ -0,0 +1,9 @@ + + + + + + + + \ No newline at end of file diff --git a/Kripton/src/main/java/de/greenrobot/performance/kripton/AppDataSource.java b/Kripton/src/main/java/de/greenrobot/performance/kripton/AppDataSource.java new file mode 100644 index 0000000..444079d --- /dev/null +++ b/Kripton/src/main/java/de/greenrobot/performance/kripton/AppDataSource.java @@ -0,0 +1,9 @@ +package de.greenrobot.performance.kripton; + +import com.abubusoft.kripton.android.annotation.BindDataSource; + +@BindDataSource(daoSet = {IndexedStringEntityDao.class, SimpleEntityNotNullDao.class}, fileName = "kripton-test.db", version = 1, log = false) +public interface AppDataSource { + + +} diff --git a/Kripton/src/main/java/de/greenrobot/performance/kripton/IndexedStringEntity.java b/Kripton/src/main/java/de/greenrobot/performance/kripton/IndexedStringEntity.java new file mode 100644 index 0000000..cf8bf99 --- /dev/null +++ b/Kripton/src/main/java/de/greenrobot/performance/kripton/IndexedStringEntity.java @@ -0,0 +1,30 @@ +package de.greenrobot.performance.kripton; + +import com.abubusoft.kripton.android.annotation.BindTable; + +/** + * Simple entity with a string property that is indexed. + */ +@BindTable(indexes = "indexedString") +public class IndexedStringEntity { + + private long id; + + private String indexedString; + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public String getIndexedString() { + return indexedString; + } + + public void setIndexedString(String indexedString) { + this.indexedString = indexedString; + } +} diff --git a/Kripton/src/main/java/de/greenrobot/performance/kripton/IndexedStringEntityDao.java b/Kripton/src/main/java/de/greenrobot/performance/kripton/IndexedStringEntityDao.java new file mode 100644 index 0000000..d66fa5d --- /dev/null +++ b/Kripton/src/main/java/de/greenrobot/performance/kripton/IndexedStringEntityDao.java @@ -0,0 +1,22 @@ +package de.greenrobot.performance.kripton; + +import com.abubusoft.kripton.android.annotation.BindDao; +import com.abubusoft.kripton.android.annotation.BindSqlDelete; +import com.abubusoft.kripton.android.annotation.BindSqlInsert; +import com.abubusoft.kripton.android.annotation.BindSqlSelect; + +import java.util.List; + +@BindDao(IndexedStringEntity.class) +public interface IndexedStringEntityDao { + + @BindSqlInsert + void insert(IndexedStringEntity entity); + + @BindSqlSelect(jql="SELECT * FROM IndexedStringEntity WHERE indexedString = ${value}") + List withIndexedString(String value); + + @BindSqlDelete(jql="DELETE FROM IndexedStringEntity") + void deleteAll(); + +} diff --git a/Kripton/src/main/java/de/greenrobot/performance/kripton/SimpleEntityNotNull.java b/Kripton/src/main/java/de/greenrobot/performance/kripton/SimpleEntityNotNull.java new file mode 100644 index 0000000..93c9493 --- /dev/null +++ b/Kripton/src/main/java/de/greenrobot/performance/kripton/SimpleEntityNotNull.java @@ -0,0 +1,104 @@ +package de.greenrobot.performance.kripton; + +import com.abubusoft.kripton.android.annotation.BindTable; + +/** + * Simple entity for performance testing. + */ +@BindTable +public class SimpleEntityNotNull { + + private long id; + + private boolean simpleBoolean; + private byte simpleByte; + private short simpleShort; + private int simpleInt; + private long simpleLong; + private float simpleFloat; + private double simpleDouble; + /** Not-null value. */ + private String simpleString; + /** Not-null value. */ + private byte[] simpleByteArray; + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public boolean getSimpleBoolean() { + return simpleBoolean; + } + + public void setSimpleBoolean(boolean simpleBoolean) { + this.simpleBoolean = simpleBoolean; + } + + public byte getSimpleByte() { + return simpleByte; + } + + public void setSimpleByte(byte simpleByte) { + this.simpleByte = simpleByte; + } + + public short getSimpleShort() { + return simpleShort; + } + + public void setSimpleShort(short simpleShort) { + this.simpleShort = simpleShort; + } + + public int getSimpleInt() { + return simpleInt; + } + + public void setSimpleInt(int simpleInt) { + this.simpleInt = simpleInt; + } + + public long getSimpleLong() { + return simpleLong; + } + + public void setSimpleLong(long simpleLong) { + this.simpleLong = simpleLong; + } + + public float getSimpleFloat() { + return simpleFloat; + } + + public void setSimpleFloat(float simpleFloat) { + this.simpleFloat = simpleFloat; + } + + public double getSimpleDouble() { + return simpleDouble; + } + + public void setSimpleDouble(double simpleDouble) { + this.simpleDouble = simpleDouble; + } + + public String getSimpleString() { + return simpleString; + } + + public void setSimpleString(String simpleString) { + this.simpleString = simpleString; + } + + public byte[] getSimpleByteArray() { + return simpleByteArray; + } + + public void setSimpleByteArray(byte[] simpleByteArray) { + this.simpleByteArray = simpleByteArray; + } +} diff --git a/Kripton/src/main/java/de/greenrobot/performance/kripton/SimpleEntityNotNullDao.java b/Kripton/src/main/java/de/greenrobot/performance/kripton/SimpleEntityNotNullDao.java new file mode 100644 index 0000000..a963107 --- /dev/null +++ b/Kripton/src/main/java/de/greenrobot/performance/kripton/SimpleEntityNotNullDao.java @@ -0,0 +1,26 @@ +package de.greenrobot.performance.kripton; + +import com.abubusoft.kripton.android.annotation.BindDao; +import com.abubusoft.kripton.android.annotation.BindSqlDelete; +import com.abubusoft.kripton.android.annotation.BindSqlInsert; +import com.abubusoft.kripton.android.annotation.BindSqlSelect; +import com.abubusoft.kripton.android.annotation.BindSqlUpdate; + +import java.util.List; + +@BindDao(SimpleEntityNotNull.class) +public interface SimpleEntityNotNullDao { + + @BindSqlInsert + void insert(SimpleEntityNotNull entity); + + @BindSqlUpdate(where="id=${entity.id}") + void update(SimpleEntityNotNull entity); + + @BindSqlSelect + List getAll(); + + @BindSqlDelete + void deleteAll(); + +} diff --git a/settings.gradle b/settings.gradle index 4e08dfe..05e6867 100644 --- a/settings.gradle +++ b/settings.gradle @@ -13,3 +13,4 @@ include ':requery' include ':SQLDelight' include ':Sqlite' include ':SquiDB' +include ':Kripton' From 9f0637bb56f85bacfbf6889e71aee47b3059be94 Mon Sep 17 00:00:00 2001 From: xcesco Date: Mon, 27 Nov 2017 19:33:20 +0100 Subject: [PATCH 2/2] upgrade benchmark to kritpon version 3.0.2 --- Kripton/build.gradle | 4 ++-- .../java/de/greenrobot/performance/room/PerfTestKripton.java | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Kripton/build.gradle b/Kripton/build.gradle index 31f33ce..980d5e4 100644 --- a/Kripton/build.gradle +++ b/Kripton/build.gradle @@ -27,6 +27,6 @@ android { dependencies { androidTestCompile project(':Common') - compile "com.abubusoft:kripton-android-library:3.0.1" - annotationProcessor "com.abubusoft:kripton-processor:3.0.1" + compile "com.abubusoft:kripton-android-library:3.0.2" + annotationProcessor "com.abubusoft:kripton-processor:3.0.2" } diff --git a/Kripton/src/androidTest/java/de/greenrobot/performance/room/PerfTestKripton.java b/Kripton/src/androidTest/java/de/greenrobot/performance/room/PerfTestKripton.java index e464213..f9b5e35 100644 --- a/Kripton/src/androidTest/java/de/greenrobot/performance/room/PerfTestKripton.java +++ b/Kripton/src/androidTest/java/de/greenrobot/performance/room/PerfTestKripton.java @@ -121,6 +121,7 @@ public TransactionResult onExecute(BindAppDaoFactory daoFactory) { entity.getSimpleBoolean(); entity.getSimpleByte(); entity.getSimpleShort(); + entity.getSimpleInt(); entity.getSimpleLong(); entity.getSimpleFloat();