|
| 1 | +package de.greenrobot.performance.squidb; |
| 2 | + |
| 3 | +import com.yahoo.squidb.data.SquidCursor; |
| 4 | +import com.yahoo.squidb.sql.Query; |
| 5 | +import de.greenrobot.performance.BasePerfTestCase; |
| 6 | +import de.greenrobot.performance.StringGenerator; |
| 7 | +import de.greenrobot.performance.Tools; |
| 8 | +import java.sql.SQLException; |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +/** |
| 13 | + * https://github.com/yahoo/squidb/wiki |
| 14 | + */ |
| 15 | +public class PerfTestSquiDB extends BasePerfTestCase { |
| 16 | + |
| 17 | + @Override |
| 18 | + protected String getLogTag() { |
| 19 | + return getClass().getSimpleName(); |
| 20 | + } |
| 21 | + |
| 22 | + @Override |
| 23 | + protected void tearDown() throws Exception { |
| 24 | + getApplication().deleteDatabase(MySquidDatabase.DATABASE_NAME); |
| 25 | + |
| 26 | + super.tearDown(); |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + protected void doIndexedStringEntityQueries() throws Exception { |
| 31 | + // set up database |
| 32 | + MySquidDatabase database = new MySquidDatabase(getApplication()); |
| 33 | + log("Set up database."); |
| 34 | + |
| 35 | + for (int i = 0; i < RUNS; i++) { |
| 36 | + log("----Run " + (i + 1) + " of " + RUNS); |
| 37 | + indexedStringEntityQueriesRun(database, getBatchSize()); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + private void indexedStringEntityQueriesRun(MySquidDatabase database, int count) { |
| 42 | + // create entities |
| 43 | + List<IndexedStringEntity> entities = new ArrayList<>(count); |
| 44 | + String[] fixedRandomStrings = StringGenerator.createFixedRandomStrings(count); |
| 45 | + for (int i = 0; i < count; i++) { |
| 46 | + IndexedStringEntity entity = new IndexedStringEntity(); |
| 47 | + // start with id 1 as 0 is treated as NO_ID |
| 48 | + entity.setId((long) i + 1); |
| 49 | + entity.setIndexedString(fixedRandomStrings[i]); |
| 50 | + entities.add(entity); |
| 51 | + } |
| 52 | + log("Built entities."); |
| 53 | + |
| 54 | + // insert entities |
| 55 | + database.beginTransaction(); |
| 56 | + try { |
| 57 | + for (int i = 0; i < count; i++) { |
| 58 | + database.persistWithId(entities.get(i)); |
| 59 | + } |
| 60 | + database.setTransactionSuccessful(); |
| 61 | + } finally { |
| 62 | + database.endTransaction(); |
| 63 | + } |
| 64 | + log("Inserted entities."); |
| 65 | + |
| 66 | + // query for entities by indexed string at random |
| 67 | + int[] randomIndices = StringGenerator.getFixedRandomIndices(getQueryCount(), count - 1); |
| 68 | + |
| 69 | + startClock(); |
| 70 | + for (int i = 0; i < getQueryCount(); i++) { |
| 71 | + int nextIndex = randomIndices[i]; |
| 72 | + //noinspection unused |
| 73 | + IndexedStringEntity indexedStringEntity = database.fetchByCriterion( |
| 74 | + IndexedStringEntity.class, |
| 75 | + IndexedStringEntity.INDEXED_STRING.eq(fixedRandomStrings[nextIndex])); |
| 76 | + } |
| 77 | + stopClock(Tools.LogMessage.QUERY_INDEXED); |
| 78 | + |
| 79 | + // delete all entities |
| 80 | + database.deleteAll(IndexedStringEntity.class); |
| 81 | + log("Deleted all entities."); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + protected void doOneByOneAndBatchCrud() throws Exception { |
| 86 | + // set up database |
| 87 | + MySquidDatabase database = new MySquidDatabase(getApplication()); |
| 88 | + log("Set up database."); |
| 89 | + |
| 90 | + // set up database |
| 91 | + for (int i = 0; i < RUNS; i++) { |
| 92 | + log("----Run " + (i + 1) + " of " + RUNS); |
| 93 | + oneByOneCrudRun(database, getBatchSize() / 10); |
| 94 | + batchCrudRun(database, getBatchSize()); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private void oneByOneCrudRun(MySquidDatabase database, int count) throws SQLException { |
| 99 | + final List<SimpleEntityNotNull> list = new ArrayList<>(); |
| 100 | + for (int i = 0; i < count; i++) { |
| 101 | + list.add(createEntity((long) i)); |
| 102 | + } |
| 103 | + |
| 104 | + startClock(); |
| 105 | + for (int i = 0; i < count; i++) { |
| 106 | + database.persistWithId(list.get(i)); |
| 107 | + } |
| 108 | + stopClock(Tools.LogMessage.ONE_BY_ONE_CREATE); |
| 109 | + |
| 110 | + // re-set values to set entity as modified |
| 111 | + for (int i = 0; i < list.size(); i++) { |
| 112 | + updateEntity(list.get(i)); |
| 113 | + } |
| 114 | + |
| 115 | + startClock(); |
| 116 | + for (int i = 0; i < count; i++) { |
| 117 | + database.persist(list.get(i)); |
| 118 | + } |
| 119 | + stopClock(Tools.LogMessage.ONE_BY_ONE_UPDATE); |
| 120 | + |
| 121 | + deleteAll(database); |
| 122 | + } |
| 123 | + |
| 124 | + @SuppressWarnings("unused") |
| 125 | + private void batchCrudRun(MySquidDatabase database, int count) throws Exception { |
| 126 | + final List<SimpleEntityNotNull> list = new ArrayList<>(); |
| 127 | + for (int i = 0; i < count; i++) { |
| 128 | + list.add(createEntity((long) i)); |
| 129 | + } |
| 130 | + |
| 131 | + startClock(); |
| 132 | + database.beginTransaction(); |
| 133 | + try { |
| 134 | + for (int i = 0; i < count; i++) { |
| 135 | + database.persistWithId(list.get(i)); |
| 136 | + } |
| 137 | + database.setTransactionSuccessful(); |
| 138 | + } finally { |
| 139 | + database.endTransaction(); |
| 140 | + } |
| 141 | + stopClock(Tools.LogMessage.BATCH_CREATE); |
| 142 | + |
| 143 | + // re-set values to set entity as modified |
| 144 | + for (int i = 0; i < list.size(); i++) { |
| 145 | + updateEntity(list.get(i)); |
| 146 | + } |
| 147 | + |
| 148 | + startClock(); |
| 149 | + database.beginTransaction(); |
| 150 | + try { |
| 151 | + for (int i = 0; i < count; i++) { |
| 152 | + database.persist(list.get(i)); |
| 153 | + } |
| 154 | + database.setTransactionSuccessful(); |
| 155 | + } finally { |
| 156 | + database.endTransaction(); |
| 157 | + } |
| 158 | + stopClock(Tools.LogMessage.BATCH_UPDATE); |
| 159 | + |
| 160 | + startClock(); |
| 161 | + List<SimpleEntityNotNull> reloaded = new ArrayList<>(count); |
| 162 | + |
| 163 | + SquidCursor<SimpleEntityNotNull> query = database.query(SimpleEntityNotNull.class, |
| 164 | + Query.select()); |
| 165 | + while (query.moveToNext()) { |
| 166 | + SimpleEntityNotNull entity = new SimpleEntityNotNull(); |
| 167 | + entity.readPropertiesFromCursor(query); |
| 168 | + reloaded.add(entity); |
| 169 | + } |
| 170 | + query.close(); |
| 171 | + stopClock(Tools.LogMessage.BATCH_READ); |
| 172 | + |
| 173 | + startClock(); |
| 174 | + for (int i = 0; i < reloaded.size(); i++) { |
| 175 | + SimpleEntityNotNull entity = reloaded.get(i); |
| 176 | + long id = entity.getId(); |
| 177 | + Boolean simpleBoolean = entity.isSimpleBoolean(); |
| 178 | + byte simpleByte = entity.getSimpleByte()[0]; |
| 179 | + Integer simpleShort = entity.getSimpleShort(); |
| 180 | + Integer simpleInt = entity.getSimpleInt(); |
| 181 | + Long simpleLong = entity.getSimpleLong(); |
| 182 | + Double simpleFloat = entity.getSimpleFloat(); |
| 183 | + Double simpleDouble = entity.getSimpleDouble(); |
| 184 | + String simpleString = entity.getSimpleString(); |
| 185 | + byte[] simpleByteArray = entity.getSimpleByteArray(); |
| 186 | + } |
| 187 | + stopClock(Tools.LogMessage.BATCH_ACCESS); |
| 188 | + |
| 189 | + startClock(); |
| 190 | + deleteAll(database); |
| 191 | + stopClock(Tools.LogMessage.BATCH_DELETE); |
| 192 | + } |
| 193 | + |
| 194 | + private void deleteAll(MySquidDatabase database) { |
| 195 | + database.deleteAll(SimpleEntityNotNull.class); |
| 196 | + } |
| 197 | + |
| 198 | + protected static SimpleEntityNotNull createEntity(long id) { |
| 199 | + SimpleEntityNotNull entity = new SimpleEntityNotNull(); |
| 200 | + // start with id 1 as 0 is treated as NO_ID |
| 201 | + entity.setId(id + 1); |
| 202 | + entity.setIsSimpleBoolean(true); |
| 203 | + entity.setSimpleByte(new byte[] { Byte.MAX_VALUE }); |
| 204 | + entity.setSimpleShort((int) Short.MAX_VALUE); |
| 205 | + entity.setSimpleInt(Integer.MAX_VALUE); |
| 206 | + entity.setSimpleLong(Long.MAX_VALUE); |
| 207 | + entity.setSimpleFloat((double) Float.MAX_VALUE); |
| 208 | + entity.setSimpleDouble(Double.MAX_VALUE); |
| 209 | + entity.setSimpleString("greenrobot greenDAO"); |
| 210 | + byte[] bytes = { 42, -17, 23, 0, 127, -128 }; |
| 211 | + entity.setSimpleByteArray(bytes); |
| 212 | + return entity; |
| 213 | + } |
| 214 | + |
| 215 | + protected static void updateEntity(SimpleEntityNotNull entity) { |
| 216 | + entity.setIsSimpleBoolean(true); |
| 217 | + entity.setSimpleByte(new byte[] { Byte.MAX_VALUE }); |
| 218 | + entity.setSimpleShort((int) Short.MAX_VALUE); |
| 219 | + entity.setSimpleInt(Integer.MAX_VALUE); |
| 220 | + entity.setSimpleLong(Long.MAX_VALUE); |
| 221 | + entity.setSimpleFloat((double) Float.MAX_VALUE); |
| 222 | + entity.setSimpleDouble(Double.MAX_VALUE); |
| 223 | + entity.setSimpleString("greenrobot greenDAO"); |
| 224 | + byte[] bytes = { 42, -17, 23, 0, 127, -128 }; |
| 225 | + entity.setSimpleByteArray(bytes); |
| 226 | + } |
| 227 | +} |
0 commit comments