jlfwong / speedscope

🔬 A fast, interactive web-based viewer for performance profiles.
https://www.speedscope.app
MIT License
5.59k stars 246 forks source link

souremaps arent being applied #446

Open mbrevda opened 1 year ago

mbrevda commented 1 year ago

Hi - trying to run speedscope to help optimize program startup. It doesn't seem like sourcemaps are being applied, making tracing operations challenging.

Screenshot 2023-11-07 at 10 36 16 AM

Here is a standalone script that closely mimics my setup, illustrating the issue:

test.sh ```sh set -e tmpdir=$(mktemp -d) cat << EOF > $tmpdir/bar.ts import http from "node:http"; function listenCallBack() { console.log("Server is running"); setTimeout(() => { console.log("exiting server..."); process.exit(0); }, 100); }; const server = () => { const server = http.createServer((req, res) => { res.end("Hello World"); }); server.listen("8080", listenCallBack); return server; }; export default server; EOF cat << EOF > $tmpdir/index.ts import('./bar.ts').then((bar) => bar.default()); EOF cd $tmpdir npx esbuild --bundle --splitting --minify --sourcemap=linked --platform=node --format=esm --out-extension:.js=.mjs --outdir=./ index.ts node --prof --enable-source-maps $tmpdir/index.mjs node --prof-process --preprocess -j isolate*.log | npx speedscope - rm -rf $tmpdir ```

Thanks!

jlfwong commented 1 year ago

Hey! As far as I remember, there's no circumstance where source-maps are automatically applied. That said, speedscope lets you apply sourcemaps after-the-fact.

See #317

mbrevda commented 1 year ago

speedscope lets you apply sourcemaps after-the-fact.

Thats cool! Unfortunately, dropping sourcemaps doesn't seem to have any effect on paths or symbols. Is there another way to load them that I can try?

mbrevda commented 1 year ago

Could this be related to esm code? I tried generating my code as .js (from .mjs) but that didn't help. Any way to debug sourcemaps to see if they are finding anything to apply?

jlfwong commented 1 year ago

I'm not sure offhand if this would have anything to do with esm!

If you want to debug, your best bet would be to step through the code here: https://github.com/jlfwong/speedscope/blob/de17f128d0ce9d2a696ac7772dec6393dc2888b4/src/lib/js-source-map.ts#L75

To get up and running locally, check out https://github.com/jlfwong/speedscope/blob/main/CONTRIBUTING.md

mbrevda commented 1 year ago

Good call! It seems that the prof traces aren't having their filename/line/column populated. Not sure if this is the best way to do it, but the following patch fixes that:

diff --git a/src/lib/profile.ts b/src/lib/profile.ts
index 4e04742..13b189b 100644
--- a/src/lib/profile.ts
+++ b/src/lib/profile.ts
@@ -66,6 +66,15 @@ export class Frame extends HasWeights {

   private constructor(info: FrameInfo) {
     super()
+    if (!info.file && !info.line && !info.col) {
+      const matches = info.name.match(/(?:\/\/?)?([^:]+):(\d+):(\d+)/)
+      if (matches) {
+        info.file = matches[1]
+        info.line = parseInt(matches[2], 10)
+        info.col = parseInt(matches[3], 10)
+      }
+    }
+
     this.key = info.key
     this.name = info.name
     this.file = info.file

However, even with this patch, the frames aren't remapped to their proper sources.