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 15:39:12 +01:00
committed by GitHub
parent 3b2c929408
commit 42d9c7222b
71 changed files with 1204 additions and 439 deletions
@@ -29,7 +29,7 @@ describe("/admin/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 () => {
@@ -26,7 +26,7 @@ describe("/admin/draft-orders", () => {
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 () => {
@@ -29,7 +29,6 @@ describe("[MEDUSA_FF_TAX_INCLUSIVE_PRICING] /admin/draft-orders", () => {
const [process, connection] = await startServerWithEnvironment({
cwd,
env: { MEDUSA_FF_TAX_INCLUSIVE_PRICING: true },
verbose: false,
})
dbConnection = connection
@@ -40,7 +40,6 @@ describe("[MEDUSA_FF_ORDER_EDITING] /admin/order-edits", () => {
const [process, connection] = await startServerWithEnvironment({
cwd,
env: { MEDUSA_FF_ORDER_EDITING: true },
verbose: false,
})
dbConnection = connection
medusaProcess = process
@@ -10,8 +10,8 @@ const adminSeeder = require("../../../helpers/admin-seeder")
const {
simpleRegionFactory,
simpleShippingOptionFactory,
simpleOrderFactory
} = require("../../../factories");
simpleOrderFactory,
} = require("../../../factories")
jest.setTimeout(30000)
@@ -24,7 +24,6 @@ describe("[MEDUSA_FF_TAX_INCLUSIVE_PRICING] /admin/orders", () => {
const [process, connection] = await startServerWithEnvironment({
cwd,
env: { MEDUSA_FF_TAX_INCLUSIVE_PRICING: true },
verbose: false,
})
dbConnection = connection
medusaProcess = process
@@ -50,7 +49,7 @@ describe("[MEDUSA_FF_TAX_INCLUSIVE_PRICING] /admin/orders", () => {
country_code: "us",
}
const region = await simpleRegionFactory(dbConnection, {
id: "test-region"
id: "test-region",
})
order = await simpleOrderFactory(dbConnection, {
id: "test-order",
@@ -58,16 +57,19 @@ describe("[MEDUSA_FF_TAX_INCLUSIVE_PRICING] /admin/orders", () => {
shipping_address: shippingAddress,
currency_code: "usd",
})
includesTaxShippingOption = await simpleShippingOptionFactory(dbConnection, {
includes_tax: true,
region_id: region.id
})
includesTaxShippingOption = await simpleShippingOptionFactory(
dbConnection,
{
includes_tax: true,
region_id: region.id,
}
)
} catch (err) {
console.log(err)
}
})
afterEach(async() => {
afterEach(async () => {
const db = useDb()
return await db.teardown()
})
@@ -76,26 +78,27 @@ describe("[MEDUSA_FF_TAX_INCLUSIVE_PRICING] /admin/orders", () => {
const api = useApi()
const orderWithShippingMethodRes = await api.post(
`/admin/orders/${order.id}/shipping-methods`,
{
option_id: includesTaxShippingOption.id,
price: 10,
`/admin/orders/${order.id}/shipping-methods`,
{
option_id: includesTaxShippingOption.id,
price: 10,
},
{
headers: {
Authorization: "Bearer test_token",
},
{
headers: {
Authorization: "Bearer test_token",
},
}
}
)
expect(orderWithShippingMethodRes.status).toEqual(200)
expect(orderWithShippingMethodRes.data.order.shipping_methods)
.toEqual(expect.arrayContaining([
expect(orderWithShippingMethodRes.data.order.shipping_methods).toEqual(
expect.arrayContaining([
expect.objectContaining({
shipping_option_id: includesTaxShippingOption.id,
includes_tax: true,
})
]))
}),
])
)
})
})
})
@@ -33,7 +33,7 @@ describe("/admin/orders", () => {
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_ORDER_EDITING] /admin/payment-collections", () => {
const [process, connection] = await startServerWithEnvironment({
cwd,
env: { MEDUSA_FF_ORDER_EDITING: true },
verbose: true,
})
dbConnection = connection
medusaProcess = process
@@ -31,7 +31,6 @@ describe("[MEDUSA_FF_ORDER_EDITING] /admin/payment", () => {
const [process, connection] = await startServerWithEnvironment({
cwd,
env: { MEDUSA_FF_ORDER_EDITING: true },
verbose: false,
})
dbConnection = connection
medusaProcess = process
@@ -1415,7 +1415,6 @@ describe("[MEDUSA_FF_TAX_INCLUSIVE_PRICING] /admin/price-lists", () => {
const [process, connection] = await startServerWithEnvironment({
cwd,
env: { MEDUSA_FF_TAX_INCLUSIVE_PRICING: true },
verbose: false,
})
dbConnection = connection
medusaProcess = process
@@ -36,7 +36,9 @@ describe("/admin/products", () => {
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 () => {
@@ -43,7 +43,6 @@ describe("[MEDUSA_FF_PUBLISHABLE_API_KEYS] Publishable API keys", () => {
MEDUSA_FF_PUBLISHABLE_API_KEYS: true,
MEDUSA_FF_SALES_CHANNELS: true,
},
verbose: false,
})
dbConnection = connection
medusaProcess = process
+17 -15
View File
@@ -2,11 +2,12 @@ const path = require("path")
const { Region } = require("@medusajs/medusa")
const setupServer = require("../../../helpers/setup-server")
const startServerWithEnvironment = require("../../../helpers/start-server-with-environment").default
const startServerWithEnvironment =
require("../../../helpers/start-server-with-environment").default
const { useApi } = require("../../../helpers/use-api")
const { initDb, useDb } = require("../../../helpers/use-db")
const adminSeeder = require("../../helpers/admin-seeder")
const { simpleRegionFactory } = require("../../factories");
const { simpleRegionFactory } = require("../../factories")
const adminReqConfig = {
headers: {
@@ -304,7 +305,6 @@ describe("[MEDUSA_FF_TAX_INCLUSIVE_PRICING] /admin/regions", () => {
const [process, connection] = await startServerWithEnvironment({
cwd,
env: { MEDUSA_FF_TAX_INCLUSIVE_PRICING: true },
verbose: false,
})
dbConnection = connection
medusaProcess = process
@@ -364,7 +364,7 @@ describe("[MEDUSA_FF_TAX_INCLUSIVE_PRICING] /admin/regions", () => {
name: "region-including-taxes",
})
)
});
})
it("should allow to update a region that includes tax", async function () {
const api = useApi()
@@ -376,17 +376,19 @@ describe("[MEDUSA_FF_TAX_INCLUSIVE_PRICING] /admin/regions", () => {
expect(response.data.region.includes_tax).toBe(false)
response = await api.post(
`/admin/regions/${region1TaxInclusiveId}`,
{
includes_tax: true,
},
adminReqConfig,
).catch((err) => {
console.log(err)
})
response = await api
.post(
`/admin/regions/${region1TaxInclusiveId}`,
{
includes_tax: true,
},
adminReqConfig
)
.catch((err) => {
console.log(err)
})
expect(response.data.region.includes_tax).toBe(true)
});
})
})
})
})
@@ -15,7 +15,7 @@ describe("/admin/return-reasons", () => {
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 () => {
@@ -34,7 +34,6 @@ describe("sales channels", () => {
const [process, connection] = await startServerWithEnvironment({
cwd,
env: { MEDUSA_FF_SALES_CHANNELS: true },
verbose: false,
})
dbConnection = connection
medusaProcess = process
@@ -1,15 +1,17 @@
const path = require("path")
const {
ShippingProfile,
} = require("@medusajs/medusa")
const { ShippingProfile } = require("@medusajs/medusa")
const setupServer = require("../../../helpers/setup-server")
const startServerWithEnvironment = require("../../../helpers/start-server-with-environment").default
const startServerWithEnvironment =
require("../../../helpers/start-server-with-environment").default
const { useApi } = require("../../../helpers/use-api")
const { initDb, useDb } = require("../../../helpers/use-db")
const adminSeeder = require("../../helpers/admin-seeder")
const shippingOptionSeeder = require("../../helpers/shipping-option-seeder")
const { simpleShippingOptionFactory, simpleRegionFactory } = require("../../factories")
const {
simpleShippingOptionFactory,
simpleRegionFactory,
} = require("../../factories")
const adminReqConfig = {
headers: {
@@ -475,7 +477,6 @@ describe("[MEDUSA_FF_TAX_INCLUSIVE_PRICING] /admin/shipping-options", () => {
const [process, connection] = await startServerWithEnvironment({
cwd,
env: { MEDUSA_FF_TAX_INCLUSIVE_PRICING: true },
verbose: false,
})
dbConnection = connection
medusaProcess = process
@@ -517,9 +518,12 @@ describe("[MEDUSA_FF_TAX_INCLUSIVE_PRICING] /admin/shipping-options", () => {
it("should creates a shipping option that includes tax", async () => {
const api = useApi()
const defaultProfile = await dbConnection.manager.findOne(ShippingProfile, {
type: "default",
})
const defaultProfile = await dbConnection.manager.findOne(
ShippingProfile,
{
type: "default",
}
)
const payload = {
name: "Test option",
@@ -551,7 +555,10 @@ describe("[MEDUSA_FF_TAX_INCLUSIVE_PRICING] /admin/shipping-options", () => {
const api = useApi()
let response = await api
.get(`/admin/shipping-options/${shippingOptionIncludesTaxId}`, adminReqConfig)
.get(
`/admin/shipping-options/${shippingOptionIncludesTaxId}`,
adminReqConfig
)
.catch((err) => {
console.log(err)
})
@@ -573,7 +580,11 @@ describe("[MEDUSA_FF_TAX_INCLUSIVE_PRICING] /admin/shipping-options", () => {
}
response = await api
.post(`/admin/shipping-options/${shippingOptionIncludesTaxId}`, payload, adminReqConfig)
.post(
`/admin/shipping-options/${shippingOptionIncludesTaxId}`,
payload,
adminReqConfig
)
.catch((err) => {
console.log(err)
})
@@ -363,7 +363,6 @@ describe("/admin/swaps", () => {
const [process, connection] = await startServerWithEnvironment({
cwd,
env: { MEDUSA_FF_TAX_INCLUSIVE_PRICING: true },
verbose: false,
})
dbConnection = connection
medusaProcess = process
@@ -28,7 +28,7 @@ describe("/admin/users", () => {
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 () => {