firebase-tools: Very slow initial query to Firestore emulator

[REQUIRED] Environment info

firebase-tools: 9.13.0

Platform: macOS, Ubuntu

[REQUIRED] Test case

import * as firebaseTesting from '@firebase/rules-unit-testing';
import type firebase from 'firebase';

let iteration = 0;
let projectId: string;
let testApp: firebase.app.App;

beforeEach(async () => {
    iteration += 1;
    projectId = `test-${iteration}`;

    testApp = firebaseTesting.initializeTestApp({
        projectId,
        databaseName: projectId,
        // @ts-ignore
        storageBucket: projectId,
    });

    testApp.firestore().settings({
        ignoreUndefinedProperties: true,
        // Merge settings to not override settings from initializeTestApp
        merge: true,
    });
});

describe('Firestore initial query', () => {
    it('should be long for the first one', async () => {
        console.time('firstQuery-initial');
        await testApp.firestore().collection('some-collection').doc('some-doc').get();
        console.timeEnd('firstQuery-initial');
        // firstQuery-initial: 8437 ms

        console.time('firstQuery-hot');
        await testApp.firestore().collection('some-collection').doc('some-doc').get();
        console.timeEnd('firstQuery-hot');
        // firstQuery-hot: 36 ms
    });

    it('should be quicker for the second one', async () => {
        console.time('secondQuery');
        await testApp.firestore().collection('some-collection').doc('some-doc').get();
        console.timeEnd('secondQuery');
        // secondQuery: 3055 ms
    });
});

[REQUIRED] Steps to reproduce

Run the test case provided above with the Firestore emulator and actual data.

[REQUIRED] Expected behavior

As you can see from the logs in the tests above, the very first query .get() to the Firestore emulator takes a huge amount of time: around 8s on my mac M1 machine. As reflected in the following logs, when making a subsequent request in the same test that uses the same test app, it only takes a few milliseconds (36 when I ran it). Finally, as reflected by the second test case, subsequents queries .get() using another Firebase test app takes around 3s.

From these tests, it seems that there are multiple things at stake while running the emulator:

  • The initial call made by a new Firebase test app to the Firestore emulator, evaluated to around 3s. It looks to me like it could be related to a kind of authentication process for the new app before making new requests. But that still looks pretty long for an emulator. Are there any actual HTTP requests being sent to the actual Firestore infrastructure that could explain this latency?
  • The very first connection made to the Firestore emulator by whichever Firebase test app makes this initial request, evaluated to 8 (total time) - 3 (first query for a new app) = 5 seconds, which is even longer.

We noticed this because one of our tests was failing because we use a timeout of 8 seconds for the test. To test the actual behavior of our function, we thus had to make a first initial request to make sure that when our test happens, the Firestore emulator was “hot” and this behavior didn’t affect our test results.

Is there any specific reason why both of these steps take so much time?

[REQUIRED] Actual behavior

It would be great to reduce this latency to the minimum. Given that we are using a new Firebase test app for almost all of our tests, our test suite takes at least <number-of-tests> * 3s to be processed, which is definitely not sustainable on the long run.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 2
  • Comments: 15 (9 by maintainers)

Most upvoted comments

Friendly ping @bkendall following @emmerich’s comment that provides more details. It’d be great to reopen the issue for further investigation.