Skip to content

Commit 21c0b41

Browse files
committed
fix: perf
1 parent 5206d89 commit 21c0b41

File tree

2 files changed

+0
-159
lines changed

2 files changed

+0
-159
lines changed

packages/auth/src/auth.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import {
2020
} from 'better-auth/plugins';
2121
import { Resend } from 'resend';
2222
import { ac, admin, member, owner, viewer } from './permissions';
23-
import { performancePlugin } from './plugins';
2423

2524
function isProduction() {
2625
return process.env.NODE_ENV === 'production';
@@ -270,7 +269,6 @@ export const auth = betterAuth({
270269
});
271270
},
272271
}),
273-
performancePlugin(),
274272
],
275273
});
276274

packages/auth/src/plugins.ts

Lines changed: 0 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -1,157 +0,0 @@
1-
import type { BetterAuthPlugin } from 'better-auth';
2-
import { APIError, createAuthEndpoint } from 'better-auth/api';
3-
import { z } from 'zod';
4-
import type { User } from '@/lib/auth';
5-
export const performancePlugin = () => {
6-
return {
7-
id: 'performance',
8-
endpoints: {
9-
initiatePerformanceTest: createAuthEndpoint(
10-
'/performance/initiate',
11-
{
12-
method: 'POST',
13-
body: z.object({
14-
rawQueryAllUsers: z.function(),
15-
rawQueryOneUser: z.function().args(z.string()),
16-
}),
17-
},
18-
async (ctx) => {
19-
const results = [];
20-
for await (const _ of Array(10)) {
21-
const result = await startTest();
22-
results.push(result);
23-
}
24-
25-
// Calculate averages
26-
const averages = {
27-
usersDuration:
28-
results.reduce((sum, r) => sum + r.usersDuration, 0) /
29-
results.length,
30-
rawUsersDuration:
31-
results.reduce((sum, r) => sum + r.rawUsersDuration, 0) /
32-
results.length,
33-
firstUserDuration:
34-
results.reduce((sum, r) => sum + r.firstUserDuration, 0) /
35-
results.length,
36-
rawFirstUserDuration:
37-
results.reduce((sum, r) => sum + r.rawFirstUserDuration, 0) /
38-
results.length,
39-
};
40-
41-
// Display results table
42-
console.log('\n=== PERFORMANCE TEST RESULTS ===');
43-
console.table([
44-
{
45-
'Test Type': 'All Users',
46-
'Better-Auth': Number.parseFloat(
47-
averages.usersDuration.toFixed(2)
48-
),
49-
Raw: Number.parseFloat(averages.rawUsersDuration.toFixed(2)),
50-
},
51-
{
52-
'Test Type': 'Single User',
53-
'Better-Auth': Number.parseFloat(
54-
averages.firstUserDuration.toFixed(2)
55-
),
56-
Raw: Number.parseFloat(averages.rawFirstUserDuration.toFixed(2)),
57-
},
58-
]);
59-
console.log(`\nTests completed: ${results.length}`);
60-
console.log('===============================================\n');
61-
62-
async function startTest() {
63-
const { rawQueryAllUsers, rawQueryOneUser } = ctx.body;
64-
const logger = ctx.context.logger;
65-
const adapter = ctx.context.adapter;
66-
logger.warn(
67-
"Warning, if you're seeing this in production, please remove the performance plugin unless it's intentional."
68-
);
69-
70-
if (ctx.request) {
71-
throw new APIError('FORBIDDEN', {
72-
message:
73-
'This endpoint is only available to call from the server.',
74-
});
75-
}
76-
77-
// BA adapter query all users
78-
logger.info('Starting performance test...');
79-
logger.info('--------------------------------');
80-
81-
const { data: users, duration: usersDuration } = await timeAction(
82-
async () => {
83-
const users = await adapter.findMany<User>({
84-
model: 'user',
85-
});
86-
return users;
87-
}
88-
);
89-
logger.info(
90-
`BETTER-AUTH: Fetched ${users.length} users in ${usersDuration}ms`
91-
);
92-
if (!users.length) {
93-
throw new APIError('INTERNAL_SERVER_ERROR', {
94-
message: 'This test requires at least one user to be present.',
95-
});
96-
}
97-
98-
// raw query all users
99-
const { data: rawUsers, duration: rawUsersDuration } =
100-
await timeAction(async () => {
101-
const users = (await rawQueryAllUsers()) as User[];
102-
return users;
103-
});
104-
logger.info(
105-
`RAW QUERY: Fetched ${rawUsers.length} users in ${rawUsersDuration}ms`
106-
);
107-
108-
logger.info('--------------------------------');
109-
110-
// BA adapter query one user
111-
const { data: firstUser, duration: firstUserDuration } =
112-
await timeAction(async () => {
113-
const user = await adapter.findOne<User>({
114-
model: 'user',
115-
where: [{ field: 'id', value: users[0].id }],
116-
});
117-
return user;
118-
});
119-
logger.info(
120-
`BETTER-AUTH: Fetched an individual user in ${firstUserDuration}ms`
121-
);
122-
123-
// raw query one user
124-
const { data: rawFirstUser, duration: rawFirstUserDuration } =
125-
await timeAction(async () => {
126-
const user = (await rawQueryOneUser(users[0].id)) as User;
127-
return user;
128-
});
129-
logger.info(
130-
`RAW QUERY: Fetched an individual user in ${rawFirstUserDuration}ms`
131-
);
132-
133-
logger.info('--------------------------------');
134-
135-
await new Promise((resolve) => setTimeout(resolve, 1000));
136-
return {
137-
usersDuration,
138-
rawUsersDuration,
139-
firstUserDuration,
140-
rawFirstUserDuration,
141-
};
142-
}
143-
}
144-
),
145-
},
146-
} satisfies BetterAuthPlugin;
147-
};
148-
149-
async function timeAction<T>(
150-
action: () => Promise<T>
151-
): Promise<{ data: T; duration: number }> {
152-
const start = performance.now();
153-
const result = await action();
154-
const end = performance.now();
155-
const duration = end - start;
156-
return { data: result, duration };
157-
}

0 commit comments

Comments
 (0)