Skip to content

An AI-powered search engine with a generative UI

feat: add performance logging for optimization analysis

Yoshiki Miura3b71eb5cf8ec
diff --git a/app/api/chat/route.ts b/app/api/chat/route.ts
index dd86bb4f..462451e1 100644
--- a/app/api/chat/route.ts
+++ b/app/api/chat/route.ts
@@ -1,6 +1,7 @@
import { cookies } from 'next/headers'
import { getCurrentUserId } from '@/lib/auth/get-current-user'
+import { resetAllCounters } from '@/lib/utils/perf-tracking'
import { createChatStreamResponse } from '@/lib/streaming/create-chat-stream-response'
import { Model } from '@/lib/types/models'
import { isProviderEnabled } from '@/lib/utils/registry'
@@ -15,11 +16,17 @@ const DEFAULT_MODEL: Model = {
}
export async function POST(req: Request) {
+ const startTime = performance.now()
const abortSignal = req.signal
+
+ // Reset counters for new request
+ resetAllCounters()
try {
const body = await req.json()
const { message, chatId, trigger, messageId, isNewChat } = body
+
+ console.log(`[PERF] API Route - Start: chatId=${chatId}, trigger=${trigger}, isNewChat=${isNewChat}`)
// Handle different triggers
if (trigger === 'regenerate-assistant-message') {
@@ -40,7 +47,10 @@ export async function POST(req: Request) {
const referer = req.headers.get('referer')
const isSharePage = referer?.includes('/share/')
+
+ const authStart = performance.now()
const userId = await getCurrentUserId()
+ console.log(`[PERF] Auth completed: ${(performance.now() - authStart).toFixed(2)}ms`)
if (isSharePage) {
return new Response('Chat API is not available on share pages', {
@@ -80,7 +90,8 @@ export async function POST(req: Request) {
)
}
- return await createChatStreamResponse({
+ const streamStart = performance.now()
+ const response = await createChatStreamResponse({
message,
model: selectedModel,
chatId,
@@ -90,6 +101,17 @@ export async function POST(req: Request) {
abortSignal,
isNewChat
})
+
+ const totalTime = performance.now() - startTime
+ console.log(`[PERF] Total API route time: ${totalTime.toFixed(2)}ms`)
+
+ // Print summary
+ console.log(`[PERF] === Summary ===`)
+ console.log(`[PERF] Chat Type: ${isNewChat ? 'NEW' : 'EXISTING'}`)
+ console.log(`[PERF] Total Time: ${totalTime.toFixed(2)}ms`)
+ console.log(`[PERF] ================`)
+
+ return response
} catch (error) {
console.error('API route error:', error)
return new Response('Error processing your request', {
diff --git a/lib/auth/get-current-user.ts b/lib/auth/get-current-user.ts
index 8070d76c..d897f876 100644
--- a/lib/auth/get-current-user.ts
+++ b/lib/auth/get-current-user.ts
@@ -1,4 +1,5 @@
import { createClient } from '@/lib/supabase/server'
+import { incrementAuthCallCount } from '@/lib/utils/perf-tracking'
export async function getCurrentUser() {
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL
@@ -14,6 +15,8 @@ export async function getCurrentUser() {
}
export async function getCurrentUserId() {
+ const count = incrementAuthCallCount()
+ console.log(`[PERF] getCurrentUserId called - count: ${count}`)
const user = await getCurrentUser()
return user?.id
}
diff --git a/lib/db/actions.ts b/lib/db/actions.ts
index edc831d4..ab863e4a 100644
--- a/lib/db/actions.ts
+++ b/lib/db/actions.ts
@@ -13,6 +13,7 @@ import {
import type { Chat, Message } from './schema'
import { chats, generateId, messages, parts } from './schema'
import { db } from '.'
+import { incrementDbOperationCount } from '@/lib/utils/perf-tracking'
/**
* Create a new chat
@@ -76,6 +77,8 @@ export async function getChat(
export async function upsertMessage(
message: PersistableUIMessage & { chatId: string }
): Promise<Message> {
+ const count = incrementDbOperationCount()
+ console.log(`[PERF] DB - upsertMessage called - count: ${count}`)
const result = await db.transaction(async tx => {
// 1. Insert or update the message
const messageData = mapUIMessageToDBMessage(message)
@@ -131,6 +134,8 @@ export async function loadChatWithMessages(
chatId: string,
userId?: string
): Promise<(Chat & { messages: UIMessage[] }) | null> {
+ const count = incrementDbOperationCount()
+ console.log(`[PERF] DB - loadChatWithMessages called - count: ${count}`)
// Don't check cache yet - need to verify permissions first
// Get chat and messages in parallel
@@ -352,6 +357,8 @@ export async function createChatWithFirstMessageTransaction({
userId: string
message: PersistableUIMessage
}): Promise<{ chat: Chat; message: Message }> {
+ console.log(`[PERF] DB - createChatWithFirstMessageTransaction start`)
+ const dbStart = performance.now()
return await db.transaction(async tx => {
// 1. Create chat
const [chat] = await tx
@@ -377,6 +384,7 @@ export async function createChatWithFirstMessageTransaction({
}
}
+ console.log(`[PERF] DB - createChatWithFirstMessageTransaction completed: ${(performance.now() - dbStart).toFixed(2)}ms`)
return { chat, message: savedMessage }
})
}
diff --git a/lib/streaming/create-chat-stream-response.ts b/lib/streaming/create-chat-stream-response.ts
index b2b2c657..6609e73e 100644
--- a/lib/streaming/create-chat-stream-response.ts
+++ b/lib/streaming/create-chat-stream-response.ts
@@ -48,8 +48,10 @@ export async function createChatStreamResponse(
// Skip loading chat for new chats optimization
let initialChat = null
if (!isNewChat) {
+ const loadChatStart = performance.now()
// Fetch chat data for authorization check and cache it
initialChat = await loadChat(chatId, userId)
+ console.log(`[PERF] loadChat completed: ${(performance.now() - loadChatStart).toFixed(2)}ms`)
// Authorization check: if chat exists, it must belong to the user
if (initialChat && initialChat.userId !== userId) {
@@ -58,6 +60,8 @@ export async function createChatStreamResponse(
statusText: 'Forbidden'
})
}
+ } else {
+ console.log(`[PERF] loadChat skipped for new chat`)
}
// Create parent trace ID for grouping all operations
diff --git a/lib/streaming/helpers/handle-stream-finish.ts b/lib/streaming/helpers/handle-stream-finish.ts
index 5a5ca784..dba636ed 100644
--- a/lib/streaming/helpers/handle-stream-finish.ts
+++ b/lib/streaming/helpers/handle-stream-finish.ts
@@ -77,17 +77,22 @@ export async function handleStreamFinish(
const chatTitle = titlePromise ? await titlePromise : undefined
// Save message with retry logic
- upsertMessage(chatId, responseMessage, userId).catch(async error => {
- console.error('Error saving message:', error)
- try {
- await retryDatabaseOperation(
- () => upsertMessage(chatId, responseMessage, userId),
- 'save message'
- )
- } catch (retryError) {
- console.error('Failed to save after retries:', retryError)
- }
- })
+ const saveStart = performance.now()
+ upsertMessage(chatId, responseMessage, userId)
+ .then(() => {
+ console.log(`[PERF] upsertMessage (AI response) completed: ${(performance.now() - saveStart).toFixed(2)}ms`)
+ })
+ .catch(async error => {
+ console.error('Error saving message:', error)
+ try {
+ await retryDatabaseOperation(
+ () => upsertMessage(chatId, responseMessage, userId),
+ 'save message'
+ )
+ } catch (retryError) {
+ console.error('Failed to save after retries:', retryError)
+ }
+ })
// Update title after message is saved
if (chatTitle && chatTitle !== DEFAULT_CHAT_TITLE) {
diff --git a/lib/streaming/helpers/prepare-messages.ts b/lib/streaming/helpers/prepare-messages.ts
index 3d4ce46e..990dbccc 100644
--- a/lib/streaming/helpers/prepare-messages.ts
+++ b/lib/streaming/helpers/prepare-messages.ts
@@ -17,6 +17,8 @@ export async function prepareMessages(
message: UIMessage | null
): Promise<UIMessage[]> {
const { chatId, userId, trigger, messageId, initialChat, isNewChat } = context
+ const startTime = performance.now()
+ console.log(`[PERF] prepareMessages - Start: trigger=${trigger}, isNewChat=${isNewChat}`)
if (trigger === 'regenerate-assistant-message' && messageId) {
// Handle regeneration - use initialChat if available to avoid DB call
@@ -82,25 +84,36 @@ export async function prepareMessages(
// Optimize for new chats: create chat and save message together
if (isNewChat) {
// Use createChatWithFirstMessage for atomic operation
+ const createStart = performance.now()
const { createChatWithFirstMessage } = await import('@/lib/actions/chat')
await createChatWithFirstMessage(chatId, messageWithId, userId, DEFAULT_CHAT_TITLE)
+ console.log(`[PERF] createChatWithFirstMessage completed: ${(performance.now() - createStart).toFixed(2)}ms`)
+ console.log(`[PERF] prepareMessages - Total: ${(performance.now() - startTime).toFixed(2)}ms`)
return [messageWithId]
}
// For existing chats
if (!initialChat) {
+ const createStart = performance.now()
await createChat(chatId, DEFAULT_CHAT_TITLE, userId)
+ console.log(`[PERF] createChat completed: ${(performance.now() - createStart).toFixed(2)}ms`)
}
+ const upsertStart = performance.now()
await upsertMessage(chatId, messageWithId, userId)
+ console.log(`[PERF] upsertMessage completed: ${(performance.now() - upsertStart).toFixed(2)}ms`)
// If we have initialChat, append the new message instead of fetching all messages
if (initialChat && initialChat.messages) {
+ console.log(`[PERF] prepareMessages - Total: ${(performance.now() - startTime).toFixed(2)}ms (using cached chat)`)
return [...initialChat.messages, messageWithId]
}
// Fallback to fetching if no initialChat
+ const loadStart = performance.now()
const updatedChat = await loadChat(chatId, userId)
+ console.log(`[PERF] loadChat (fallback) completed: ${(performance.now() - loadStart).toFixed(2)}ms`)
+ console.log(`[PERF] prepareMessages - Total: ${(performance.now() - startTime).toFixed(2)}ms`)
return updatedChat?.messages || [messageWithId]
}
}
diff --git a/lib/utils/perf-tracking.ts b/lib/utils/perf-tracking.ts
new file mode 100644
index 00000000..a605db83
--- /dev/null
+++ b/lib/utils/perf-tracking.ts
@@ -0,0 +1,27 @@
+// Performance tracking utilities (not server actions)
+
+let authCallCount = 0
+let dbOperationCount = 0
+
+export function resetAuthCallCount() {
+ authCallCount = 0
+}
+
+export function incrementAuthCallCount() {
+ authCallCount++
+ return authCallCount
+}
+
+export function resetDbOperationCount() {
+ dbOperationCount = 0
+}
+
+export function incrementDbOperationCount() {
+ dbOperationCount++
+ return dbOperationCount
+}
+
+export function resetAllCounters() {
+ authCallCount = 0
+ dbOperationCount = 0
+}
\ No newline at end of file
diff --git a/scripts/chat-cli.ts b/scripts/chat-cli.ts
index 44e95579..3bd5f6f6 100755
--- a/scripts/chat-cli.ts
+++ b/scripts/chat-cli.ts
@@ -44,6 +44,7 @@ interface ChatPayload {
trigger: 'submit-user-message' | 'regenerate-assistant-message'
message?: UIMessage
messageId?: string
+ isNewChat?: boolean
}
class ChatApiTester {
@@ -166,6 +167,8 @@ class ChatApiTester {
} else {
const userMessage = this.createUserMessage(message || this.config.message)
payload.message = userMessage
+ // Add isNewChat flag - always true for CLI since we generate new chatId each time
+ payload.isNewChat = true
}
console.log('🚀 Sending request to:', this.config.apiUrl)