Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Next.js implementation of the TechEmpower Fortunes benchmark #1866

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ bld/
[Bb]in/
[Oo]bj/
[Ll]og/
dist/

# Visual Studio 2015 cache/options directory
.vs/
Expand Down
3 changes: 3 additions & 0 deletions src/BenchmarksApps/TechEmpower/nextjs/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "next/core-web-vitals"
}
35 changes: 35 additions & 0 deletions src/BenchmarksApps/TechEmpower/nextjs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env*.local

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts
58 changes: 58 additions & 0 deletions src/BenchmarksApps/TechEmpower/nextjs/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
FROM node:18-alpine AS base

# Install dependencies only when needed
FROM base AS deps
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
WORKDIR /app

# Install dependencies based on the preferred package manager
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
RUN \
if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
elif [ -f package-lock.json ]; then npm ci; \
elif [ -f pnpm-lock.yaml ]; then yarn global add pnpm && pnpm i --frozen-lockfile; \
else echo "Lockfile not found." && exit 1; \
fi

# Rebuild the source code only when needed
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .

# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry during the build.
ENV NEXT_TELEMETRY_DISABLED 1

#RUN yarn build

# If using npm comment out above and use below instead
RUN npm run build

# Production image, copy all the files and run next
FROM base AS runner
WORKDIR /app

ENV NODE_ENV production
# Uncomment the following line in case you want to disable telemetry during runtime.
ENV NEXT_TELEMETRY_DISABLED 1

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

# Copy files required to run the app
#COPY --from=builder --chown=nextjs:nodejs /app/publi[c] ./public
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
COPY --from=builder --chown=nextjs:nodejs /app/dist/server.js ./cluster.js

USER nextjs

EXPOSE 3000

ENV PORT 3000
ENV DB_HOST postgres_te

CMD ["node", "cluster.js"]
13 changes: 13 additions & 0 deletions src/BenchmarksApps/TechEmpower/nextjs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Fortunes implementation using Next.js

