Skip to content

Commit

Permalink
mikro working
Browse files Browse the repository at this point in the history
  • Loading branch information
Lars-Erik Roald committed Jul 11, 2024
1 parent 0bd6357 commit dd0fc48
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 31 deletions.
44 changes: 26 additions & 18 deletions src/mikro/benchmark-pg.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
import 'reflect-metadata';
import { MikroORM, EntitySchema } from '@mikro-orm/core';
import { exit } from 'node:process';
import { MikroORM } from '@mikro-orm/core';
import { PostgreSqlDriver } from '@mikro-orm/postgresql';
import dotenv from 'dotenv';

import { Customer, CustomerSchema } from './schema';
import { Employee, EmployeeSchema } from './schema';
import { CustomerSchema } from './schema';
import { EmployeeSchema } from './schema';
import { Order, OrderSchema } from './schema';
import { OrderDetail, OrderDetailSchema } from './schema';
import { Product, ProductSchema } from './schema';
import { Supplier, SupplierSchema } from './schema';
import { OrderDetailSchema } from './schema';
import { ProductSchema } from './schema';
import { SupplierSchema } from './schema';

dotenv.config();
const ITERATIONS = Number.parseInt(process.env.ITERATIONS);
const ROUNDS = Number.parseInt(process.env.ROUNDS);
const POOLSIZE = Number.parseInt(process.env.POOLSIZE)

async function main() {
const orm = await MikroORM.init({
// metadataProvider: TSONMetadataProvider,
entities: [CustomerSchema, EmployeeSchema, OrderSchema, OrderDetailSchema, ProductSchema, SupplierSchema],
discovery: {
warnWhenNoEntities: true,
requireEntitiesArray: false,
alwaysAnalyseProperties: true,
},
// entities: [Customer, Employee, Order, OrderDetail, Product, Supplier],
dbName: 'your_db_name',
dbName: 'postgres',
driver: PostgreSqlDriver,
clientUrl: `${process.env.POSTGRES_URL}`,
pool: {
min: 1,
max: 1
min: POOLSIZE,
max: POOLSIZE
},
debug: false,
});
Expand All @@ -37,30 +38,37 @@ async function main() {

async function benchmark() {
await warmup();
await getRowsWithRelations();
console.time(`mikro:pool ${POOLSIZE}`);
for (let i = 0; i < ROUNDS; i++) {
await getRowsWithRelations();
}
console.timeEnd(`mikro:pool ${POOLSIZE}`)
await orm.close(true);
exit(0);
}

async function warmup() {
const em = orm.em.fork();
await em.find(Order, {}, { limit: 1 });
//to initate possible lazy loaded pool
const promises = [];
for (let i = 0; i < ITERATIONS; i++) {
promises.push(em.find(Order, {}, { limit: 1 }));
}
await Promise.all(promises);
}

async function getRowsWithRelations() {
const em = orm.em.fork();
console.time('mikroORM');
const promises = [];
for (let i = 0; i < ITERATIONS; i++) {
const p = em.find(Order, {}, {
populate: ['customer', 'employee', 'orderDetails.product.supplier'],
populate: ['customer', 'employee', 'orderDetails.product.supplier'],
}).then(JSON.stringify);
promises.push(p);
}
await Promise.all(promises);
console.timeEnd('mikroORM');
}


await orm.close(true);
}


Expand Down
32 changes: 19 additions & 13 deletions src/mikro/schema.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EntitySchema, Collection, type Rel } from '@mikro-orm/core';

