Skip to content

Commit

Permalink
Add benchmarking
Browse files Browse the repository at this point in the history
  • Loading branch information
dkorpel committed Aug 21, 2018
1 parent 902fd4f commit f2153d7
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 12 deletions.
7 changes: 4 additions & 3 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@
}
}

function _log(ptr, len) {
console.log(toJsString(ptr, len));
function reportBenchmark(number, time) {
console.log("Evaluated "+number+" moves in "+time+" ms");
}

function getTimeMillis() {
Expand Down Expand Up @@ -118,7 +118,8 @@
drawCircle,
setAlpha,
setFont,
getTimeMillis
getTimeMillis,
reportBenchmark
}
};

Expand Down
Binary file modified docs/tictac.wasm
Binary file not shown.
31 changes: 28 additions & 3 deletions source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,16 @@ extern(C):
}
}

void _log(immutable(char)* ptr, size_t len);

void consoleLog(string str) {
int getTimeMillis() @nogc;
void reportBenchmark(int number, int msecs) @nogc;
__gshared int benchmarkStartTime = 0;

void benchmarkStart() @nogc {
benchmarkStartTime = getTimeMillis();
}

void benchmarkEnd(int number) @nogc {
reportBenchmark(number, getTimeMillis()-benchmarkStartTime);
}

// These simple C standard library implementations are needed because
Expand Down Expand Up @@ -144,4 +150,23 @@ extern(C):

return result;
}

// Benchmark stuff
enum bool benchmarkShown = false;
long benchmarkMsecs = 0;
long benchmarkMoves = 0;

import std.datetime.stopwatch: StopWatch, AutoStart;
private StopWatch stopWatch = StopWatch(AutoStart.no);

void benchmarkStart() @nogc {
stopWatch.reset();
stopWatch.start();
}

void benchmarkEnd(int movesAmount) @nogc {
stopWatch.stop();
benchmarkMsecs = stopWatch.peek.total!"msecs";
benchmarkMoves = movesAmount;
}
}
6 changes: 5 additions & 1 deletion source/draw.d
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,11 @@ void drawMessage(in ref GameState state) {
auto str = messageStrings[state.message];
int i = clamp(state.sinceLastMessage * 4, 0, cast(int) str.length);

window.write(1, 23, bgField, fgField, str[0..i]); //, str[0..$]
window.write(1, 23, bgField, fgField, str[0..i]);

import app: benchmarkMoves, benchmarkMsecs, benchmarkShown;
if (benchmarkShown)
window.write(1, 24, bgField, fgField, "Evaluated ", benchmarkMoves, " moves in ", benchmarkMsecs, "ms");
}

void drawTitleScreen(in ref GameState) {
Expand Down
12 changes: 10 additions & 2 deletions source/game.d
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ void updateGame(ref GameState state, Input input) {
}
break;
case Mode.scramble:
if (globalTimer & 1) state.field.randomizeField();
if ((globalTimer % 4) == 0) state.field.randomizeField();
if (input.enter)
{
field.getValidMoves();
Expand Down Expand Up @@ -358,9 +358,11 @@ void doMove(ref Field field, int x, int y) {
Who winner = field.checkGameWin();
if (winner != Who.noone) {
field.gameWon = winner;
field.validMoves = 0;
} else {
field.getValidMoves();
}

field.getValidMoves();
}

// Cap amount of moves to prevent absurd waiting times for A.I.
Expand All @@ -373,7 +375,13 @@ enum maxMovesConsidered = 3_000_000;
void cpuDoMove(ref GameState state) {
int x, y;
totalMovesConsidered = 0;

import app: benchmarkStart, benchmarkEnd;

benchmarkStart();
bestMoveScore(state.field, state.cpuRecursionLevel, x, y);
benchmarkEnd(totalMovesConsidered);

state.field.doMove(x, y);
state.updateAfterMove();
}
Expand Down
9 changes: 6 additions & 3 deletions source/jsdraw.d
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ void drawField(in ref Field field, in ref WindowContext context, double anim = 1
drawLine(x, y+offset, x+edge, y+offset, innerThickness, fgColor);
}

// Content and valid moves
// Content
drawSetFont("30px Arial");
foreach(i; 0..9) foreach(j; 0..9) {
auto str = field.fieldContent[i][j].toSymbol;
Expand All @@ -148,18 +148,21 @@ void drawField(in ref Field field, in ref WindowContext context, double anim = 1
str.ptr, str.length,
fgColor,
);
}

// Valid moves
if (field.gameWon == Who.noone) foreach(i; 0..9) foreach(j; 0..9) {
if (field.validMove[i][j] == Validity.allowed) {
// "Blink animation", going quickly to alpha 0.75 and slowly back to 0.25
if (anim > 0.5) setAlpha(1.25-anim);
else if (anim > 0.25) setAlpha((anim-0.25)*3);
else setAlpha(0);

drawRect(
x + i*cellEdge, y + j *cellEdge,
cellEdge, cellEdge,
allowedCellColor);

setAlpha(1);
}
}
Expand Down

0 comments on commit f2153d7

Please sign in to comment.