diff --git a/benchmarks/source-drupal/.env.example b/benchmarks/source-drupal/.env.example index f5bf0e2680199..aa86da88b044b 100644 --- a/benchmarks/source-drupal/.env.example +++ b/benchmarks/source-drupal/.env.example @@ -1 +1,3 @@ BENCHMARK_DRUPAL_BASE_URL= +BENCHMARK_DRUPAL_USERNAME= +BENCHMARK_DRUPAL_PASSWORD= diff --git a/benchmarks/source-drupal/package.json b/benchmarks/source-drupal/package.json index 82a893571b15b..f2170496730c9 100644 --- a/benchmarks/source-drupal/package.json +++ b/benchmarks/source-drupal/package.json @@ -7,6 +7,7 @@ "scripts": { "build": "gatsby build", "build:send": "cross-env BENCHMARK_REPORTING_URL=true gatsby build", + "data-update": "ts-node scripts/data-update.ts", "develop": "gatsby develop", "format": "prettier --write \"**/*.{js,jsx,json,md}\"", "start": "npm run develop", @@ -14,6 +15,7 @@ }, "dependencies": { "dotenv": "^8.2.0", + "faker": "^4.1.0", "gatsby": "^2.19.7", "gatsby-image": "^2.2.40", "gatsby-plugin-sharp": "^2.4.5", @@ -21,8 +23,10 @@ "gatsby-source-filesystem": "^2.1.48", "gatsby-transformer-sharp": "^2.3.14", "lodash.kebabcase": "^4.1.1", + "node-fetch": "^2.6.0", "react": "^16.12.0", - "react-dom": "^16.12.0" + "react-dom": "^16.12.0", + "ts-node": "^8.6.2" }, "devDependencies": { "cross-env": "^7.0.0", diff --git a/benchmarks/source-drupal/scripts/data-update.ts b/benchmarks/source-drupal/scripts/data-update.ts new file mode 100644 index 0000000000000..f4ea6440c0f7a --- /dev/null +++ b/benchmarks/source-drupal/scripts/data-update.ts @@ -0,0 +1,11 @@ +#!/usr/bin/env node + +import { update } from "./updater" + +require(`dotenv`).config() + +const username = process.env.BENCHMARK_DRUPAL_USERNAME +const password = process.env.BENCHMARK_DRUPAL_PASSWORD +const server = process.env.BENCHMARK_DRUPAL_BASE_URL + +update(username, password, server) diff --git a/benchmarks/source-drupal/scripts/updater.ts b/benchmarks/source-drupal/scripts/updater.ts new file mode 100644 index 0000000000000..45ee621430169 --- /dev/null +++ b/benchmarks/source-drupal/scripts/updater.ts @@ -0,0 +1,62 @@ +import fetch from "node-fetch" +import faker from "faker" + +interface IArticle { + id: string + attributes: { + title: string + } +} + +// Remove last word of title and replace it with a random word. +const updateTitle = (title: string): string => + `${title.substring(0, title.lastIndexOf(` `))} ${faker.lorem.word()}` + +const patchArticle = async ( + username: string, + password: string, + server: string, + article: IArticle +): Promise => { + const url = `https://${server}/jsonapi/node/article/${article.id}` + + const response = await fetch(url, { + method: `PATCH`, + headers: { + "Content-Type": `application/vnd.api+json`, + Authorization: `Basic ${Buffer.from(`${username}:${password}`).toString( + `base64` + )}` + }, + body: JSON.stringify({ + data: { + type: `node--article`, + id: article.id, + attributes: { + title: updateTitle(article.attributes.title) + } + } + }) + }) +} + +const getFirstArticle = async (server: string): Promise => { + const url = `https://${server}/jsonapi/node/article?page[limit]=1&sort=created` + const response = await fetch(url) + const body = await response.json() + return body.data[0] +} + +export const update = async ( + username?: string, + password?: string, + server?: string +): Promise => { + if (!username || !password || !server) { + console.error(`You must pass username, password and server`) + return + } + + const article = await getFirstArticle(server) + await patchArticle(username, password, server, article) +} diff --git a/benchmarks/source-drupal/tsconfig.json b/benchmarks/source-drupal/tsconfig.json new file mode 100644 index 0000000000000..71e9a8b717d61 --- /dev/null +++ b/benchmarks/source-drupal/tsconfig.json @@ -0,0 +1,11 @@ +{ + "compilerOptions": { + "typeRoots": ["./node_modules/@types", "./typings"], + "target": "ES2018" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */, + "module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */, + "strict": true /* Enable all strict type-checking options. */, + "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */, + "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */, + "noImplicitAny": false + } +} \ No newline at end of file