我在 GCP 中有 firestore 数据库。我通过公共路线访问 firestore 数据库,延迟约为 300 毫秒 (280-300)。
我创建了一个私有服务连接端点来减少延迟并私下访问数据库。
但是 PSC 端点之后的事件我得到了相同的延迟。
下面是我的脚本,用于检查端点延迟、读取和写入延迟。
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);
我从云外壳实例尝试一下。
我想实现云外壳延迟(〜80毫秒),在GCP中是否可能,如果是的话如何实现?
奋斗了2天后我才知道
我的脚本没有连接到同一区域的 firestore,而是连接到不同区域(asia-south1)。这就是延迟增加的原因。
当我在同一区域创建 firestore 和 VM 时,我的延迟读取 = 10 毫秒,写入 = 9 毫秒。
还是不确定