1+ import { BenchmarkFunction } from "./benchmark_types" ;
2+
13export function round ( num : number , decimalPlaces : number = 0 ) {
24 return tonumber ( string . format ( `%.${ decimalPlaces } f` , num ) ) ;
35}
@@ -6,3 +8,64 @@ export const json: {
68 decode : ( this : void , str : string ) => { } ;
79 encode : ( this : void , val : any ) => string ;
810} = require ( "json" ) ;
11+
12+ export function readAll ( file : LuaFile ) : string | undefined {
13+ let content : ( string | undefined ) [ ] ;
14+ if ( _VERSION == "Lua 5.3" ) {
15+ // @ts -ignore
16+ content = file . read ( "a" ) ;
17+ } else {
18+ // JIT
19+ // @ts -ignore
20+ content = file . read ( "*a" ) ;
21+ }
22+
23+ if ( content && content [ 0 ] ) {
24+ return content [ 0 ] ;
25+ }
26+ }
27+
28+ export function readDir ( dir = "" ) : string [ ] | undefined {
29+ let isWindows = false ;
30+ let [ success , findHandle ] = pcall ( ( ) => io . popen ( `find ${ dir } -type f -d 1` ) ) ;
31+
32+ if ( ! success ) {
33+ [ success , findHandle ] = pcall ( ( ) => io . popen ( `dir /A-D /B ${ dir } ` ) ) ;
34+ isWindows = true ;
35+ }
36+
37+ if ( success ) {
38+ // appereantly TS can't infer this on it's own
39+ findHandle = findHandle as LuaFile ;
40+
41+ const findResult = readAll ( findHandle ) ;
42+ findHandle . close ( ) ;
43+
44+ if ( findResult ) {
45+ let files = findResult . split ( "\n" ) ;
46+ if ( isWindows ) {
47+ // on windows we need to append the directory path
48+ // on unix this is done by find automatically
49+ files = files . map ( f => `${ dir } /${ f } ` ) ;
50+ }
51+ return findResult . split ( "\n" ) . filter ( p => p !== "" ) ;
52+ }
53+ }
54+ }
55+
56+ export function loadBenchmarksFromDirectory ( dir = "" ) : BenchmarkFunction [ ] {
57+ // Memory tests
58+ const benchmarkPaths = readDir ( dir ) ;
59+
60+ if ( ! benchmarkPaths ) {
61+ return [ ] ;
62+ }
63+
64+ return benchmarkPaths . map ( f => {
65+ // replace slashes with dots
66+ let dotPath = string . gsub ( f , "%/" , "." ) [ 0 ] ;
67+ // remove extension
68+ dotPath = string . gsub ( dotPath , ".lua" , "" ) [ 0 ] ;
69+ return require ( dotPath ) . default as BenchmarkFunction ;
70+ } ) ;
71+ }
0 commit comments