-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve FeatureRepository impl performance
reason: the backing data structure for features was an ArrayList, which has O(n) worst-case look-up performance. since the method findFeature(...) is frequently used, this has negative implications on the performance of the library. A HashMap is better suited for feature look-up.
- Loading branch information
Showing
2 changed files
with
84 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
ocpp-common/src/test/java/eu/chargetime/ocpp/test/FeatureRepositoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package eu.chargetime.ocpp.test; | ||
|
||
import eu.chargetime.ocpp.FeatureRepository; | ||
import eu.chargetime.ocpp.feature.Feature; | ||
import eu.chargetime.ocpp.model.Confirmation; | ||
import eu.chargetime.ocpp.model.Request; | ||
import eu.chargetime.ocpp.model.TestConfirmation; | ||
import eu.chargetime.ocpp.model.TestRequest; | ||
import org.junit.Test; | ||
|
||
import java.util.Optional; | ||
import java.util.UUID; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class FeatureRepositoryTest { | ||
|
||
private final TestFeature feature = new TestFeature(); | ||
private static final String ACTION_NAME = "TestFeature"; | ||
|
||
@Test | ||
public void testFind() { | ||
FeatureRepository f = new FeatureRepository(); | ||
f.addFeature(feature); | ||
|
||
assertFalse(f.findFeature("TestFeatureFail").isPresent()); | ||
assertFalse(f.findFeature(3).isPresent()); | ||
|
||
assertWhenFound(f.findFeature(ACTION_NAME)); | ||
assertWhenFound(f.findFeature(new TestRequest())); | ||
assertWhenFound(f.findFeature(new TestConfirmation())); | ||
} | ||
|
||
private void assertWhenFound(Optional<Feature> dummyFeature) { | ||
assertTrue(dummyFeature.isPresent()); | ||
assertEquals(dummyFeature.get(), feature); | ||
} | ||
|
||
private static class TestFeature implements Feature { | ||
|
||
@Override | ||
public Confirmation handleRequest(UUID sessionIndex, Request request) { | ||
throw new RuntimeException("Do not call this method"); | ||
} | ||
|
||
@Override | ||
public Class<? extends Request> getRequestType() { | ||
return TestRequest.class; | ||
} | ||
|
||
@Override | ||
public Class<? extends Confirmation> getConfirmationType() { | ||
return TestConfirmation.class; | ||
} | ||
|
||
@Override | ||
public String getAction() { | ||
return ACTION_NAME; | ||
} | ||
} | ||
} |