11import Docker from 'dockerode' ;
2- import path from 'path' ;
3- import { performance } from 'perf_hooks' ;
2+ import {
3+ performance ,
4+ } from 'perf_hooks' ;
45import del from 'del' ;
5- import writeToFile from './utils/writeToFile' ;
6- import generateFolder from './utils/generateFolder' ;
76import decodeBase64 from './utils/decodeBase64' ;
87import logger from './utils/logger' ;
9- import findExtension from './utils/findExtension' ;
108import {
119 TestCase ,
1210 Result ,
1311 Tests ,
1412} from './models/models' ;
1513import getOutput from './utils/getOutput' ;
14+ import saveCode from './utils/saveCode' ;
1615
1716interface RunnerOpts {
1817 id : string ;
@@ -31,30 +30,6 @@ export default class Runner {
3130 this . docker = docker ;
3231 }
3332
34- private static async saveCode (
35- folderPath : string ,
36- code : string ,
37- testCases : TestCase [ ] ,
38- base64 : boolean ,
39- language : string ,
40- ) : Promise < string > {
41- const folder = await generateFolder ( folderPath ) ;
42- const extension = findExtension ( language ) ;
43- const promisesToKeep = [ ( base64 )
44- ? writeToFile ( path . join ( folder , `code.${ extension } ` ) , decodeBase64 ( code ) )
45- : writeToFile ( path . join ( folder , `code.${ extension } ` ) , code ) ,
46- ] ;
47- for ( let i = 0 ; i < testCases . length ; i += 1 ) {
48- const [ input , output ] = ( base64 )
49- ? [ decodeBase64 ( testCases [ i ] . input ) , decodeBase64 ( testCases [ i ] . output ) ]
50- : [ testCases [ i ] . input , testCases [ i ] . output ] ;
51- promisesToKeep . push ( writeToFile ( path . join ( folder , `in${ i } .txt` ) , input ) ) ;
52- promisesToKeep . push ( writeToFile ( path . join ( folder , `out${ i } .txt` ) , output ) ) ;
53- }
54- await Promise . all ( promisesToKeep ) ;
55- return folder ;
56- }
57-
5833 async run ( {
5934 id,
6035 tag,
@@ -65,37 +40,38 @@ export default class Runner {
6540 language,
6641 timeout,
6742 } : RunnerOpts ) : Promise < Result > {
68- const Path = await Runner . saveCode ( folderPath , code , testCases , base64 , language ) ;
69- const container = await this . docker . createContainer ( {
70- Image : tag ,
71- Cmd : [ 'bash' , '/start.sh' , `${ testCases . length } ` , `${ timeout } ` ] ,
72- HostConfig : {
73- Mounts : [ {
74- Source : Path ,
75- Target : '/app' ,
76- Type : 'bind' ,
77- } ] ,
78- } ,
79- } ) ;
80-
43+ const Paths = await saveCode ( folderPath , code , testCases , base64 , language ) ;
44+ const promisesToKeep : Array < Promise < Array < object > > > = [ ] ;
45+ for ( let i = 0 ; i < Paths . length ; i += 1 ) {
46+ promisesToKeep . push ( this . docker . run ( tag , [ 'bash' , '/start.sh' , `${ i } ` , `${ timeout } ` ] , null , {
47+ HostConfig : {
48+ AutoRemove : true ,
49+ Mounts : [ {
50+ Source : Paths [ i ] ,
51+ Target : '/app' ,
52+ Type : 'bind' ,
53+ } ] ,
54+ } ,
55+ } ) ) ;
56+ }
8157 logger . info ( `Starting process ${ id } ` ) ;
82-
8358 const t0 = performance . now ( ) ;
84- await container . start ( ) ;
85- await container . wait ( ) ;
59+ await Promise . all ( promisesToKeep ) ;
8660 const t1 = performance . now ( ) ;
61+ logger . info ( `Process ${ id } completed in ${ ( t1 - t0 ) / 1000 } seconds` ) ;
8762
88- logger . info ( `Process ${ id } completed in ${ t1 / 1000 - t0 / 1000 } seconds` ) ;
89-
90- container . remove ( ) ;
91- const [ output , runTime , error , exitCodes ] = await getOutput ( Path , testCases . length ) ;
92- del ( Path , {
93- force : true ,
63+ const [ output , runTime , error , exitCodes ] = await getOutput ( Paths , testCases . length ) ;
64+ Paths . forEach ( ( Path ) => {
65+ del ( Path , {
66+ force : true ,
67+ } ) ;
9468 } ) ;
9569
9670 const tests : Tests [ ] = [ ] ;
9771 for ( let i = 0 ; i < testCases . length ; i += 1 ) {
98- const expectedOutput = testCases [ i ] . output ;
72+ const expectedOutput = ( base64 )
73+ ? decodeBase64 ( testCases [ i ] . output )
74+ : testCases [ i ] . output ;
9975 const obtainedOutput = output [ i ] . toString ( ) ;
10076 const time = runTime [ i ] . toString ( ) . split ( '\n' ) ;
10177 const exitCode = parseInt ( exitCodes [ i ] . toString ( ) , 10 ) ;
0 commit comments