loopbackio / loopback-next

LoopBack makes it easy to build modern API applications that require complex integrations.
https://loopback.io
Other
4.95k stars 1.07k forks source link

Spike: why are relation tests so slow and how to fix that #6017

Closed bajtos closed 3 years ago

bajtos commented 4 years ago

See https://github.com/strongloop/loopback-next/issues/5670. Most of the slow tests (taking more than a second to complete) are for the relations.

Let's investigate why are these tests so slow. Do they have expensive setup? Can we find a more efficient way how to test the relation generator?

Acceptance criteria

agnes512 commented 4 years ago

As I commented in https://github.com/strongloop/loopback-next/issues/6131#issuecomment-679297030. The way we check pk and fk in lb4 relation couldn't detect model inheritance.

agnes512 commented 4 years ago

Current relation CLI main building relation process:

Prompt:

  1. Select relation type
  2. Select source/target models[read]
  3. Check if corresponding repos exist [read]
  4. Check if the source model pk exists [read]
  5. Prompt for foreign key
  6. Check if the fk exists
  7. Prompt for getter/relation name
  8. Check if the relation name exists [read]
  9. Prompt for inclusion resolver

Scaffold:

  1. Check the type of the fk [read]
  2. Check if the decorator needs to be changed [read]
  3. Update models [write]
  4. Update repos [write]
  5. Create a controller and update index file [write]

Current design pros and cons.

Read files frequently slows down the process. That's why some tests take a while to run. For example, test case lb4 relation generates model relation for existing property name verifies that a preexisting property will be overwritten: 2675ms creates both hasMany and blongsTo relations, which makes it extremely slow.

PROS:

CONS:

Proposal

IMO the relation CLI should keep those checks and validations. lb4 relation is one of the basic commands. We should make sure it fails invalid cases and generate valid relations for users (esp new users). Therefore, it should preserve those validations. For that reason, I propose the following to improve the test speed:

  context('generates model relation with custom relation name', () => {
    const promptArray = [
      {
        relationType: 'belongsTo',
        sourceModel: 'Order',
        destinationModel: 'Customer',
        foreignKeyName: 'customerId',
        relationName: 'my_customer',
      },
      {
        relationType: 'belongsTo',
        sourceModel: 'OrderClass',
        destinationModel: 'CustomerClass',
        relationName: 'my_customer',
      },
    ];
    ...
jannyHou commented 4 years ago
  1. remove redundant ones

If they don't decrease the coverage, or we have a good reason not testing them, they should definitely be removed.

  1. Refactor the code. Try to just read corresponding files once.

👍

agnes512 commented 3 years ago

Closing as done, see https://github.com/strongloop/loopback-next/pull/6937