-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbenchmark.ts
More file actions
302 lines (261 loc) · 8.12 KB
/
Copy pathbenchmark.ts
File metadata and controls
302 lines (261 loc) · 8.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/usr/bin/env bun
import { join, dirname } from "path";
import { mkdir, exists } from "fs/promises";
import { resetDev } from "./reset";
const SCRIPT_DIR = dirname(import.meta.path);
const ROOT_DIR = join(SCRIPT_DIR, "..");
const RUNS = 3;
const OUTPUT_DIR = join(ROOT_DIR, "benchmark-results");
const CLAUDE_PATH = "/Users/sawyerhood/.claude/local/claude";
interface TestCase {
name: string;
directory?: string;
prompt: (methodInstruction: string) => string;
reset?: () => Promise<void>;
}
const TEST_CASES: Record<string, TestCase> = {
"game-tracker": {
name: "game-tracker",
directory: join(ROOT_DIR, "game-tracker"),
prompt: (instruction) =>
`Start the dev server and ${instruction} to create an account (with a unique and secure email and password), login, add 5 games (rate them all a 9), and view the collection. think`,
reset: resetDev,
},
};
type Method = "dev-browser" | "playwright-skill" | "playwright-mcp" | "claude-code-native-chrome";
const METHODS: Record<Method, string> = {
"dev-browser": "use the dev browser",
"playwright-skill": "use the playwright skill",
"playwright-mcp": "use playwright mcp",
"claude-code-native-chrome": "use the browser",
};
interface PluginConfig {
enabledPlugins: Record<string, boolean>;
allowedMcpServers?: Array<{ serverName?: string; serverCommand?: string[] }>;
}
interface McpConfig {
mcpServers: Record<string, { type: string; command: string; args: string[] }>;
}
const METHOD_CONFIGS: Record<
Method,
{ plugins: PluginConfig; mcp: McpConfig }
> = {
"dev-browser": {
plugins: {
enabledPlugins: {
"dev-browser@dev-browser-marketplace": true,
"playwright-skill@playwright-skill": false,
"example-skills@anthropic-agent-skills": false,
},
},
mcp: { mcpServers: {} },
},
"playwright-skill": {
plugins: {
enabledPlugins: {
"dev-browser@dev-browser-marketplace": false,
"playwright-skill@playwright-skill": true,
"example-skills@anthropic-agent-skills": true,
},
},
mcp: { mcpServers: {} },
},
"playwright-mcp": {
plugins: {
enabledPlugins: {
"dev-browser@dev-browser-marketplace": false,
"playwright-skill@playwright-skill": false,
"example-skills@anthropic-agent-skills": false,
},
allowedMcpServers: [{ serverName: "playwright" }],
},
mcp: {
mcpServers: {
playwright: {
type: "stdio",
command: "npx",
args: ["-y", "@playwright/mcp@latest"],
},
},
},
},
"claude-code-native-chrome": {
plugins: {
enabledPlugins: {
"dev-browser@dev-browser-marketplace": false,
"playwright-skill@playwright-skill": false,
"example-skills@anthropic-agent-skills": false,
},
},
mcp: { mcpServers: {} },
},
};
async function configureMethod(
method: Method,
testCase: TestCase
): Promise<void> {
const config = METHOD_CONFIGS[method];
const dir = testCase.directory || ROOT_DIR;
const settingsFile = join(dir, ".claude", "settings.local.json");
const mcpFile = join(dir, ".mcp.json");
// Ensure .claude directory exists
await mkdir(join(dir, ".claude"), { recursive: true });
// Write settings and MCP config
await Bun.write(settingsFile, JSON.stringify(config.plugins, null, 2));
await Bun.write(mcpFile, JSON.stringify(config.mcp, null, 2));
console.log(`Configured for method: ${method}`);
}
async function resetIfNeeded(testCase: TestCase): Promise<void> {
if (testCase.reset) {
await testCase.reset();
}
}
async function runBenchmark(
method: Method,
instruction: string,
run: number,
testCase: TestCase
): Promise<void> {
const outputPath = join(
OUTPUT_DIR,
`${testCase.name}-${method}-run${run}.jsonl`
);
const prompt = testCase.prompt(instruction);
const dir = testCase.directory || ROOT_DIR;
const mcpFile = join(dir, ".mcp.json");
const outputFile = Bun.file(outputPath);
const writer = outputFile.writer();
// Build command args
const args = [
CLAUDE_PATH,
"-p",
prompt,
"--dangerously-skip-permissions",
"--output-format",
"stream-json",
"--verbose",
];
// Add --chrome flag for native chrome method
if (method === "claude-code-native-chrome") {
args.push("--chrome");
}
// Add MCP config if method has MCP servers
const config = METHOD_CONFIGS[method];
if (Object.keys(config.mcp.mcpServers).length > 0) {
args.push("--mcp-config", mcpFile);
}
const proc = Bun.spawn(args, {
cwd: dir,
stdout: "pipe",
stderr: "inherit",
});
// Stream output to both console and file
const reader = proc.stdout.getReader();
const decoder = new TextDecoder();
while (true) {
const { done, value } = await reader.read();
if (done) break;
const text = decoder.decode(value);
process.stdout.write(text);
writer.write(text);
}
writer.end();
await proc.exited;
}
function printUsage(): void {
console.log("Usage: bun run scripts/benchmark.ts [options]");
console.log("");
console.log("Options:");
console.log(" --test-case <name> Run specific test case (default: all)");
console.log(" --method <name> Run specific method (default: all)");
console.log("");
console.log(`Available test cases: ${Object.keys(TEST_CASES).join(", ")}`);
console.log(`Available methods: ${Object.keys(METHODS).join(", ")}`);
}
async function main(): Promise<void> {
const args = process.argv.slice(2);
let methodsToRun: Method[];
let testCasesToRun: TestCase[];
// Parse arguments
let testCaseArg: string | undefined;
let methodArg: string | undefined;
for (let i = 0; i < args.length; i++) {
if (args[i] === "--test-case" && args[i + 1]) {
testCaseArg = args[++i];
} else if (args[i] === "--method" && args[i + 1]) {
methodArg = args[++i];
} else if (args[i] === "--help" || args[i] === "-h") {
printUsage();
process.exit(0);
}
}
// Determine which test cases to run
if (testCaseArg) {
if (!(testCaseArg in TEST_CASES)) {
console.error(`Unknown test case: ${testCaseArg}`);
console.error(
`Available test cases: ${Object.keys(TEST_CASES).join(", ")}`
);
process.exit(1);
}
testCasesToRun = [TEST_CASES[testCaseArg]];
} else {
testCasesToRun = Object.values(TEST_CASES);
}
// Determine which methods to run
if (methodArg) {
if (!(methodArg in METHODS)) {
console.error(`Unknown method: ${methodArg}`);
console.error(`Available methods: ${Object.keys(METHODS).join(", ")}`);
process.exit(1);
}
methodsToRun = [methodArg as Method];
} else {
methodsToRun = Object.keys(METHODS) as Method[];
}
// Check if test case directories exist
for (const testCase of testCasesToRun) {
if (testCase.directory && !(await exists(testCase.directory))) {
console.error(
`Error: ${testCase.name} directory not found at ${testCase.directory}`
);
console.error(
"Run ./setup.sh first to set up the required repositories."
);
process.exit(1);
}
}
// Create output directory
await mkdir(OUTPUT_DIR, { recursive: true });
for (const testCase of testCasesToRun) {
console.log("");
console.log(`======= Test Case: ${testCase.name} =======`);
for (const method of methodsToRun) {
console.log("");
console.log(`=== Running benchmark for: ${method} ===`);
const instruction = METHODS[method];
// Configure plugins and MCPs for this method
await configureMethod(method, testCase);
for (let run = 1; run <= RUNS; run++) {
console.log(
`--- Run ${run} of ${RUNS} for ${testCase.name}/${method} ---`
);
await resetIfNeeded(testCase);
await runBenchmark(method, instruction, run, testCase);
console.log("");
console.log(
`Saved: ${OUTPUT_DIR}/${testCase.name}-${method}-run${run}.jsonl`
);
}
}
}
console.log("");
console.log("=== All benchmarks complete ===");
console.log(
"Run 'bun run scripts/generate-benchmark.ts' to generate comparison report"
);
}
main().catch((err) => {
console.error(err);
process.exit(1);
});