-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e8354e0
commit 7c4f371
Showing
6 changed files
with
113 additions
and
9 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/*! | ||
* Copyright 2025 Google LLC. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const muxEnabledTrue = []; | ||
const muxEnabledFalse = []; | ||
const transaction_times = []; | ||
async function main( | ||
instanceId, | ||
databaseId, | ||
projectId, | ||
multiplexedEnabled, | ||
numThreads, | ||
numQueries | ||
) { | ||
async function readQuery(database) { | ||
const startTime = Date.now(); | ||
const query = { | ||
sql: 'SELECT * FROM Singers', | ||
}; | ||
await database.run(query); | ||
const operationTime = Date.now() - startTime; | ||
transaction_times.push(operationTime); | ||
} | ||
|
||
async function runQueries() { | ||
multiplexedEnabled === 'true' | ||
? (process.env.GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS = true) | ||
: (process.env.GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS = false); | ||
|
||
const startTime = Date.now(); | ||
const {Spanner} = require('../build/src'); | ||
const spanner = new Spanner({ | ||
projectId, | ||
}); | ||
|
||
// Gets a reference to a Cloud Spanner instance and database | ||
const instance = spanner.instance(instanceId); | ||
const database = instance.database(databaseId); | ||
|
||
for (let i = 0; i < numQueries; i++) { | ||
await readQuery(database); | ||
} | ||
|
||
multiplexedEnabled === 'true' | ||
? muxEnabledTrue.push(Date.now() - startTime) | ||
: muxEnabledFalse.push(Date.now() - startTime); | ||
} | ||
|
||
function calculatePercentiles(latencies) { | ||
// Step 1: Sort the array | ||
const sortedLatencies = latencies.slice().sort((a, b) => a - b); | ||
|
||
// Step 2: Calculate p50 (50th percentile) | ||
const p50Index = Math.floor(0.5 * sortedLatencies.length); | ||
const p50Latency = sortedLatencies[p50Index]; | ||
|
||
// Step 3: Calculate p90 (90th percentile) | ||
const p90Index = Math.floor(0.9 * sortedLatencies.length); | ||
const p90Latency = sortedLatencies[p90Index]; | ||
// Step 3: Calculate p99 (99th percentile) | ||
const p99Index = Math.floor(0.99 * sortedLatencies.length); | ||
const p99Latency = sortedLatencies[p99Index]; | ||
|
||
return { | ||
p50: p50Latency, | ||
p90: p90Latency, | ||
p99: p99Latency, | ||
}; | ||
} | ||
|
||
async function runConcurrently() { | ||
const promises = []; | ||
for (let i = 0; i < numThreads; i++) { | ||
promises.push(runQueries()); | ||
} | ||
await Promise.all(promises); | ||
} | ||
|
||
runConcurrently() | ||
.then(() => { | ||
const percentiles = calculatePercentiles(transaction_times); | ||
console.log(`p50 Latency: ${percentiles.p50}`); | ||
console.log(`p90 Latency: ${percentiles.p90}`); | ||
console.log(`p99 Latency: ${percentiles.p99}`); | ||
}) | ||
.catch(error => { | ||
console.log('error: ', error); | ||
}); | ||
} | ||
|
||
process.on('unhandledRejection', err => { | ||
console.error(err.message); | ||
process.exitCode = 1; | ||
}); | ||
main(...process.argv.slice(2)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters