@@ -2,19 +2,11 @@ import * as fs from "fs"
22import * as path from "path"
33import * as os from "os"
44
5+ import pMap from "p-map"
56import { build , filesystem , GluegunPrompt , GluegunToolbox } from "gluegun"
67import { runTests } from "@vscode/test-electron"
78
8- import {
9- type Language ,
10- languages ,
11- type Run ,
12- findRun ,
13- createRun ,
14- getPendingTask ,
15- createPendingTask ,
16- getTask ,
17- } from "@benchmark/db"
9+ import { type Language , languages , type Run , findRun , createRun , getTask , createTask , Task } from "@benchmark/db"
1810
1911import { __dirname , extensionDevelopmentPath , extensionTestsPath , exercisesPath } from "./paths.js"
2012import { getExercises } from "./exercises.js"
@@ -41,34 +33,33 @@ const run = async (toolbox: GluegunToolbox) => {
4133
4234const runAll = async ( id ?: number ) => {
4335 const run = await findOrCreateRun ( { id } )
44- const exercises = getExercises ( )
45-
46- for ( const [ language , languageExercises ] of Object . entries ( exercises ) ) {
47- await Promise . all (
48- languageExercises . map ( ( exercise ) =>
49- findOrCreatePendingTask ( { runId : run . id , language : language as Language , exercise } ) ,
50- ) ,
51- )
52- }
5336
54- for ( const [ language , languageExercises ] of Object . entries ( exercises ) ) {
55- for ( const exercise of languageExercises ) {
56- await runExercise ( { run, language : language as Language , exercise } )
57- }
37+ const entries = Object . entries ( getExercises ( ) ) . flatMap ( ( [ language , languageExercises ] ) =>
38+ languageExercises . map ( ( exercise ) => ( { language : language as Language , exercise } ) ) ,
39+ )
40+
41+ const tasks = await pMap (
42+ entries ,
43+ async ( { language, exercise } ) => findOrCreateTask ( { runId : run . id , language, exercise } ) ,
44+ { concurrency : 10 } ,
45+ )
46+
47+ for ( const task of tasks ) {
48+ await runExercise ( { run, task } )
5849 }
5950}
6051
6152const runLanguage = async ( { id, language } : { id ?: number ; language : Language } ) => {
6253 const run = await findOrCreateRun ( { id } )
63- const exercises = getExercises ( )
64- const languageExercises = exercises [ language ]
6554
66- await Promise . all (
67- languageExercises . map ( ( exercise ) => findOrCreatePendingTask ( { runId : run . id , language, exercise } ) ) ,
55+ const tasks = await pMap (
56+ getExercises ( ) [ language ] ,
57+ async ( exercise ) => findOrCreateTask ( { runId : run . id , language, exercise } ) ,
58+ { concurrency : 10 } ,
6859 )
6960
70- for ( const exercise of languageExercises ) {
71- await runExercise ( { run, language , exercise } )
61+ for ( const task of tasks ) {
62+ await runExercise ( { run, task } )
7263 }
7364}
7465
@@ -82,21 +73,20 @@ const runLanguageExercise = async ({
8273 exercise : string
8374} ) => {
8475 const run = await findOrCreateRun ( { id } )
85- await findOrCreatePendingTask ( { runId : run . id , language, exercise } )
86- return runExercise ( { run, language , exercise } )
76+ const task = await findOrCreateTask ( { runId : run . id , language, exercise } )
77+ return runExercise ( { run, task } )
8778}
8879
89- const runExercise = async ( { run, language, exercise } : { run : Run ; language : Language ; exercise : string } ) => {
80+ const runExercise = async ( { run, task } : { run : Run ; task : Task } ) => {
81+ const { language, exercise } = task
9082 const workspacePath = path . resolve ( exercisesPath , language , exercise )
9183 const promptPath = path . resolve ( exercisesPath , `prompts/${ language } .md` )
9284
9385 if ( ! fs . existsSync ( promptPath ) ) {
9486 throw new Error ( `Prompt file does not exist: ${ promptPath } ` )
9587 }
9688
97- const task = await getTask ( { runId : run . id , language, exercise } )
98-
99- if ( task ) {
89+ if ( task . finishedAt ) {
10090 console . log ( `Test result exists for ${ language } / ${ exercise } , skipping` )
10191 return false
10292 }
@@ -108,7 +98,7 @@ const runExercise = async ({ run, language, exercise }: { run: Run; language: La
10898 extensionTestsPath,
10999 launchArgs : [ workspacePath , "--disable-extensions" ] ,
110100 extensionTestsEnv : {
111- RUN_ID : run . id . toString ( ) ,
101+ TASK_ID : task . id . toString ( ) ,
112102 LANGUAGE : language ,
113103 EXERCISE : exercise ,
114104 PROMPT_PATH : promptPath ,
@@ -157,15 +147,15 @@ const findOrCreateRun = async ({ id, model = "anthropic/claude-3.7-sonnet" }: {
157147 socketPath : path . resolve ( os . tmpdir ( ) , `benchmark-${ crypto . randomUUID ( ) } .sock` ) ,
158148 } )
159149
160- const findOrCreatePendingTask = async ( {
150+ const findOrCreateTask = async ( {
161151 runId,
162152 language,
163153 exercise,
164154} : {
165155 runId : number
166156 language : Language
167157 exercise : string
168- } ) => ( await getPendingTask ( { runId, language, exercise } ) ) || ( await createPendingTask ( { runId, language, exercise } ) )
158+ } ) => ( await getTask ( { runId, language, exercise } ) ) || ( await createTask ( { runId, language, exercise } ) )
169159
170160const main = async ( ) => {
171161 const cli = build ( )
0 commit comments