Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions src/runner.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { test, expect, describe } from "vitest";
import { countUploadableQueries, UPLOADABLE_STATES } from "./runner.ts";
import { buildQueries } from "./reporters/site-api.ts";
import type { OptimizedQuery } from "./sql/recent-query.ts";

function fakeQuery(state: string): OptimizedQuery {
return {
optimization: { state },
} as OptimizedQuery;
}

describe("countUploadableQueries", () => {
test("counts improvements_available, no_improvement_found, and error", () => {
const results = [
fakeQuery("improvements_available"),
fakeQuery("no_improvement_found"),
fakeQuery("error"),
];
expect(countUploadableQueries(results)).toBe(3);
});

test("excludes not_supported, timeout, waiting, optimizing", () => {
const results = [
fakeQuery("not_supported"),
fakeQuery("timeout"),
fakeQuery("waiting"),
fakeQuery("optimizing"),
];
expect(countUploadableQueries(results)).toBe(0);
});

test("counts only uploadable in a mixed set", () => {
const results = [
fakeQuery("improvements_available"),
fakeQuery("not_supported"),
fakeQuery("no_improvement_found"),
fakeQuery("timeout"),
fakeQuery("error"),
fakeQuery("waiting"),
];
expect(countUploadableQueries(results)).toBe(3);
});
});

describe("UPLOADABLE_STATES matches buildQueries filter", () => {
test("buildQueries keeps exactly the same states as UPLOADABLE_STATES", () => {
const allStates = [
"improvements_available",
"no_improvement_found",
"error",
"not_supported",
"timeout",
"waiting",
"optimizing",
];

for (const state of allStates) {
const results = [fakeQuery(state)];
const uploaded = buildQueries(results).length;
const counted = countUploadableQueries(results);
expect(counted, `state "${state}"`).toBe(uploaded);
}
});
});
13 changes: 9 additions & 4 deletions src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ export class Runner {
error = err;
});

let total = 0;

console.time("total");
const recentQueries: RecentQuery[] = [];
for await (const chunk of stream) {
Expand Down Expand Up @@ -134,7 +132,6 @@ export class Runner {
continue;
}

total++;
const recentQuery = await RecentQuery.fromLogEntry(query, hash);
recentQueries.push(recentQuery)
}
Expand All @@ -155,7 +152,7 @@ export class Runner {
});

console.log(
`Matched ${this.remote.optimizer.validQueriesProcessed} queries out of ${total}`,
`Matched ${this.remote.optimizer.validQueriesProcessed} queries`,
);

const recommendations: ReportIndexRecommendation[] = [];
Expand Down Expand Up @@ -220,6 +217,8 @@ export class Runner {
}
}

const total = countUploadableQueries(allResults);

const statistics = deriveIndexStatistics(filteredRecommendations);
const timeElapsed = Date.now() - startDate.getTime();
const reportContext: ReportContext = {
Expand Down Expand Up @@ -247,4 +246,10 @@ export class Runner {
}
}

export const UPLOADABLE_STATES = new Set(["improvements_available", "no_improvement_found", "error"]);

export function countUploadableQueries(results: OptimizedQuery[]): number {
return results.filter((q) => UPLOADABLE_STATES.has(q.optimization.state)).length;
}

export type QueryProcessResult = OptimizedQuery;
Loading