Skip to content

Commit fc7d7ba

Browse files
nelyousfiDSchau
authored andcommitted
fix(develop):Refactoring benchmark apps (#12433)
<!-- Have any questions? Check out the contributing docs at https://gatsby.dev/contribute, or ask in this Pull Request and a Gatsby maintainer will be happy to help :) --> ## Description - Add small refactoring to the benchmarks apps. - Fix benchmark query app warning: ``` warning Action createPage was called outside of its expected asynchronous lifecycle createPages in default-site-plugin. ``` ## Related Issues
1 parent 4f12ffa commit fc7d7ba

File tree

5 files changed

+37
-42
lines changed

5 files changed

+37
-42
lines changed

benchmarks/create-pages/gatsby-node.js

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
let NUM_PAGES = 5000
2-
3-
if (process.env.NUM_PAGES) {
4-
NUM_PAGES = process.env.NUM_PAGES
5-
}
1+
const NUM_PAGES = parseInt(process.env.NUM_PAGES || 5000, 10)
62

73
exports.createPages = ({ actions: { createPage } }) => {
8-
var step
9-
for (step = 0; step < NUM_PAGES; step++) {
4+
for (let step = 0; step < NUM_PAGES; step++) {
105
createPage({
116
path: `/path/${step}/`,
127
component: require.resolve(`./src/templates/blank.js`),

benchmarks/markdown/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Stress tests creating lots of pages rendered from Markdown.
44

5-
Defaults to building a site with 5k markdown pages. Set the `NUM_PAGES` environment variable to change that e.g. `NUM_PAGES=25000 gatsby build`
5+
Defaults to building a site with 5k markdown pages and 25 maximum rows per age. Set the `NUM_PAGES` and `MAX_NUM_ROWS` environment variables to change that e.g. `NUM_PAGES=25000 MAX_NUM_ROWS=100 gatsby build`
66

77
# Running the benchmark
88

benchmarks/markdown/gatsby-node.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ const template = require(`./page-template`)
33

44
exports.sourceNodes = ({ actions: { createNode } }) => {
55
// Create markdown nodes
6-
let step
7-
for (step = 0; step < NUM_PAGES; step++) {
6+
for (let step = 0; step < NUM_PAGES; step++) {
87
createNode({
98
id: step.toString(),
109
parent: null,

benchmarks/markdown/page-template.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const faker = require(`faker`)
22
const matter = require(`gray-matter`)
33

4-
const NUM_ROWS = parseInt(process.env.NUM_ROWS || 25, 10)
4+
const MAX_NUM_ROWS = parseInt(process.env.MAX_NUM_ROWS || 25, 10)
55

66
module.exports = index => `
77
${matter
@@ -17,7 +17,7 @@ ${matter
1717
1818
|Name|Description|Required|
1919
|:--:|-----------|--------|
20-
${new Array(faker.random.number(NUM_ROWS))
20+
${new Array(faker.random.number(MAX_NUM_ROWS))
2121
.fill(undefined)
2222
.map(() =>
2323
`

benchmarks/query/gatsby-node.js

+31-30
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,9 @@ const _ = require(`lodash`)
22
const faker = require(`faker`)
33
const fs = require(`fs`)
44

5-
let NUM_PAGES = 5000
6-
if (process.env.NUM_PAGES) {
7-
NUM_PAGES = process.env.NUM_PAGES
8-
}
5+
const NUM_PAGES = parseInt(process.env.NUM_PAGES || 5000, 10)
96

10-
let NUM_TYPES = 1
11-
if (process.env.NUM_TYPES) {
12-
NUM_TYPES = process.env.NUM_TYPES
13-
}
7+
const NUM_TYPES = parseInt(process.env.NUM_TYPES || 10, 10)
148

159
function newTypeName() {
1610
return _.capitalize(_.camelCase(faker.lorem.word()))
@@ -21,7 +15,7 @@ let types = []
2115
// Create NUM_PAGES nodes, split over NUM_TYPES types. Each node has
2216
// the bare minimum of content
2317
exports.sourceNodes = ({ actions: { createNode } }) => {
24-
for (var i = 0; i < NUM_TYPES; i++) {
18+
for (let i = 0; i < NUM_TYPES; i++) {
2519
types.push(newTypeName())
2620
}
2721
// Create markdown nodes
@@ -30,7 +24,7 @@ exports.sourceNodes = ({ actions: { createNode } }) => {
3024
let step = 0
3125

3226
_.forEach(types, typeName => {
33-
for (var i = 0; i < pagesPerType; i++) {
27+
for (let i = 0; i < pagesPerType; i++) {
3428
step++
3529
const id = `${typeName}${step.toString()}`
3630
createNode({
@@ -48,7 +42,7 @@ exports.sourceNodes = ({ actions: { createNode } }) => {
4842
})
4943
}
5044

51-
// Total hack. It would be nice if we could programatically generate
45+
// Total hack. It would be nice if we could programmatically generate
5246
// graphQL per component. But in the meantime, we just generate the
5347
// actual component js file with the graphql
5448
function createPageTemplateJs(typeName) {
@@ -90,28 +84,35 @@ function allTypeQuery(typeName) {
9084
`
9185
}
9286

93-
// Create a page for each node, and write out a new component js for
94-
// each different type to .cache/${typeName}Template.js
95-
async function createTypePages({ graphql, actions }, typeName) {
87+
// Create template in .cache for the received type
88+
function createTemplateFile(typeName) {
9689
const templateSrc = createPageTemplateJs(typeName)
9790
const templateFilename = `./.cache/${typeName}Template.js`
9891
fs.writeFileSync(templateFilename, templateSrc)
99-
let result = await graphql(allTypeQuery(typeName))
100-
_.forEach(result.data[`all${typeName}`].edges, edge => {
101-
const { node } = edge
102-
actions.createPage({
103-
path: `/${typeName}/${node.id}/`,
104-
component: require.resolve(templateFilename),
105-
context: {
106-
id: node.id,
107-
useQueryIndex: true,
108-
},
109-
})
110-
})
92+
return templateFilename
11193
}
11294

113-
exports.createPages = async args => {
114-
_.forEach(types, typeName => {
115-
createTypePages(args, typeName)
116-
})
95+
// Create node for the received type
96+
async function createNode(graphql, typeName) {
97+
const result = await graphql(allTypeQuery(typeName))
98+
return result.data[`all${typeName}`].edges
99+
}
100+
101+
// Create page for each type
102+
exports.createPages = async ({ actions, graphql }) => {
103+
for (let i = 0; i < types.length; i++) {
104+
const typeName = types[i]
105+
const templateFilename = createTemplateFile(typeName)
106+
const edges = await createNode(graphql, typeName)
107+
_.forEach(edges, ({ node }) => {
108+
actions.createPage({
109+
path: `/${typeName}/${node.id}/`,
110+
component: require.resolve(templateFilename),
111+
context: {
112+
id: node.id,
113+
useQueryIndex: true,
114+
},
115+
})
116+
})
117+
}
117118
}

0 commit comments

Comments
 (0)