First round of optimisation to CardDb

This optimisation removes redundant queries when looking for cards by specificed Art Preference.

getCardsFromSet has been worked-around, avoiding querying for potential candidates already in memory.
A preliminary benchmark/tests is implemented too.
This commit is contained in:
leriomaggio
2021-08-25 18:15:28 +01:00
parent 637aadc188
commit 841c98dc12
2 changed files with 112 additions and 17 deletions

View File

@@ -0,0 +1,86 @@
package forge.card;
import forge.ImageKeys;
import forge.item.PaperCard;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.util.Collection;
import java.util.Set;
import java.util.TreeSet;
import static org.testng.Assert.assertNotNull;
public class CardDbPerformanceTests extends CardDbTestCase {
private Set<String> fullDbCardNames = new TreeSet<>();
@Override
@BeforeMethod
public void setup() {
super.setup();
Collection<PaperCard> uniqueCards = this.cardDb.getUniqueCards();
for (PaperCard card : uniqueCards)
this.fullDbCardNames.add(card.getName());
}
@Test
public void testBenchmarkFullDbGetCardLegacyImplementation() {
int nRuns = 100;
long averageTime = 0;
long minTime = 10000; // 10 secs
long maxTime = 0;
for (int r = 1; r <= nRuns; r++) {
long start = System.currentTimeMillis();
for (String name : this.fullDbCardNames) {
PaperCard card = this.legacyCardDb.getCard(name);
assertNotNull(card);
}
long timeRun = System.currentTimeMillis() - start;
averageTime += timeRun;
if (timeRun < minTime)
minTime = timeRun;
if (timeRun > maxTime)
maxTime = timeRun;
}
System.out.println("[LEGACY] Total Time (in sec): " + ((double) averageTime)/ 1000);
System.out.println("[LEGACY] Average Time (in sec): " + ((double) averageTime / nRuns)/ 1000);
System.out.println("[LEGACY] Best Time (in sec): " + ((double) minTime)/ 1000);
System.out.println("[LEGACY] Worst Time (in sec): " + ((double) maxTime)/ 1000);
}
@Test
public void testBenchmarkFullDbGetCardNewDbImplementation() {
int nRuns = 100;
long averageTime = 0;
long minTime = 10000; // 10 secs
long maxTime = 0;
for (int r = 1; r <= nRuns; r++) {
long start = System.currentTimeMillis();
for (String name : this.fullDbCardNames) {
PaperCard card = this.cardDb.getCard(name);
assertNotNull(card);
}
long timeRun = System.currentTimeMillis() - start;
averageTime += timeRun;
if (timeRun < minTime)
minTime = timeRun;
if (timeRun > maxTime)
maxTime = timeRun;
}
System.out.println("[NEW] Total Time (in sec): " + ((double) averageTime)/ 1000);
System.out.println("[NEW] Average Time (in sec): " + ((double) averageTime / nRuns)/ 1000);
System.out.println("[NEW] Best Time (in sec): " + ((double) minTime)/ 1000);
System.out.println("[NEW] Worst Time (in sec): " + ((double) maxTime)/ 1000);
}
@Test
public void testGetCardFullDbNewImplementationToProfile(){
for (String name : this.fullDbCardNames) {
PaperCard card = this.cardDb.getCard(name);
assertNotNull(card);
}
}
}