1717'use strict' ;
1818
1919const { MutationSet, Spanner} = require ( '@google-cloud/spanner' ) ;
20+ const { readFileSync} = require ( 'fs' ) ;
2021
21- async function main ( cmd , projectId , instanceId , databaseId ) {
22- if ( cmd === 'compare' ) { // Compare benchmarks.
23- const [ v1JSONFile , v2JSONFile ] = process . args . slice ( 1 ) ;
24- const v1JSON = JSON . parse ( fs . readFileSync ( v1JSONFile ) ) ;
25- const v2JSON = JSON . parse ( fs . readFileSync ( v2JSONFile ) ) ;
26- return compareDifferences ( v1JSON , v2JSON ) ;
22+ async function runComparisons ( v1JSONFile , v2JSONFile ) {
23+ const v1JSON = JSON . parse ( readFileSync ( v1JSONFile ) ) ;
24+ const v2JSON = JSON . parse ( readFileSync ( v2JSONFile ) ) ;
25+ return compareDifferences ( v1JSON , v2JSON ) ;
26+ }
27+
28+ function main ( ) {
29+ if ( process . argv [ 2 ] === 'compare' ) {
30+ runComparisons ( ...process . argv . slice ( 3 ) ) ;
31+ } else {
32+ runBenchmarking ( ...process . argv . slice ( 2 ) ) ;
2733 }
34+ }
2835
36+ async function runBenchmarking ( projectId , instanceId , databaseId ) {
2937 // Otherwise run the benchmarks.
3038 const spanner = new Spanner ( {
3139 projectId : projectId ,
@@ -240,7 +248,7 @@ async function databaseWriteAtLeastOnce(database) {
240248
241249function compareDifferences ( v1 , v2 ) {
242250 const percents = [ ] ;
243- for ( const key of v1 ) {
251+ for ( const key in v1 ) {
244252 const defV1 = v1 [ key ] ;
245253 const ramV1 = defV1 . ram ;
246254 const latencyV1 = defV1 . latency ;
@@ -250,8 +258,16 @@ function compareDifferences(v1, v2) {
250258
251259 percents . push ( {
252260 key : key ,
253- ram : calculatePercent ( ramV1 , ramV2 ) ,
254- latency : calculatePercent ( latencyV1 , latencyV2 ) ,
261+ ramP50 :calculatePercent ( ramV1 . p50 , ramV2 . p50 ) . toFixed ( 2 ) ,
262+ ramP75 :calculatePercent ( ramV1 . p75 , ramV2 . p75 ) . toFixed ( 2 ) ,
263+ ramP90 :calculatePercent ( ramV1 . p90 , ramV2 . p90 ) . toFixed ( 2 ) ,
264+ ramP95 :calculatePercent ( ramV1 . p95 , ramV2 . p95 ) . toFixed ( 2 ) ,
265+ ramP99 :calculatePercent ( ramV1 . p95 , ramV2 . p99 ) . toFixed ( 2 ) ,
266+ latP50 : calculatePercent ( latencyV1 . p50 , latencyV2 . p50 ) . toFixed ( 2 ) ,
267+ latP75 : calculatePercent ( latencyV1 . p75 , latencyV2 . p75 ) . toFixed ( 2 ) ,
268+ latP90 : calculatePercent ( latencyV1 . p90 , latencyV2 . p90 ) . toFixed ( 2 ) ,
269+ latP95 : calculatePercent ( latencyV1 . p95 , latencyV2 . p95 ) . toFixed ( 2 ) ,
270+ latP99 : calculatePercent ( latencyV1 . p95 , latencyV2 . p99 ) . toFixed ( 2 ) ,
255271 } ) ;
256272 }
257273
@@ -262,9 +278,12 @@ function compareDifferences(v1, v2) {
262278 return 0 ;
263279 } ) ;
264280
265- console . log ( `Method RAM % Latency %` ) ;
266281 for ( const value of percents ) {
267- console . log ( `${ value . key } ${ value . ram . toString ( 2 ) } ${ value . latency . toString ( 2 ) } ` ) ;
282+ console . log ( `${ value . key } ` ) ;
283+ console . log ( `\t p50 (%) p75 (%) p90 (%) p95 (%) p99 (%)` ) ;
284+ console . log ( `\tRAM: ${ value . ramP50 } ${ value . ramP75 } ${ value . ramP90 } ${ value . ramP95 } ${ value . ramP99 } ` ) ;
285+ console . log ( `\tLat: ${ value . latP50 } ${ value . latP75 } ${ value . latP90 } ${ value . latP95 } ${ value . latP99 } ` ) ;
286+ console . log ( '' ) ;
268287 }
269288}
270289
@@ -276,4 +295,4 @@ process.on('unhandledRejection', err => {
276295 console . error ( err . message ) ;
277296 process . exitCode = 1 ;
278297} ) ;
279- main ( ... process . argv . slice ( 2 ) ) ;
298+ main ( ) ;
0 commit comments