// Customer entity
export class Customer {
export class Customer {
id!: string;
companyName!: string;
contactName!: string;
Expand All @@ -18,6 +18,7 @@ export class Customer {

export const CustomerSchema = new EntitySchema<Customer>({
name: 'Customer',
tableName: 'customers',
properties: {
id: { type: 'string', primary: true },
companyName: { type: 'string', fieldName: 'company_name' },
Expand Down Expand Up @@ -47,14 +48,15 @@ export class OrderDetail {

export const OrderDetailSchema = new EntitySchema<OrderDetail>({
name: 'OrderDetail',
tableName: 'order_details',
properties: {
unitPrice: { type: 'number', fieldName: 'unit_price' },
quantity: { type: 'number' },
discount: { type: 'number' },
orderId: { type: 'string', primary: true, fieldName: 'order_id' },
productId: { type: 'string', primary: true, fieldName: 'product_id' },
order: { kind: 'm:1', entity: () => Order },
product: { kind: 'm:1', entity: () => Product },
// orderId: { type: 'string', primary: true},
// productId: { type: 'string', primary: true },
order: { kind: 'm:1', entity: () => Order, fieldName: 'order_id', primary: true },
product: { kind: 'm:1', entity: () => Product, fieldName: 'product_id', primary: true },
},
});

Expand All @@ -80,6 +82,7 @@ export class Employee {

export const EmployeeSchema = new EntitySchema<Employee>({
name: 'Employee',
tableName: 'employees',
properties: {
id: { type: 'string', primary: true },
lastName: { type: 'string', fieldName: 'last_name' },
Expand Down Expand Up @@ -113,15 +116,16 @@ export class Order {
shipRegion?: string;
shipPostalCode?: string;
shipCountry!: string;
customerId!: string;
employeeId!: string;
// customerId!: string;
// employeeId!: string;
customer!: Rel<Customer>;
employee!: Rel<Employee>;
orderDetails = new Collection<OrderDetail>(this);
}

export const OrderSchema = new EntitySchema<Order>({
name: 'Order',
tableName: 'orders',
properties: {
id: { type: 'string', primary: true },
orderDate: { type: 'Date', fieldName: 'order_date' },
Expand All @@ -134,10 +138,10 @@ export const OrderSchema = new EntitySchema<Order>({
shipRegion: { type: 'string', nullable: true, fieldName: 'ship_region' },
shipPostalCode: { type: 'string', nullable: true, fieldName: 'ship_postal_code' },
shipCountry: { type: 'string', fieldName: 'ship_country' },
customerId: { type: 'string', fieldName: 'customer_id' },
employeeId: { type: 'string', fieldName: 'employee_id' },
customer: { kind: 'm:1', entity: () => Customer },
employee: { kind: 'm:1', entity: () => Employee },
// customerId: { type: 'string', fieldName: 'customer_id' },
// employeeId: { type: 'string' },
customer: { kind: 'm:1', entity: () => Customer, fieldName: 'customer_id' },
employee: { kind: 'm:1', entity: () => Employee, fieldName: 'employee_id' },
orderDetails: { kind: '1:m', entity: () => OrderDetail, mappedBy: 'order' },
},
});
Expand All @@ -159,6 +163,7 @@ export class Product {

export const ProductSchema = new EntitySchema<Product>({
name: 'Product',
tableName: 'products',
properties: {
id: { type: 'string', primary: true },
name: { type: 'string' },
Expand All @@ -168,8 +173,8 @@ export const ProductSchema = new EntitySchema<Product>({
unitsOnOrder: { type: 'number', fieldName: 'units_on_order' },
reorderLevel: { type: 'number', fieldName: 'reorder_level' },
discontinued: { type: 'number' },
supplierId: { type: 'string', fieldName: 'supplier_id' },
supplier: { kind: 'm:1', entity: () => Supplier },
// supplierId: { type: 'string', fieldName: 'supplier_id' },
supplier: { kind: 'm:1', entity: () => Supplier, fieldName: 'supplier_id' },
orderDetails: { kind: '1:m', entity: () => OrderDetail, mappedBy: 'product' },
},
});
Expand All @@ -191,6 +196,7 @@ export class Supplier {

export const SupplierSchema = new EntitySchema<Supplier>({
name: 'Supplier',
tableName: 'suppliers',
properties: {
id: { type: 'string', primary: true },
companyName: { type: 'string', fieldName: 'company_name' },
Expand Down

0 comments on commit dd0fc48

Please sign in to comment.