Skip to content

Commit

Permalink
mysql
Browse files Browse the repository at this point in the history
  • Loading branch information
lroal committed Jul 15, 2024
1 parent b0b5f65 commit 9959d42
Show file tree
Hide file tree
Showing 26 changed files with 4,050 additions and 58 deletions.
13 changes: 12 additions & 1 deletion .devcontainer/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ services:
build:
context: .
dockerfile: Dockerfile
command: /bin/sh -c "sed -i 's/localhost:15432/postgres/g' /workspace/.env && sleep infinity"
command: /bin/sh -c "sed -i 's/localhost:15432/postgres/g' /workspace/.env && sed -i 's/mysql on localhost:13306/mysql/g' /workspace/.env && sleep infinity"
volumes:
- ..:/workspace:cached
- try-node-node_modules:/workspace/node_modules
depends_on:
- postgres
- mysql
postgres:
image: postgres
environment:
Expand All @@ -17,5 +18,15 @@ services:
ports:
- 15432:5432

mysql:
image: mysql:8.0.31
environment:
- MYSQL_ROOT_PASSWORD=test
- MYSQL_DATABASE=test
- MYSQL_USER=test
- MYSQL_PASSWORD=test
ports:
- 13306:3306

volumes:
try-node-node_modules:
3 changes: 2 additions & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
ITERATIONS=50
ROUNDS=10
LOG=false
POSTGRES_URL="postgres://postgres:postgres@localhost:15432/postgres"
POSTGRES_URL="postgres://postgres:postgres@localhost:15432/postgres"
MYSQL_URL="mysql://test:test@localhost:13306/test"
29 changes: 15 additions & 14 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,20 @@ jobs:
# ports:
# - 1433:1433

# mysql:
# image: mysql:8.0.31
# env:
# MYSQL_DATABASE: test
# MYSQL_USER: test
# MYSQL_PASSWORD: test
# MYSQL_ROOT_PASSWORD: test
# ports:
# - 3306:3306
# options: >-
# --health-cmd="mysqladmin ping"
# --health-interval=10s
# --health-timeout=5s
# --health-retries=3
mysql:
image: mysql:8.0.31
env:
MYSQL_DATABASE: test
MYSQL_USER: test
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: test
ports:
- 3306:3306
options: >-
--health-cmd="mysqladmin ping"
--health-interval=10s
--health-timeout=5s
--health-retries=3
steps:
- uses: actions/checkout@v4
Expand All @@ -62,5 +62,6 @@ jobs:
with:
node-version: ${{ matrix.node-version }}
- run: sed -i 's/localhost:15432/postgres/g' .env
- run: sed -i 's/localhost:13306/mysql/g' .env
- run: npm install
- run: npm run benchmark
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ The CPU had 4 physical cores, and each core supports 2 threads, making a total o
| Drizzle| 31.847 s | 26.013 s |
| Prisma | 49.419 s | 33.730 s |
| Mikro | 2:05.736 min | 1:58.967 min|

**MySQL**
| | Pool size = 1 | Pool size = 10 |
|--------|---------------|----------------|
| Orange | 16.925 s | 18.858 s |
| Drizzle| 7:26.233 min | 7:31.768 min |
| Prisma | 46.560 s | 32.646 s |
| Mikro | 1:42.455 min | 1:43.959 min|
## Steps to Run

