I have firestore DB in GCP. I was accessing the firestore DB via public route and the latency was around 300ms (280-300).
I create a private service connect endpoint to reduce the latency and access DB privately.
But event after PSC endpoint I’m getting same latency.
Below is my script to check endpoint latency, read and write latency.
const admin = require('firebase-admin');
const path = require('path');
const axios = require('axios');
// Initialize Firebase Admin SDK
const serviceAccount = require(path.resolve(__dirname, '/home/amityadav/sa.json'));
// Replace with your own service account key path
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});
console.log('Credentials After initializeApp:', serviceAccount.client_email);
const db = admin.firestore();
const collectionName = 'testCollection';
const documentId = 'testDoc';
const docRef = db.collection(collectionName).doc(documentId);
console.log('Firestore Path:', `projects/${serviceAccount.project_id}/databases/(default)/documents/${collectionName}/${documentId}`);
// Function to test network latency
async function testNetworkLatency(url) {
const start = Date.now();
try {
await axios.get(url);
const end = Date.now();
return end - start;
} catch (error) {
console.error(`Error accessing ${url}:`, error);
return -1;
}
}
// Function to check if accessing DB privately or publicly
async function checkAccessMethod() {
const privateUrl = 'http://metadata.google.internal'; // Known private endpoint
const publicUrl = 'https://www.google.com'; // Public endpoint
const privateLatency = await testNetworkLatency(privateUrl);
const publicLatency = await testNetworkLatency(publicUrl);
console.log(`Private Network Latency: ${privateLatency} ms`);
console.log(`Public Network Latency: ${publicLatency} ms`);
if (privateLatency > 0 && (privateLatency < publicLatency)) {
console.log('Accessing Firestore privately.');
} else {
console.log('Accessing Firestore publicly.');
}
}
// Function to write data to Firestore
async function writeToFirestore(data) {
const startWrite = Date.now();
try {
await db.collection(collectionName).doc(documentId).set(data);
const endWrite = Date.now();
const writeLatency = endWrite - startWrite;
console.log(`Write Latency: ${writeLatency} ms`);
} catch (error) {
console.error('Error writing to Firestore:', error);
}
}
// Function to read data from Firestore
async function readFromFirestore() {
const startRead = Date.now();
try {
const docRef = db.collection(collectionName).doc(documentId);
const snapshot = await docRef.get();
const endRead = Date.now();
const readLatency = endRead - startRead;
console.log(`Read Latency: ${readLatency} ms`);
// Print the data fetched (optional)
if (snapshot.exists) {
console.log('Fetched Data:', snapshot.data());
} else {
console.log('No data found');
}
} catch (error) {
console.error('Error reading from Firestore:', error);
}
}
// Function to execute write and read operations every 2 seconds
async function executeOperations() {
try {
const testData = { message: 'Hello, Firestore!' };
// Check if accessing DB privately or publicly
await checkAccessMethod();
// Write data to Firestore
await writeToFirestore(testData);
// Read data from Firestore
await readFromFirestore();
} catch (error) {
console.error('Error executing operations:', error);
}
}
// Start executing operations every 2 seconds
setInterval(executeOperations, 2000);
I give it a try from cloud shell instance.
Output from my VM shell
Output from Cloud shell
I wanted to achieve cloud shell latency (~80ms), Is it possible in GCP and If yes HOW ?
Amit Yadav is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.