-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexample-server.js
More file actions
168 lines (145 loc) · 4.86 KB
/
Copy pathexample-server.js
File metadata and controls
168 lines (145 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env node
const http = require('http');
const fs = require('fs');
const pprof = require('@datadog/pprof');
// Initialize CPU profiler (time profiler)
const profiler = pprof.time;
// Debug available methods
console.log('Available pprof methods:', Object.keys(pprof));
console.log('Available time profiler methods:', Object.keys(profiler));
// Some CPU-intensive functions to show in the profile
function processData() {
const data = [];
for (let i = 0; i < 1000; i++) {
data.push(Math.random() * i);
}
return data.reduce((sum, val) => sum + val, 0);
}
function handleDatabase() {
// Simulate database work
const queries = [];
for (let i = 0; i < 500; i++) {
queries.push(`SELECT * FROM table_${i % 10}`);
}
return queries.join('; ');
}
function businessLogic(request) {
const data = processData();
const dbResult = handleDatabase();
// Some more CPU work
const result = [];
for (let i = 0; i < 100; i++) {
result.push({
id: i,
data: data + i,
query: dbResult.substring(0, 50)
});
}
return result;
}
// Create HTTP server
const server = http.createServer(async (req, res) => {
if (req.url === '/profile') {
try {
// Generate and return CPU profile
const profileData = profiler.stop();
console.log('Profile data type:', typeof profileData);
console.log('Profile data keys:', Object.keys(profileData || {}));
// Try both sync and async encode methods
let profile;
if (pprof.encodeSync) {
console.log('Using synchronous encode');
profile = pprof.encodeSync(profileData);
} else {
console.log('Using async encode');
profile = await pprof.encode(profileData);
}
console.log('Encoded profile type:', typeof profile);
console.log('Encoded profile length:', profile?.length);
console.log('First 20 bytes:', profile?.slice(0, 20));
res.writeHead(200, {
'Content-Type': 'application/octet-stream',
'Content-Disposition': 'attachment; filename="cpu-profile.pb"'
});
res.end(profile);
console.log('CPU profile generated and sent');
// Restart profiling for next request
profiler.start();
} catch (error) {
console.error('Error generating profile:', error);
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('Error generating profile');
}
return;
}
if (req.url === '/load') {
// Generate some CPU load
const startTime = Date.now();
const result = businessLogic(req);
const duration = Date.now() - startTime;
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({
message: 'Load generated',
duration: `${duration}ms`,
itemCount: result.length
}));
return;
}
// Default response with instructions
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(`
<!DOCTYPE html>
<html>
<head>
<title>PProf Example Server</title>
<style>
body { font-family: Arial, sans-serif; max-width: 600px; margin: 50px auto; }
.endpoint { background: #f5f5f5; padding: 15px; margin: 10px 0; border-radius: 5px; }
code { background: #e8e8e8; padding: 2px 5px; border-radius: 3px; }
</style>
</head>
<body>
<h1>PProf Example Server</h1>
<p>This server demonstrates CPU profiling with @datadog/pprof.</p>
<div class="endpoint">
<h3>Generate Load</h3>
<p>Visit <a href="/load">/load</a> to generate some CPU-intensive work</p>
<p>This will run functions like <code>processData()</code>, <code>handleDatabase()</code>, and <code>businessLogic()</code></p>
</div>
<div class="endpoint">
<h3>Download CPU Profile</h3>
<p>Visit <a href="/profile">/profile</a> to download the CPU profile as a .pb file</p>
<p>You can then use: <code>node cli.js cpu-profile.pb</code> to generate a flamegraph</p>
</div>
<h3>Example Usage:</h3>
<ol>
<li>Visit <a href="/load">/load</a> several times to generate CPU activity</li>
<li>Download the profile from <a href="/profile">/profile</a></li>
<li>Run: <code>node cli.js cpu-profile.pb</code></li>
<li>Open the generated HTML file to view the flamegraph</li>
</ol>
</body>
</html>
`);
});
const PORT = process.env.PORT || 3002;
server.listen(PORT, () => {
console.log(`PProf example server running on http://localhost:${PORT}`);
console.log('');
console.log('Usage:');
console.log(`1. Generate load: curl http://localhost:${PORT}/load`);
console.log(`2. Get profile: curl http://localhost:${PORT}/profile > cpu-profile.pb`);
console.log('3. Generate HTML: node cli.js cpu-profile.pb');
console.log('');
// Start CPU profiling
profiler.start();
});
// Graceful shutdown
process.on('SIGINT', () => {
console.log('\nStopping server...');
profiler.stop();
server.close(() => {
console.log('Server stopped');
process.exit(0);
});
});