This is an implementation of the [TechEmpower Fortunes benchmark](https://github.com/TechEmpower/FrameworkBenchmarks/wiki/Project-Information-Framework-Tests-Overview#fortunes) using Next.js

Run the app by executing `npm run dev` in the app root.

The app requires a Postgres database based on the [TechEmpower Postgres Docker image](https://github.com/TechEmpower/FrameworkBenchmarks/tree/master/toolset/databases/postgres). Clone the [TechEmpower repo](https://github.com/TechEmpower/FrameworkBenchmarks), navigate to `./toolset/databases/postgres`, and run `docker build -f .\postgres.dockerfile -t postgres_te .` to build a container from that image, then `docker run -p 5432:5432 --name postgres_te postgres_te`.

*NOTE: Currently using Next.js version 13.4.0 rather than latest due to [this issue](https://github.com/vercel/next.js/issues/48173#issuecomment-1584585902)*

The app is using a custom server to enable clustering (scaling node instances across CPUs) in production based on the [custom server example here](https://github.com/vercel/next.js/tree/canary/examples/custom-server).

The [Dockerfile](./Dockerfile) will build a standalone image (based on node-alpine) for running the app on port 3000. Note that in docker the host name for the Postgres database is set to `postgres_te`.
60 changes: 60 additions & 0 deletions src/BenchmarksApps/TechEmpower/nextjs/app/fortunes/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import 'server-only';
import os from 'node:os';
import { Pool } from "pg";
import { Fortune } from "./fortune";
import { env } from "process";

const noDb = env.NO_DB ? true : false;
let getFortunes;

if (noDb) {
console.log('Running in "no database" mode');
getFortunes = async function () {
var fortunes = [
new Fortune(1, "fortune: No such file or directory"),
new Fortune(2, "A computer scientist is someone who fixes things that aren't broken."),
new Fortune(3, "After enough decimal places, nobody gives a damn."),
new Fortune(4, "A bad random number generator: 1, 1, 1, 1, 1, 4.33e+67, 1, 1, 1"),
new Fortune(5, "A computer program does what you tell it to do, not what you want it to do."),
new Fortune(6, "Emacs is a nice operating system, but I prefer UNIX. — Tom Christaensen"),
new Fortune(7, "Any program that runs right is obsolete."),
new Fortune(8, "A list is only as strong as its weakest link. — Donald Knuth"),
new Fortune(9, "Feature: A bug with seniority."),
new Fortune(10, "Computers make very fast, very accurate mistakes."),
new Fortune(11, "<script>alert(\"This should not be displayed in a browser alert box.\");</script>"),
new Fortune(12, "フレームワークのベンチマーク"),
new Fortune(0, "Additional fortune added at request time.")
];
fortunes.sort((a, b) => a.message.localeCompare(b.message));
return fortunes;
}
} else {
const poolMaxClients = env.DB_MAX_CLIENTS ? parseInt(env.DB_MAX_CLIENTS) : os.cpus().length;
const dbHost = env.DB_HOST ?? "localhost";
const pool = new Pool({
user: "benchmarkdbuser",
password: "benchmarkdbpass",
database: "hello_world",
host: dbHost,
max: poolMaxClients
});
const queries = {
fortunes: {
name: "get-fortunes",
text: "SELECT * FROM fortune"
}
};
console.log(`Connected to database ${dbHost}`);
getFortunes = async function () {
const res = await pool.query(queries.fortunes);
var fortunes = res.rows.map(r => new Fortune(r.id, r.message));
//console.log(`${fortunes.length} rows read from database`);
fortunes.push(new Fortune(0, "Additional fortune added at request time."));
fortunes.sort((a, b) => a.message.localeCompare(b.message));
return fortunes;
}
}

export const db = {
getFortunes: getFortunes
};
9 changes: 9 additions & 0 deletions src/BenchmarksApps/TechEmpower/nextjs/app/fortunes/fortune.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export class Fortune {
constructor(id: number, message: string) {
this.id = id;
this.message = message;
}

id: number;
message: string;
}
21 changes: 21 additions & 0 deletions src/BenchmarksApps/TechEmpower/nextjs/app/fortunes/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { db } from "./db";
import { Metadata } from 'next'

// Force page to render dynamically from the server every request
export const dynamic = "force-dynamic";
export const revalidate = 0;
export const metadata: Metadata = { viewport: {} };

export default async function Page() {
const data = await db.getFortunes();
return (
<table>
<tbody>
<tr><th>id</th><th>message</th></tr>
{data.map((row, idx) =>
<tr key={idx}><td>{row.id}</td><td>{row.message}</td></tr>
)}
</tbody>
</table>
)
}
12 changes: 12 additions & 0 deletions src/BenchmarksApps/TechEmpower/nextjs/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html>
<head><title>Fortunes</title></head>
<body>{children}</body>
</html>
)
}
5 changes: 5 additions & 0 deletions src/BenchmarksApps/TechEmpower/nextjs/app/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { redirect } from 'next/navigation';

export default async function Page() {
redirect('/fortunes');
}
8 changes: 8 additions & 0 deletions src/BenchmarksApps/TechEmpower/nextjs/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/** @type {import('next').NextConfig} */
module.exports = {
output: "standalone",
reactStrictMode: true,
compress: false,
poweredByHeader: false,
generateEtags: false
}
55 changes: 55 additions & 0 deletions src/BenchmarksApps/TechEmpower/nextjs/nextjs.benchmarks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
imports:
- https://raw.githubusercontent.com/dotnet/crank/main/src/Microsoft.Crank.Jobs.Wrk/wrk.yml
- https://github.com/aspnet/Benchmarks/blob/main/scenarios/aspnet.profiles.standard.yml?raw=true

variables:
serverPort: 5000

jobs:
nextjs:
source:
repository: https://github.com/aspnet/benchmarks.git
branchOrCommit: main
dockerFile: src/BenchmarksApps/TechEmpower/nextjs/Dockerfile
dockerImageName: nextjsfortunes_te
dockerContextDirectory: src/BenchmarksApps/TechEmpower/nextjs
readyStateText: All workers listening.
variables:
serverScheme: http
environmentVariables:
HOSTNAME: "{{serverAddress}}"
PORT: "{{serverPort}}"
DB_HOST: "{{databaseServer}}"

postgresql:
source:
repository: https://github.com/TechEmpower/FrameworkBenchmarks.git
branchOrCommit: master
dockerFile: toolset/databases/postgres/postgres.dockerfile
dockerImageName: postgres_te
dockerContextDirectory: toolset/databases/postgres
readyStateText: ready to accept connections
noClean: true

scenarios:
fortunes:
db:
job: postgresql
application:
job: nextjs
load:
job: wrk
variables:
presetHeaders: html
path: /fortunes

profiles:
# this profile uses the local folder as the source
# instead of the public repository
source:
agents:
main:
source:
localFolder: .
respository: ''
dockerFile: Dockerfile
5 changes: 5 additions & 0 deletions src/BenchmarksApps/TechEmpower/nextjs/nodemon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"watch": ["server.ts"],
"exec": "ts-node --project tsconfig.server.json server.ts",
"ext": "js ts"
}
Loading