The github action will always run remotely on push.
Expand Down
3,317 changes: 3,317 additions & 0 deletions data/init-mysql.sql

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions data/init-postgres.sql
Original file line number Diff line number Diff line change
Expand Up @@ -194,15 +194,15 @@ INSERT INTO customers(id,company_name,contact_name,contact_title,address,city,re
,('WOLZA','Wolski Zajazd','Zbyszek Piestrzeniewicz','Owner','ul. Filtrowa 68','Warszawa',NULL,'01-012','Poland','(26) 642-7012','(26) 642-7012');

INSERT INTO employees(id,last_name,first_name,title,title_of_courtesy,birth_date,hire_date,address,city,postal_code,country,home_phone,extension,notes,recipient_id) VALUES
('2','Fuller','Andrew','Vice President, Sales','Dr.','2/19/1952','8/14/1992','908 W. Capital Way','Tacoma','98401','USA','(206) 555-9482',3457,'Andrew received his BTS commercial in 1974 and a Ph.D. in international marketing from the University of Dallas in 1981. He is fluent in French and Italian and reads German. He joined the company as a sales representative was promoted to sales manager in January 1992 and to vice president of sales in March 1993. Andrew is a member of the Sales Management Roundtable the Seattle Chamber of Commerce and the Pacific Rim Importers Association.',NULL)
,('5','Buchanan','Steven','Sales Manager','Mr.','3/4/1955','10/17/1993','14 Garrett Hill','London','98401','SW1 8JR','(71) 555-4848',3453,'Steven Buchanan graduated from St. Andrews University Scotland with a BSC degree in 1976. Upon joining the company as a sales representative in 1992 he spent 6 months in an orientation program at the Seattle office and then returned to his permanent post in London. He was promoted to sales manager in March 1993. Mr. Buchanan has completed the courses "Successful Telemarketing" and "International Sales Management." He is fluent in French.',2)
,('1','Davolio','Nancy','Sales Representative','Ms.','12/8/1948','5/1/1992','507 - 20th Ave. E.Apt. 2A','Seattle','98122','USA','(206) 555-9857',5467,'Education includes a BA in psychology from Colorado State University in 1970. She also completed "The Art of the Cold Call." Nancy is a member of Toastmasters International.',2)
,('3','Leverling','Janet','Sales Representative','Ms.','8/30/1963','4/1/1992','722 Moss Bay Blvd.','Kirkland','98033','USA','(206) 555-3412',3355,'Janet has a BS degree in chemistry from Boston College (1984). She has also completed a certificate program in food retailing management. Janet was hired as a sales associate in 1991 and promoted to sales representative in February 1992.',2)
,('4','Peacock','Margaret','Sales Representative','Mrs.','9/19/1937','5/3/1993','4110 Old Redmond Rd.','Redmond','98052','USA','(206) 555-8122',5176,'Margaret holds a BA in English literature from Concordia College (1958) and an MA from the American Institute of Culinary Arts (1966). She was assigned to the London office temporarily from July through November 1992.',2)
,('6','Suyama','Michael','Sales Representative','Mr.','7/2/1963','10/17/1993','Coventry House Miner Rd.','London','EC2 7JR','UK','(71) 555-7773',428,'Michael is a graduate of Sussex University (MA, economics, 1983) and the University of California at Los Angeles (MBA, marketing, 1986). He has also taken the courses "Multi-Cultural Selling" and "Time Management for the Sales Professional." He is fluent in Japanese and can read and write French, Portuguese, and Spanish.',5)
,('7','King','Robert','Sales Representative','Mr.','5/29/1960','1/2/1994','Edgeham Hollow Winchester Way','London','RG1 9SP','UK','(71) 555-5598',465,'Robert King served in the Peace Corps and traveled extensively before completing his degree in English at the University of Michigan in 1992, the year he joined the company. After completing a course entitled "Selling in Europe',5)
,('8','Callahan','Laura','Inside Sales Coordinator','Ms.','3/9/1982','4/1/1996','4726 - 11th Ave. N.E.','Seattle','98105','USA','(206) 555-1189',2344,'Laura received a BA in psychology from the University of Washington. She has also completed a course in business French. She reads and writes French.',2)
,('9','Dodsworth','Anne','Sales Representative','Ms.','1/27/1998','11/15/2026','7 Houndstooth Rd.','London','WG2 7LT','UK','(71) 555-4444',452,'Anne has a BA degree in English from St. Lawrence College. She is fluent in French and German.',5);
('2','Fuller','Andrew','Vice President, Sales','Dr.','1952-02-19','1992-08-14','908 W. Capital Way','Tacoma','98401','USA','(206) 555-9482',3457,'Andrew received his BTS commercial in 1974 and a Ph.D. in international marketing from the University of Dallas in 1981. He is fluent in French and Italian and reads German. He joined the company as a sales representative was promoted to sales manager in January 1992 and to vice president of sales in March 1993. Andrew is a member of the Sales Management Roundtable the Seattle Chamber of Commerce and the Pacific Rim Importers Association.',NULL),
('5','Buchanan','Steven','Sales Manager','Mr.','1955-03-04','1993-10-17','14 Garrett Hill','London','98401','SW1 8JR','(71) 555-4848',3453,'Steven Buchanan graduated from St. Andrews University Scotland with a BSC degree in 1976. Upon joining the company as a sales representative in 1992 he spent 6 months in an orientation program at the Seattle office and then returned to his permanent post in London. He was promoted to sales manager in March 1993. Mr. Buchanan has completed the courses "Successful Telemarketing" and "International Sales Management." He is fluent in French.',2),
('1','Davolio','Nancy','Sales Representative','Ms.','1948-12-08','1992-05-01','507 - 20th Ave. E.Apt. 2A','Seattle','98122','USA','(206) 555-9857',5467,'Education includes a BA in psychology from Colorado State University in 1970. She also completed "The Art of the Cold Call." Nancy is a member of Toastmasters International.',2),
('3','Leverling','Janet','Sales Representative','Ms.','1963-08-30','1992-04-01','722 Moss Bay Blvd.','Kirkland','98033','USA','(206) 555-3412',3355,'Janet has a BS degree in chemistry from Boston College (1984). She has also completed a certificate program in food retailing management. Janet was hired as a sales associate in 1991 and promoted to sales representative in February 1992.',2),
('4','Peacock','Margaret','Sales Representative','Mrs.','1937-09-19','1993-05-03','4110 Old Redmond Rd.','Redmond','98052','USA','(206) 555-8122',5176,'Margaret holds a BA in English literature from Concordia College (1958) and an MA from the American Institute of Culinary Arts (1966). She was assigned to the London office temporarily from July through November 1992.',2),
('6','Suyama','Michael','Sales Representative','Mr.','1963-07-02','1993-10-17','Coventry House Miner Rd.','London','EC2 7JR','UK','(71) 555-7773',428,'Michael is a graduate of Sussex University (MA, economics, 1983) and the University of California at Los Angeles (MBA, marketing, 1986). He has also taken the courses "Multi-Cultural Selling" and "Time Management for the Sales Professional." He is fluent in Japanese and can read and write French, Portuguese, and Spanish.',5),
('7','King','Robert','Sales Representative','Mr.','1960-05-29','1994-01-02','Edgeham Hollow Winchester Way','London','RG1 9SP','UK','(71) 555-5598',465,'Robert King served in the Peace Corps and traveled extensively before completing his degree in English at the University of Michigan in 1992, the year he joined the company. After completing a course entitled "Selling in Europe',5),
('8','Callahan','Laura','Inside Sales Coordinator','Ms.','1982-03-09','1996-04-01','4726 - 11th Ave. N.E.','Seattle','98105','USA','(206) 555-1189',2344,'Laura received a BA in psychology from the University of Washington. She has also completed a course in business French. She reads and writes French.',2),
('9','Dodsworth','Anne','Sales Representative','Ms.','1998-01-27','2026-11-15','7 Houndstooth Rd.','London','WG2 7LT','UK','(71) 555-4444',452,'Anne has a BA degree in English from St. Lawrence College. She is fluent in French and German.',5);

INSERT INTO orders(id,customer_id,employee_id,order_date,required_date,shipped_date,ship_via,freight,ship_name,ship_city,ship_region,ship_postal_code,ship_country) VALUES
('10248','VINET','5','1996-07-04 00:00:00.000','1996-08-01 00:00:00.000','1996-07-16 00:00:00.000',3,32.38,'Vins et alcools Chevalier','Reims',NULL,'51100','France')
Expand Down
58 changes: 41 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,64 @@
"type": "module",
"devDependencies": {
"@types/node": "^20.14.2",
"prisma": "^5.15.1",
"prisma": "^5.16.2",
"ts-node": "^10.9.2",
"tsx": "^4.15.1",
"typescript": "^5.4.5"
},
"scripts": {
"docker": "npm run start-db",
"db:start": "docker run -d -p 15432:5432 -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=postgres --name my_postgres postgres",
"db:stop": "docker stop my_postgres && docker rm my_postgres",
"init": "cross-env POOLSIZE=1 tsx ./src/initPostgres.ts",
"benchmark": " prisma generate --schema=./src/prisma/schema.prisma && npm run drizzle && npm run orange && npm run prisma && npm run mikro",
"drizzle": "npm run drizzle:pool1 && npm run drizzle:pool10",
"drizzle:pool1": "npm run init && cross-env POOLSIZE=1 tsx ./src/drizzle/benchmark-pg.ts",
"drizzle:pool10": "npm run init && cross-env POOLSIZE=10 tsx ./src/drizzle/benchmark-pg.ts",
"orange": "npm run orange:pool1 && npm run orange:pool10",
"orange:pool1": "npm run init && cross-env POOLSIZE=1 tsx ./src/orange/benchmark-pg.ts",
"orange:pool10": "npm run init && cross-env POOLSIZE=10 tsx ./src/orange/benchmark-pg.ts",
"prisma": "npm run prisma:pool1 && npm run prisma:pool10",
"prisma:pool1": "npm run init && cross-env POOLSIZE=1 tsx ./src/prisma/benchmark-pg.ts",
"prisma:pool10": "npm run init && cross-env POOLSIZE=10 tsx ./src/prisma/benchmark-pg.ts",
"mikro": "npm run mikro:pool1 && npm run mikro:pool10",
"mikro:pool1": "npm run init && cross-env POOLSIZE=1 tsx ./src/mikro/benchmark-pg.ts",
"mikro:pool10": "npm run init && cross-env POOLSIZE=10 tsx ./src/mikro/benchmark-pg.ts",
"db:start": "npm run db:start:mysql && npm run db:start:pg",
"db:start:mysql": "docker run -d -p 13306:3306 -e MYSQL_ROOT_PASSWORD=test -e MYSQL_DATABASE=test -e MYSQL_USER=test -e MYSQL_PASSWORD=test --name my_mysql mysql:8.0.31",
"db:start:pg": "docker run -d -p 15432:5432 -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=postgres --name my_pg postgres",
"db:stop:mysql": "docker stop my_mysql && docker rm my_mysql",
"db:stop:pg": "docker stop my_pg && docker rm my_pg",
"db:stop": "docker stop my_pg && docker rm my_pg",
"init": "npm run init:pg & npm run init:mysql",
"init:pg": "cross-env POOLSIZE=1 tsx ./src/init-pg.ts",
"init:mysql": "cross-env POOLSIZE=1 tsx ./src/init-mysql.ts",
"benchmark": "npm run drizzle && npm run orange && npm run prisma && npm run mikro",
"drizzle": "npm run drizzle:mysql && npm run drizzle:pg",
"drizzle:mysql": "npm run drizzle:pool1:mysql && npm run drizzle:pool10:mysql",
"drizzle:pg": "npm run drizzle:pool1:pg && npm run drizzle:pool10:pg",
"drizzle:pool1:mysql": "npm run init:mysql && cross-env POOLSIZE=1 tsx ./src/drizzle/benchmark-mysql.ts",
"drizzle:pool10:mysql": "npm run init:mysql && cross-env POOLSIZE=10 tsx ./src/drizzle/benchmark-mysql.ts",
"drizzle:pool1:pg": "npm run init:pg && cross-env POOLSIZE=1 tsx ./src/drizzle/benchmark-pg.ts",
"drizzle:pool10:pg": "npm run init:pg && cross-env POOLSIZE=10 tsx ./src/drizzle/benchmark-pg.ts",
"orange": "npm run orange:mysql && npm run orange:pg",
"orange:pg": "npm run orange:pool1:pg && npm run orange:pool10:pg",
"orange:mysql": "npm run orange:pool1:mysql && npm run orange:pool10:mysql",
"orange:pool1:mysql": "npm run init:mysql && cross-env POOLSIZE=1 tsx ./src/orange/benchmark-mysql.ts",
"orange:pool10:mysql": "npm run init:mysql && cross-env POOLSIZE=10 tsx ./src/orange/benchmark-mysql.ts",
"orange:pool1:pg": "npm run init:pg && cross-env POOLSIZE=1 tsx ./src/orange/benchmark-pg.ts",
"orange:pool10:pg": "npm run init:pg && cross-env POOLSIZE=10 tsx ./src/orange/benchmark-pg.ts",
"prisma": "npm run prisma:mysql && npm run prisma:pg",
"prisma:mysql": "npm run init:mysql && prisma generate --schema=./src/prisma/schema-mysql.prisma && cross-env POOLSIZE=1 tsx ./src/prisma/benchmark-mysql.ts && cross-env POOLSIZE=10 tsx ./src/prisma/benchmark-mysql.ts",
"prisma:pg": "npm run init:pg && prisma generate --schema=./src/prisma/schema-pg.prisma && cross-env POOLSIZE=1 tsx ./src/prisma/benchmark-pg.ts && cross-env POOLSIZE=10 tsx ./src/prisma/benchmark-pg.ts",
"prisma:pool1:mysql": "npm run init:mysql && prisma generate --schema=./src/prisma/schema-mysql.prisma && cross-env POOLSIZE=1 tsx ./src/prisma/benchmark-mysql.ts",
"prisma:pool10:mysql": "npm run init:mysql && prisma generate --schema=./src/prisma/schema-mysql.prisma && cross-env POOLSIZE=10 tsx ./src/prisma/benchmark-mysql.ts",
"prisma:pool1:pg": "npm run init:pg && prisma generate --schema=./src/prisma/schema-pg.prisma && cross-env POOLSIZE=1 tsx ./src/prisma/benchmark-pg.ts",
"prisma:pool10:pg": "npm run init:pg && prisma generate --schema=./src/prisma/schema-pg.prisma && cross-env POOLSIZE=10 tsx ./src/prisma/benchmark-pg.ts",
"mikro": "npm run mikro:mysql && npm run mikro:pg",
"mikro:mysql": "npm run mikro:pool1:mysql && npm run mikro:pool10:mysql",
"mikro:pg": "npm run mikro:pool1:pg && npm run mikro:pool10:pg",
"mikro:pool1:mysql": "npm run init:mysql && cross-env POOLSIZE=1 tsx ./src/mikro/benchmark-mysql.ts",
"mikro:pool10:mysql": "npm run init:mysql && cross-env POOLSIZE=10 tsx ./src/mikro/benchmark-mysql.ts",
"mikro:pool1:pg": "npm run init:pg && cross-env POOLSIZE=1 tsx ./src/mikro/benchmark-pg.ts",
"mikro:pool10:pg": "npm run init:pg && cross-env POOLSIZE=10 tsx ./src/mikro/benchmark-pg.ts",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"@mikro-orm/core": "^6.2.9",
"@mikro-orm/mysql": "^6.2.9",
"@mikro-orm/postgresql": "^6.2.9",
"@prisma/client": "^5.16.2",
"cross-env": "^7.0.3",
"dotenv": "^16.4.5",
"drizzle-orm": "^0.32.0",
"mysql2": "^3.10.2",
"orange-orm": "^4.4.0",
"pg": "^8.12.0",
"postgres": "^3.4.4",
Expand Down
56 changes: 56 additions & 0 deletions src/drizzle/benchmark-mysql.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { exit } from 'node:process';
import db from './mysql'

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

benchmark();

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

async function warmup() {
//to initate possible lazy loaded pool
const promises = [];
for (let i = 0; i < ITERATIONS; i++) {
promises.push(db.query.customers.findFirst());
}
await Promise.all(promises);
}

async function getRowsWithRelations() {
const promises = [];
for (let i = 0; i < ITERATIONS; i++) {
const p = db.query.orders.findMany({
with: {
details: {
with: {
product: {
with: {
supplier: {

}
}
}
}
},
customer: {},
employee: {},
}
// }).then(x => console.dir(x, {depth: Infinity}));
}).then(JSON.stringify);
promises.push(p);
}
await Promise.all(promises);
}



6 changes: 3 additions & 3 deletions src/drizzle/benchmark-pg.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { exit } from 'node:process';
import postgres from './postgres'
import db from './pg'

const ITERATIONS = Number.parseInt(process.env.ITERATIONS);
const ROUNDS = Number.parseInt(process.env.ROUNDS);
Expand All @@ -21,15 +21,15 @@ async function warmup() {
//to initate possible lazy loaded pool
const promises = [];
for (let i = 0; i < ITERATIONS; i++) {
promises.push(postgres.query.customers.findFirst());
promises.push(db.query.customers.findFirst());
}
await Promise.all(promises);
}

async function getRowsWithRelations() {
const promises = [];
for (let i = 0; i < ITERATIONS; i++) {
const p = postgres.query.orders.findMany({
const p = db.query.orders.findMany({
with: {
details: {
with: {
Expand Down
24 changes: 24 additions & 0 deletions src/drizzle/mysql.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { drizzle } from 'drizzle-orm/mysql2';
import mysql from 'mysql2/promise';
import * as schema from './schema-mysql';
import dotenv from 'dotenv';

dotenv.config();

const POOLSIZE = Number.parseInt(process.env.POOLSIZE, 10);
const LOG = process.env.LOG === 'true';

const pool = mysql.createPool({
uri: process.env.MYSQL_URL,
connectionLimit: POOLSIZE,
});

export const db = drizzle(pool, {
schema,
mode: 'default',
logger: LOG,
});

export type db = typeof db;

export default db;
2 changes: 1 addition & 1 deletion src/drizzle/postgres.ts → src/drizzle/pg.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { drizzle } from 'drizzle-orm/postgres-js';
import postgres from "postgres";
import * as schema from './schema';
import * as schema from './schema-pg';
import dotenv from 'dotenv';
dotenv.config();
const POOLSIZE = Number.parseInt(process.env.POOLSIZE);
Expand Down
Loading

0 comments on commit 9959d42

Please sign in to comment.