feat(medusa): Performance improvements of Carts domain (#2648)

**What**

I have created a new method on the cart service which is `addLineItems`, allowing a user to add one or multiple items in an optimized way. Also updated the `generate` method from the line item service which now also accept a object data or a collection of data which. Various places have been optimized and cache support has been added to the price selection strategy.

The overall optimization allows to reach another 9000% improvement in the response time as a median (Creating a cart with 6 items):

|   | Min (ms)  | Median (ms)  | Max (ms)  | Median Improvement (%)
|---|:-:|---|---|---|
| Before optimisation  | 1200  | 9999 | 12698  |  N/A
| After optimisation | 63  | 252  | 500  | 39x
| After re optimisation | 56 | 82  | 399  | 121x
| After including addressed feedback | 65 | 202  | 495  | 49x

FIXES CORE-722
This commit is contained in:
Adrien de Peretti
2022-12-07 14:39:12 +00:00
committed by GitHub
parent 3b2c929408
commit 42d9c7222b
71 changed files with 1204 additions and 439 deletions
@@ -47,7 +47,7 @@ describe("/store/carts", () => {
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
dbConnection = await initDb({ cwd })
medusaProcess = await setupServer({ cwd, verbose: false })
medusaProcess = await setupServer({ cwd })
})
afterAll(async () => {
@@ -28,7 +28,6 @@ describe("[MEDUSA_FF_SALES_CHANNELS] /store/carts", () => {
const [process, connection] = await startServerWithEnvironment({
cwd,
env: { MEDUSA_FF_SALES_CHANNELS: true },
verbose: false,
})
dbConnection = connection
medusaProcess = process
@@ -27,7 +27,6 @@ describe("[MEDUSA_FF_TAX_INCLUSIVE_PRICING] /store/carts", () => {
const [process, connection] = await startServerWithEnvironment({
cwd,
env: { MEDUSA_FF_TAX_INCLUSIVE_PRICING: true },
verbose: false,
})
dbConnection = connection
medusaProcess = process
@@ -261,6 +260,25 @@ describe("[MEDUSA_FF_TAX_INCLUSIVE_PRICING] /store/carts", () => {
})
describe("with a cart with full tax inclusive variant pricing", () => {
const variantId1 = IdMap.getId("test-variant-1-tax-inclusive")
const variantId2 = IdMap.getId("test-variant-2-tax-inclusive")
const productId1 = IdMap.getId("test-product-1-tax-inclusive")
const productId2 = IdMap.getId("test-product-2-tax-inclusive")
const createCartPayload = {
region_id: regionId,
items: [
{
variant_id: variantId1,
quantity: 1,
},
{
variant_id: variantId2,
quantity: 1,
},
],
}
beforeEach(async () => {
await simpleRegionFactory(dbConnection, regionData)
await simpleProductFactory(
@@ -319,6 +337,25 @@ describe("[MEDUSA_FF_TAX_INCLUSIVE_PRICING] /store/carts", () => {
})
describe("with a cart mixing tax inclusive and exclusive variant pricing", () => {
const variantId1 = IdMap.getId("test-variant-1-mixed-tax-inclusive")
const variantId2 = IdMap.getId("test-variant-2-mixed-tax-inclusive")
const productId1 = IdMap.getId("test-product-1-mixed-tax-inclusive")
const productId2 = IdMap.getId("test-product-2-mixed-tax-inclusive")
const createCartPayload = {
region_id: regionId,
items: [
{
variant_id: variantId1,
quantity: 1,
},
{
variant_id: variantId2,
quantity: 1,
},
],
}
beforeEach(async () => {
await simpleRegionFactory(dbConnection, regionData)
await simpleProductFactory(
@@ -14,7 +14,7 @@ describe("/store/collections", () => {
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", ".."))
dbConnection = await initDb({ cwd })
medusaProcess = await setupServer({ cwd, verbose: true })
medusaProcess = await setupServer({ cwd })
})
afterAll(async () => {
@@ -19,7 +19,7 @@ describe("/store/customers", () => {
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", ".."))
dbConnection = await initDb({ cwd })
medusaProcess = await setupServer({ cwd, verbose: false })
medusaProcess = await setupServer({ cwd })
})
afterAll(async () => {
@@ -30,7 +30,6 @@ describe("[MEDUSA_FF_ORDER_EDITING] /store/order-edits", () => {
const [process, connection] = await startServerWithEnvironment({
cwd,
env: { MEDUSA_FF_ORDER_EDITING: true },
verbose: false,
})
dbConnection = connection
medusaProcess = process
@@ -31,7 +31,7 @@ describe("/store/carts", () => {
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", ".."))
dbConnection = await initDb({ cwd })
medusaProcess = await setupServer({ cwd, verbose: false })
medusaProcess = await setupServer({ cwd })
})
afterAll(async () => {
@@ -25,7 +25,6 @@ describe("[MEDUSA_FF_ORDER_EDITING] /store/payment-collections", () => {
const [process, connection] = await startServerWithEnvironment({
cwd,
env: { MEDUSA_FF_ORDER_EDITING: true },
verbose: false,
})
dbConnection = connection
medusaProcess = process
@@ -15,7 +15,7 @@ describe("/store/variants", () => {
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", ".."))
dbConnection = await initDb({ cwd })
medusaProcess = await setupServer({ cwd, verbose: false })
medusaProcess = await setupServer({ cwd })
})
afterAll(async () => {
@@ -31,7 +31,6 @@ describe("sales channels", () => {
const [process, connection] = await startServerWithEnvironment({
cwd,
env: { MEDUSA_FF_SALES_CHANNELS: true },
verbose: false,
})
dbConnection = connection
medusaProcess = process