Repository navigation
Make JBWAPI ready to connect as fast as possible #97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dgant
wants to merge
8
commits into
JavaBWAPI:develop
Choose a base branch
from
dgant:develop
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+249
−30
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cd27cd8
Avoid allocating WalkPosition for integer walkability queries
dgant 85b1d81
Accelerate BWAPI connection readiness
dgant be48f83
Fix BWEM chokepoint identity for parallel passages
dgant 4f939b0
Move optional pre-extracted JNA support to a separate PR
dgant 4f34d77
Express startup savings as a percentage and trim allocation comment
dgant 6d0ec37
Merge pull request #5 from dgant/codex/chokepoint-identity
dgant 2068443
Share walkability bounds checks between overloads
dgant 5025875
Merge pull request #4 from dgant/allocationless-iswalkable
dgant File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,49 @@ | ||
| package bwapi; | ||
|
|
||
| import org.junit.Test; | ||
|
|
||
| import java.lang.management.BufferPoolMXBean; | ||
| import java.lang.management.ManagementFactory; | ||
|
|
||
| import static org.junit.Assert.assertNull; | ||
| import static org.junit.Assert.assertThrows; | ||
| import static org.junit.Assert.assertTrue; | ||
| import static org.mockito.Mockito.doAnswer; | ||
| import static org.mockito.Mockito.mock; | ||
|
|
||
| /** | ||
| * Guards the order of startup work: attempt the BWAPI connection before creating | ||
| * performance metrics or allocating the large buffers used for asynchronous frames. | ||
| * Measures direct-buffer allocation at a mocked connection boundary, so the test | ||
| * needs neither a running server nor a machine-dependent elapsed-time threshold. | ||
| */ | ||
| public class BWClientStartupTest { | ||
| // Stops startGame at the mocked connection boundary, before a game loop can run. | ||
| private static class StopAfterConnectionAttempt extends RuntimeException { | ||
| } | ||
|
|
||
| @Test | ||
| public void attemptsConnectionBeforeAllocatingFrameBuffersOrMetrics() { | ||
| BWClient bwClient = new BWClient(new DefaultBWListener()); | ||
| Client client = mock(Client.class); | ||
| bwClient.setClient(client); | ||
| BWClientConfiguration configuration = new BWClientConfiguration.Builder() | ||
| .withAsync(true) | ||
| .withAsyncFrameBufferCapacity(3) | ||
| .build(); | ||
| BufferPoolMXBean directBuffers = ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class) | ||
| .stream() | ||
| .filter(pool -> "direct".equals(pool.getName())) | ||
| .findFirst() | ||
| .get(); | ||
| long bytesBeforeStart = directBuffers.getMemoryUsed(); | ||
| doAnswer(invocation -> { | ||
| assertTrue("Connection attempts must precede allocation of even one game-state buffer", | ||
| directBuffers.getMemoryUsed() - bytesBeforeStart < ClientData.GameData.SIZE); | ||
| assertNull(bwClient.getPerformanceMetrics()); | ||
| throw new StopAfterConnectionAttempt(); | ||
| }).when(client).reconnect(); | ||
|
|
||
| assertThrows(StopAfterConnectionAttempt.class, () -> bwClient.startGame(configuration)); | ||
| } | ||
| } |
This file contains hidden or 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
This file contains hidden or 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,44 @@ | ||
| package bwapi; | ||
|
|
||
| import org.junit.Test; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertFalse; | ||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
| public class GameWalkabilityTest { | ||
| @Test | ||
| public void walkabilityQueriesMatchMapDataAcrossTheMap() throws IOException { | ||
| Game game = GameBuilder.createGame(); | ||
| boolean foundWalkable = false; | ||
| boolean foundBlocked = false; | ||
| for (int x = 0; x < game.mapWidth() * 4; x++) { | ||
| for (int y = 0; y < game.mapHeight() * 4; y++) { | ||
| boolean expected = game.botClientData().gameData().isWalkable(x, y); | ||
| assertEquals(expected, game.isWalkable(x, y)); | ||
| assertEquals(expected, game.isWalkable(new WalkPosition(x, y))); | ||
| foundWalkable |= expected; | ||
| foundBlocked |= !expected; | ||
| } | ||
| } | ||
| assertTrue(foundWalkable); | ||
| assertTrue(foundBlocked); | ||
| } | ||
|
|
||
| @Test | ||
| public void rejectsOutOfBoundsWalkCoordinates() throws IOException { | ||
| Game game = GameBuilder.createGame(); | ||
| int[] invalidX = {-1, Integer.MIN_VALUE, game.mapWidth() * 4, 1 << 29, Integer.MAX_VALUE}; | ||
| int[] invalidY = {-1, Integer.MIN_VALUE, game.mapHeight() * 4, 1 << 29, Integer.MAX_VALUE}; | ||
| for (int x : invalidX) { | ||
| assertFalse(game.isWalkable(x, 0)); | ||
| assertFalse(game.isWalkable(new WalkPosition(x, 0))); | ||
| } | ||
| for (int y : invalidY) { | ||
| assertFalse(game.isWalkable(0, y)); | ||
| assertFalse(game.isWalkable(new WalkPosition(0, y))); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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,71 @@ | ||
| package bwapi; | ||
|
|
||
| import com.sun.jna.Pointer; | ||
| import com.sun.jna.platform.win32.Kernel32; | ||
| import com.sun.jna.platform.win32.WinBase; | ||
| import com.sun.jna.platform.win32.WinNT; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertNotNull; | ||
| import static org.junit.Assert.assertNull; | ||
| import static org.junit.Assume.assumeTrue; | ||
|
|
||
| /** | ||
| * Verifies the Windows mapping binding's JNA signatures against a real shared-memory object. | ||
| * The existing JNA Kernel32 binding creates the mapping and its first view; | ||
| * MappingKernel opens a second view. Reads and writes must agree in both directions. | ||
| * A Unicode name checks Windows string marshalling, and a missing mapping must | ||
| * return null. These tests require Windows but do not require a BWAPI server. | ||
| */ | ||
| public class MappingKernelTest { | ||
| @Before | ||
| public void requireWindows() { | ||
| assumeTrue(System.getProperty("os.name").startsWith("Windows")); | ||
| } | ||
|
|
||
| @Test | ||
| public void opensUnicodeNamedMappingAndSharesWritesBetweenViews() { | ||
| // Include Unicode to exercise the same W32API string mapping used by the client. | ||
| String name = "Local\\jbwapi-mapping-\u03bb-" + System.nanoTime(); | ||
| int access = WinNT.SECTION_MAP_READ | WinNT.SECTION_MAP_WRITE; | ||
| WinNT.HANDLE created = Kernel32.INSTANCE.CreateFileMapping( | ||
| WinBase.INVALID_HANDLE_VALUE, null, WinNT.PAGE_READWRITE, 0, 4, name); | ||
| assertNotNull(created); | ||
| WinNT.HANDLE opened = null; | ||
| Pointer originalView = null; | ||
| Pointer clientView = null; | ||
| try { | ||
| originalView = Kernel32.INSTANCE.MapViewOfFile(created, access, 0, 0, 4); | ||
| assertNotNull(originalView); | ||
| originalView.setInt(0, 10003); | ||
|
|
||
| opened = ClientConnectionW32.MappingKernel.INSTANCE.OpenFileMapping(access, false, name); | ||
| assertNotNull(opened); | ||
| clientView = ClientConnectionW32.MappingKernel.INSTANCE.MapViewOfFile(opened, access, 0, 0, 4); | ||
| assertNotNull(clientView); | ||
| assertEquals(10003, clientView.getInt(0)); | ||
|
|
||
| clientView.setInt(0, 42); | ||
| assertEquals(42, originalView.getInt(0)); | ||
| } finally { | ||
| if (clientView != null) { | ||
| Kernel32.INSTANCE.UnmapViewOfFile(clientView); | ||
| } | ||
| if (originalView != null) { | ||
| Kernel32.INSTANCE.UnmapViewOfFile(originalView); | ||
| } | ||
| if (opened != null) { | ||
| Kernel32.INSTANCE.CloseHandle(opened); | ||
| } | ||
| Kernel32.INSTANCE.CloseHandle(created); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void returnsNullForMissingMapping() { | ||
| assertNull(ClientConnectionW32.MappingKernel.INSTANCE.OpenFileMapping( | ||
| WinNT.SECTION_MAP_READ, false, "Local\\jbwapi-missing-" + System.nanoTime())); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.