【问题标题】:How to query Firestore from a k6 script?如何从 k6 脚本查询 Firestore?
【发布时间】:2022-01-19 15:21:27
【问题描述】:

我们正在尝试通过集合上的 k6 脚本查询 Firestore。
为了集成 Firestore 库,我们在 k6 网站上遵循了此指南:
https://k6.io/docs/using-k6/modules/#setting-up-the-bundler

当我们运行我们的脚本时,我们得到了这个奇怪的错误,我们在 Firestore 文档中找不到:

@firebase/firestore: Firestore (8.1.2): AsyncQueue 初始化查询'Query(target=Target(my_collection_here, orderBy: [timestamp (asc), name (asc)]); limitType=F)' 失败:TypeError:值不是对象:未定义

脚本是这样的:

import 'core-js/stable';
import 'regenerator-runtime/runtime';
import firebase from 'firebase/app';
import 'firebase/firestore';
import { sleep } from 'k6';
import behavior from './tests/behaviors/index.js';
import scenariosAndStages from './tests/options/index.js';

const firebaseConfig = {
  apiKey: ***,
  authDomain: ***,
  projectId: ***,
  storageBucket: ***,
  messagingSenderId: ***,
  appId: ***,
};

firebase.initializeApp(firebaseConfig);
const db = firebase.firestore();

const conf = {
  multiply: __ENV.MULTIPLY || 1,
};

export const options = {
  ext: {
    loadimpact: {
      name: __ENV.NAME,
      projectID: __ENV.PROJECT_ID,
      distribution: {
        'amazon:us:ashburn': { loadZone: 'amazon:us:ashburn', percent: 100 },
      },
    },
  },
  discardResponseBodies: true,
  scenarios: __ENV.SCENARIOS ? { [__ENV.SCENARIOS]: scenariosAndStages.scenarios[__ENV.SCENARIOS] } : undefined,
  stages: scenariosAndStages.stages[__ENV.STAGES],
};

export default () => {
  db
    .collection('my_collection_here')
    .orderBy('timestamp')
    .get()
    .then(console.log);
  sleep(10);
  behavior.myBehavior('application load test one call', conf.multiply);
};

我们做错了什么?

【问题讨论】:

  • k6 是用来测试的,对吧?您是使用 emulator 执行此操作,还是直接针对您的 Firestore 服务器进行测试?
  • 直接针对 Firestore 服务器
  • 嗯。好的,好吧,我建议使用模拟器进行大多数测试,因为它至少可以为你节省几美元(更有可能是几美分,除非你犯了一个大错误)来测试本地仿真而不是你的真正的服务器。就是说...我有点好奇this line,有限的兼容性位。我不确定firestore是否依赖于NodeJS本身......但我从来没有在node之外使用过这个包,所以我得再看看,也许问问几个同事。
  • 好的,是的,Firestore requires Node.js,所以由于 k6 和 Firestore 的要求,它们不兼容。我会将其发布为答案。

标签: javascript firebase google-cloud-firestore k6


【解决方案1】:

经过一些简单的研究,我意识到Firestore JS SDK requires Node.js 为了正常工作,根据k6's documentation 使其与k6 不兼容。

我会推荐 unit testing 的 Firebase 模拟器。如果有人更愿意为了测试目的模拟某些函数,我建议改用 Jest,如图所示here

【讨论】:

    猜你喜欢
    • 2022-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-13
    • 2020-01-23
    • 2017-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多