簡體   English   中英

Google Cloud Functions +實時數據庫非常慢

[英]Google Cloud Functions + Realtime Database extremely slow

我正在Unity開發一款適用於移動設備的多人游戲。 我一直在使用GameSparks作為后端,但覺得它沒有給我控制我想要的項目。 環顧四周之后,我決定選擇Firebase。 感覺這是一個很好的決定,但現在我在數據庫+雲功能方面遇到了一些嚴重的緩慢

該游戲具有回合制模式,您可以在其中進行回合並在對手完成時獲得通知。 通過cloudcript調用上傳一個回合,只有必要的數據。 然后將新游戲狀態拼湊在一起,並更新游戲數據庫條目(activeCasualGames / {gameId})。 完成此操作后,我將更新gameInfo(activeCasualGamesInfo / {gameId})條目,其中包含有關游戲的一些基本信息,然后發送雲消息以通知對手輪到他們。

發送的數據僅為32 kb,並且代碼沒有進行復雜的更改(請參閱下面的雲功能代碼)。 此時游戲的完整數據庫條目最大約為100kb,但從發送回合到服務器和對手通知的游戲更新的時間范圍從50秒到超過一分鍾。 在GameSparks上,相同的操作可能需要幾秒鍾。

有沒有其他人在使用Firebase時遇到這種緩慢的問題? 我是否在雲腳本功能中犯了一些錯誤,或者這是我應該期待的性能? 游戲還有一個實時模式,我還沒有開始在Firebase中實現,並且由於這種延遲,我不覺得它會運行得太好

提前謝謝了!

exports.uploadCasualGameRound = functions.https.onCall(
  (roundUploadData, response) => {
    const roundData = JSON.parse(roundUploadData);

    const updatedGameData = JSON.parse(roundData.gameData);
    const updatedGameInfo = JSON.parse(roundData.gameInfo);

    console.log("game data object:");
    console.log(updatedGameData);

    console.log("game info:");
    console.log(updatedGameInfo);

    const gameId = updatedGameData.gameId;

    console.log("updating game with id: " + gameId);

    admin
      .database()
      .ref("/activeCasualGames/" + gameId)
      .once("value")
      .then(function(snapshot: { val: any }) {
        let game = snapshot.val();
        console.log("old game:");
        console.log(game);

        const isNewGame = (game as boolean) === true;

        if (isNewGame === false) {
          console.log("THIS IS AN EXISTING GAME!");
        } else {
          console.log("THIS IS A NEW GAME!");
        }

        // make the end state of the currently stored TurnBasedGameData the beginning state of the uploaded one (limits the upload of data and prevents timeout errors)
        if (isNewGame === true) {
          updatedGameData.gameStateRoundStart.playerOneRounds =
            updatedGameData.gameStateRoundEnd.playerOneRounds;
        } else {
          // Player one rounds are not uploaded by player two, and vice versa. Compensate for this here:
          if (updatedGameData.whoseTurn === updatedGameData.playerTwoId) {
            updatedGameData.gameStateRoundEnd.playerTwoRounds =
              game.gameStateRoundEnd.playerTwoRounds;
          } else {
            updatedGameData.gameStateRoundEnd.playerOneRounds =
              game.gameStateRoundEnd.playerOneRounds;
          }

          updatedGameData.gameStateRoundStart = game.gameStateRoundEnd;
        }

        game = updatedGameData;

        console.log("Game after update:");
        console.log(game);

        // game.lastRoundFinishedDate = Date.now();
        game.finalRoundWatchedByOtherPlayer = false;

        admin
          .database()
          .ref("/activeCasualGames/" + gameId)
          .set(game)
          .then(() => {
            updatedGameInfo.roundUploaded = true;
            return admin
              .database()
              .ref("/activeCasualGamesInfo/" + gameId)
              .set(updatedGameInfo)
              .then(() => {
               exports.sendOpponentFinishedCasualGameRoundMessage(gameId, updatedGameInfo.lastPlayer, updatedGameInfo.whoseTurn);
                return true;
              });
          });
      });
  }
);

使用Firebase雲功能+實時數據庫時,您必須考慮一些事項。 這個問題可能有助於解決無服務器性能問題,例如冷啟動 此外,我認為代碼本身可能會被重構,以減少對實時數據庫的調用,因為每次外部請求都需要時間。 一些提示是使用更多本地存儲的資源(在內存,瀏覽器緩存等)並使用一些全局變量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM