Firestore DB 读/写延迟与 GCP 中的公共路由和专用服务连接端点相同

问题描述 投票:0回答:1

我在 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);

我从云外壳实例尝试一下。

我的 VM shell 的输出

Cloud shell 的输出

我想实现云外壳延迟(〜80毫秒),在GCP中是否可能,如果是的话如何实现?

node.js google-cloud-firestore google-cloud-endpoints google-cloud-shell
1个回答
0
投票

奋斗了2天后我才知道

我的脚本没有连接到同一区域的 firestore,而是连接到不同区域(asia-south1)。这就是延迟增加的原因。

当我在同一区域创建 firestore 和 VM 时,我的延迟读取 = 10 毫秒,写入 = 9 毫秒。

还是不确定

  1. 如何连接到特定区域的firestore数据库,它只能连接到默认数据库。
  2. 为什么它不尊重“databaseId:”和“databaseURL”变量?
© www.soinside.com 2019 - 2024. All rights reserved.