Skip to content

Commit

Permalink
Update benchmark scripts to new interfaces (#4873)
Browse files Browse the repository at this point in the history
  • Loading branch information
timleslie authored Feb 17, 2021
1 parent 2655c0b commit de3e982
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 80 deletions.
5 changes: 5 additions & 0 deletions .changeset/healthy-spiders-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystonejs/benchmarks': minor
---

Updated benchmark scripts to use the new keystone interfaces.
51 changes: 27 additions & 24 deletions tests/benchmarks/fixtures/create-related.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,49 @@
const { Text, Relationship } = require('@keystonejs/fields');
const { setupServer } = require('@keystonejs/test-utils');
const { text, relationship } = require('@keystone-next/fields');
const { createSchema, list } = require('@keystone-next/keystone/schema');
const { setupFromConfig } = require('@keystonejs/test-utils');
const { FixtureGroup, timeQuery, populate, range } = require('../lib/utils');

function setupKeystone(adapterName) {
return setupServer({
return setupFromConfig({
adapterName,
createLists: keystone => {
keystone.createList('User', {
fields: {
name: { type: Text },
posts: { type: Relationship, ref: 'Post', many: true },
},
});
keystone.createList('Post', {
fields: {
title: { type: Text },
},
});
},
config: createSchema({
lists: {
User: list({
fields: {
name: text(),
posts: relationship({ ref: 'Post', many: true }),
},
}),
Post: list({
fields: {
title: text(),
},
}),
},
}),
});
}

const group = new FixtureGroup(setupKeystone);

group.add({
fn: async ({ keystone, adapterName }) => {
fn: async ({ context, adapterName }) => {
const query = `
mutation {
createUser(data: { name: "test", posts: { create: [] } }) { id }
}`;
const { time, success } = await timeQuery({ keystone, query });
const { time, success } = await timeQuery({ context, query });
console.log({ adapterName, time, success, name: 'Cold create with relationship, N=1' });
},
});

group.add({
fn: async ({ keystone, adapterName }) => {
fn: async ({ context, adapterName }) => {
const query = `
mutation {
createUser(data: { name: "test", posts: { create: [] } }) { id }
}`;
const { time, success } = await timeQuery({ keystone, query });
const { time, success } = await timeQuery({ context, query });
console.log({ adapterName, time, success, name: 'Warm create with relationship, N=1' });
},
});
Expand All @@ -49,14 +52,14 @@ range(14).forEach(i => {
const N = 1;
const M = 2 ** i;
group.add({
fn: async ({ keystone, adapterName }) => {
fn: async ({ context, adapterName }) => {
const query = `
mutation createMany($users: [UsersCreateInput]){
createUsers(data: $users) { id }
}`;
const posts = { create: populate(M, i => ({ title: `post${i}` })) };
const variables = { users: populate(N, i => ({ data: { name: `test${i}`, posts } })) };
const { time, success } = await timeQuery({ keystone, query, variables });
const { time, success } = await timeQuery({ context, query, variables });
console.log({
adapterName,
time,
Expand All @@ -72,14 +75,14 @@ range(k).forEach(i => {
const N = 2 ** i;
const M = 2 ** (k - 1 - i);
group.add({
fn: async ({ keystone, adapterName }) => {
fn: async ({ context, adapterName }) => {
const query = `
mutation createMany($users: [UsersCreateInput]){
createUsers(data: $users) { id }
}`;
const posts = { create: populate(M, i => ({ title: `post${i}` })) };
const variables = { users: populate(N, i => ({ data: { name: `test${i}`, posts } })) };
const { time, success } = await timeQuery({ keystone, query, variables });
const { time, success } = await timeQuery({ context, query, variables });
console.log({
adapterName,
time,
Expand Down
35 changes: 19 additions & 16 deletions tests/benchmarks/fixtures/create.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,57 @@
const { Text } = require('@keystonejs/fields');
const { setupServer } = require('@keystonejs/test-utils');
const { text } = require('@keystone-next/fields');
const { createSchema, list } = require('@keystone-next/keystone/schema');
const { setupFromConfig } = require('@keystonejs/test-utils');
const { FixtureGroup, timeQuery, populate, range } = require('../lib/utils');

function setupKeystone(adapterName) {
return setupServer({
return setupFromConfig({
adapterName,
createLists: keystone => {
keystone.createList('User', {
fields: {
name: { type: Text },
},
});
},
config: createSchema({
lists: {
User: list({
fields: {
name: text(),
},
}),
},
}),
});
}

const group = new FixtureGroup(setupKeystone);

group.add({
fn: async ({ keystone, adapterName }) => {
fn: async ({ context, adapterName }) => {
const query = `
mutation {
createUser(data: { name: "test" }) { id }
}`;
const { time, success } = await timeQuery({ keystone, query });
const { time, success } = await timeQuery({ context, query });
console.log({ adapterName, time, success, name: 'Cold create, N=1' });
},
});

group.add({
fn: async ({ keystone, adapterName }) => {
fn: async ({ context, adapterName }) => {
const query = `
mutation {
createUser(data: { name: "test" }) { id }
}`;
const { time, success } = await timeQuery({ keystone, query });
const { time, success } = await timeQuery({ context, query });
console.log({ adapterName, time, success, name: 'Warm create, N=1' });
},
});

range(15).forEach(i => {
const N = 2 ** i;
group.add({
fn: async ({ keystone, adapterName }) => {
fn: async ({ context, adapterName }) => {
const query = `
mutation createMany($users: [UsersCreateInput]){
createUsers(data: $users) { id }
}`;
const variables = { users: populate(N, i => ({ data: { name: `test${i}` } })) };
const { time, success } = await timeQuery({ keystone, query, variables });
const { time, success } = await timeQuery({ context, query, variables });
console.log({ adapterName, time, success, name: `Create-many, N=${N}` });
},
});
Expand Down
85 changes: 47 additions & 38 deletions tests/benchmarks/fixtures/query.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,55 @@
const { Text, Relationship } = require('@keystonejs/fields');
const { text, relationship } = require('@keystone-next/fields');
const { createSchema, list } = require('@keystone-next/keystone/schema');
const { setupFromConfig } = require('@keystonejs/test-utils');
const { createItem, createItems } = require('@keystonejs/server-side-graphql-client');
const { setupServer } = require('@keystonejs/test-utils');
const { FixtureGroup, timeQuery, populate, range } = require('../lib/utils');

function setupKeystone(adapterName) {
return setupServer({
return setupFromConfig({
adapterName,
createLists: keystone => {
keystone.createList('User', {
fields: {
name: { type: Text },
posts: { type: Relationship, ref: 'Post', many: true },
},
});
keystone.createList('Post', {
fields: {
title: { type: Text },
},
});
},
config: createSchema({
lists: {
User: list({
fields: {
name: text(),
posts: relationship({ ref: 'Post', many: true }),
},
}),
Post: list({
fields: {
title: text(),
},
}),
},
}),
});
}

const group = new FixtureGroup(setupKeystone);

group.add({
fn: async ({ keystone, adapterName }) => {
fn: async ({ context, adapterName }) => {
const { id: userId } = await createItem({
keystone,
context,
listKey: 'User',
item: { name: 'test', posts: { create: [] } },
});
const query = `query getPost($userId: ID!) { User(where: { id: $userId }) { id } }`;
const { time, success } = await timeQuery({ keystone, query, variables: { userId } });
const { time, success } = await timeQuery({ context, query, variables: { userId } });
console.log({ adapterName, time, success, name: 'Cold read with relationship, N=1' });
},
});

group.add({
fn: async ({ keystone, adapterName }) => {
fn: async ({ context, adapterName }) => {
const { id: userId } = await createItem({
keystone,
context,
listKey: 'User',
item: { name: 'test', posts: { create: [] } },
});
const query = `query getUser($userId: ID!) { User(where: { id: $userId }) { id } }`;
const { time, success } = await timeQuery({
keystone,
context,
query,
variables: { userId },
repeat: 1000,
Expand All @@ -57,16 +62,17 @@ range(14).forEach(i => {
const N = 1;
const M = 2 ** i;
group.add({
fn: async ({ keystone, adapterName }) => {
fn: async ({ context, adapterName }) => {
const posts = { create: populate(M, i => ({ title: `post${i}` })) };
const users = await createItems({
keystone,
item: populate(N, i => ({ data: { name: `test${i}`, posts } })),
context,
listKey: 'User',
items: populate(N, i => ({ data: { name: `test${i}`, posts } })),
});
const userId = users[0].id;
const query = `query getUser($userId: ID!) { User(where: { id: $userId }) { id } }`;
const { time, success } = await timeQuery({
keystone,
context,
query,
variables: { userId },
repeat: 1000,
Expand All @@ -86,16 +92,17 @@ range(k).forEach(i => {
const N = 2 ** i;
const M = 2 ** (k - 1 - i);
group.add({
fn: async ({ keystone, adapterName }) => {
fn: async ({ context, adapterName }) => {
const posts = { create: populate(M, i => ({ title: `post${i}` })) };
const users = await createItems({
keystone,
item: populate(N, i => ({ data: { name: `test${i}`, posts } })),
context,
listKey: 'User',
items: populate(N, i => ({ data: { name: `test${i}`, posts } })),
});
const userId = users[0].id;
const query = `query getUser($userId: ID!) { User(where: { id: $userId }) { id } }`;
const { time, success } = await timeQuery({
keystone,
context,
query,
variables: { userId },
repeat: 1000,
Expand All @@ -114,16 +121,17 @@ range(14).forEach(i => {
const N = 1;
const M = 2 ** i;
group.add({
fn: async ({ keystone, adapterName }) => {
fn: async ({ context, adapterName }) => {
const posts = { create: populate(M, i => ({ title: `post${i}` })) };
const users = await createItems({
keystone,
item: populate(N, i => ({ data: { name: `test${i}`, posts } })),
context,
listKey: 'User',
items: populate(N, i => ({ data: { name: `test${i}`, posts } })),
});
const userId = users[0].id;
const query = `query getUser($userId: ID!) { User(where: { id: $userId }) { id posts { id } } }`;
const { time, success } = await timeQuery({
keystone,
context,
query,
variables: { userId },
repeat: 100,
Expand All @@ -142,16 +150,17 @@ range(k).forEach(i => {
const N = 2 ** i;
const M = 2 ** (k - 1 - i);
group.add({
fn: async ({ keystone, adapterName }) => {
fn: async ({ context, adapterName }) => {
const posts = { create: populate(M, i => ({ title: `post${i}` })) };
const users = await createItems({
keystone,
item: populate(N, i => ({ data: { name: `test${i}`, posts } })),
context,
listKey: 'User',
items: populate(N, i => ({ data: { name: `test${i}`, posts } })),
});
const userId = users[0].id;
const query = `query getUser($userId: ID!) { User(where: { id: $userId }) { id posts { id } } }`;
const { time, success } = await timeQuery({
keystone,
context,
query,
variables: { userId },
repeat: 100,
Expand Down
4 changes: 2 additions & 2 deletions tests/benchmarks/lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const { multiAdapterRunners } = require('@keystonejs/test-utils');
const { runCustomQuery } = require('@keystonejs/server-side-graphql-client');

const timeQuery = async ({ keystone, query, variables, repeat = 1 }) => {
const timeQuery = async ({ context, query, variables, repeat = 1 }) => {
const t0_us = process.hrtime.bigint();
const allErrors = [];
for (let i = 0; i < repeat; i++) {
try {
await runCustomQuery({ keystone, query, variables });
await runCustomQuery({ context, query, variables });
} catch (error) {
allErrors.push(error);
}
Expand Down
2 changes: 2 additions & 0 deletions tests/benchmarks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
"repository": "https://github.com/keystonejs/keystone/tree/master/tests/benchmarks",
"homepage": "https://github.com/keystonejs/keystone",
"dependencies": {
"@keystone-next/fields": "^5.0.0",
"@keystone-next/keystone": "^10.0.0",
"@keystonejs/adapter-knex": "^13.0.1",
"@keystonejs/adapter-mongoose": "^11.0.1",
"@keystonejs/app-graphql": "^6.2.1",
Expand Down

1 comment on commit de3e982

@vercel
Copy link

@vercel vercel bot commented on de3e982 Feb 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.