Web Tracing Framework

来源:互联网 发布:麟龙炒股软件 编辑:程序博客网 时间:2024/06/16 12:26

The tracing framework is a suite of tools and libraries, and there are many different ways to obtain each. The easiest way to get started is to install the Chrome Extension (on Mac/Linux) and see what it records on your page. From there, you can start to add custom instrumentation and analyzing the data.

Chrome

The most awesome, advanced features require the Chrome extension and some optional command line flags. It's recommended you run with the dev channel or a Canary build. Mac or Linux are preferred!

Install Chrome Extension

Others

None yet! Contribute some! Maybe a Firefox one?

Command Line Tools

Requires: node >= 0.8.14

You can use NPM to install the tracing-framework module. You can either install it within your project as a dev dependency or globally to get access to the command line tools.

$ node -v # ensure at least v0.8.14$ npm install -g tracing-framework$ wtf-trace myapp.js$ wtf-dump myapp-[date].wtf-trace # or open in the UI

Building From Source

The hosted library and extension are updated every few weeks, but if you prefer living on the bleeding edge you can checkout and build the entire toolset from source. Detailed instructions are available in the building documentation.

The tracing framework operates by being loaded inside your page or app before any of your application code executes. Attempting to load it afterwards will produce incorrect results.

Using the Chrome Extension

The easiest way to put the framework on the page is via the Chrome extension. It gives you a toggle button for injection and makes it easy to enable and disable the framework when developing and testing. The extension also provides additional support for advanced features such as browser internal events like garbage collections.

You can toggle the injection via the page action popup, as well as show the UI at any time. Note that your page will reload when you enable or disable the injection, so it's handy to have any state you need to preserve stored in the URL to save time.

Screenshot showing enabling and disabling the library.

Manually Embedding

When running where a browser extension is not available, such as Internet Explorer or on mobile devices, it's possible to manually embed the library on a page using some <script> tags. This is also useful when running automated tests that cannot depend on user interaction.

Add a <script> tag pointing at the compiled tracing library. This can be your own build result (if building from source), a copy of the library stored on your own server, or the publicly hosted one on Github. Note that the Github hosted version will not load on HTTPS pages, so you will have to copy it to your own server first.

After loading the library it must be initialized and started. You should do this with another <script> tag immediately following the previous one and again before any other scripts. This tag initializes options used by the library, optionally shows the on-screen HUD, and starts tracing right away (usually the desired behavior).

  1. <html>
  2. <head>
  3. <!-- These MUST be before any other scripts! -->
  4. <script src="http://google.github.io/tracing-framework/bin/wtf_trace_web_js_compiled.js"></script>
  5. <script>
  6. var options = {
  7. /* options, if any */
  8. };
  9. // Setup tracing with the given options.
  10. wtf.trace.prepare(options);
  11. // Optionally, create the on-screen HUD for better control.
  12. wtf.hud.prepare(options);
  13. // Start recording a trace immediately (if desired).
  14. wtf.trace.start(options);
  15. </script>
  16. <!-- Add your stuff here... -->
  17. </head>
  18. </html>

For more details about the options that can be passed see the options documentation and the event providers page.

node.js

The easiest way to get the tracing framework to record your application is to use the wtf-trace tool included with the NPM package. This allows you to run any other node app with the library already present and recording at startup and will dump the trace file when the application ends (or CTRL-C is used).

  1. $ wtf-trace myapp.js
  2. Running...
  3. <ctrl-c>
  4. $ ls *.wtf-trace
  5. myapp-123456.wtf-trace

When more control is desired you can instead directly require and setup the library. First you must obtain the wtf_node_js_compiled.jsfile, which can be found in the node_modules/tracing-framework/build-out/ directory from your NPM install.

  1. // Require from wherever the library happens to be.
  2. global.wtf = require('tracing-framework');
  3. // Provide any options here.
  4. var options = {
  5. /* options, if any */
  6. };
  7. // Start tracing with a handler for ctrl-c saving.
  8. wtf.trace.node.start(options);

If you prefer to more tightly control the trace recording you can call the normal API instead:

  1. global.wtf = require('tracing-framework');
  2. wtf.trace.prepare({ /* options, if any */ });
  3.  
  4. // Later on, when desired:
  5. wtf.trace.start();
  6. // Do stuff...
  7. wtf.trace.snapshot();

Once the library is included and initialized as above one must record and save off traces. When using the on-screen HUD such as when added via the Chrome Extension this is as easy as clicking a button, but otherwise you must control the recording and saving yourself. Note that even with the HUD enabled it's still possible to use the programmatic control as described below.

Capturing with the HUD

The on-screen HUD provides several options for saving, resetting, and showing trace snapshots. You can easily reset the current data, save a snapshot to disk, or open a new UI with the data loaded with either buttons or hotkeys.

TIP: hold shift while clicking 'Show in UI' to open the trace in a new window.

TIP: see the tooltips on the buttons for keyboard keys that perform the action.

Screenshot showing the on-screen HUD buttons.

Screenshot showing saving a trace file for later use.

Screenshot showing how to snapshot and open the UI.

Capturing with Code

Programmatically capturing traces can be useful when trying to gather data from specific timeranges, reducing the interference of the framework by not showing the HUD, or trying to automate trace capture.

Starting/stopping a Capture

  1. // Start capturing immediately.
  2. wtf.trace.start({
  3. /* option overrides, if any */
  4. });
  5.  
  6. // Reset the current buffer, clearing all recorded events.
  7. wtf.trace.reset();
  8.  
  9. // Stop capturing and cleanup.
  10. wtf.trace.stop();

Captures are started with the wtf.trace.start() API. This takes an optional object dictionary that can override any options that were previously passed to a wtf.trace.prepare() call or were setup by the extension.

The trace begins recording immediately after the call returns and when in snapshotting mode any new events are appended to the buffer. If you want to clear the buffer (for example, to ignore previous uninteresting events) you can use wtf.trace.reset().

When the capture session is no longer required a call to wtf.trace.stop() is used to clean up any resources used by the snapshot. After this the data is disposed and there is no way to save it. Any future calls to wtf.trace.start() will begin with a clean buffer.

Saving a Snapshot

  1. // Save a snapshot with the default save options (file named on page/time):
  2. wtf.trace.snapshot();
  3.  
  4. // Save a file with the given prefix:
  5. wtf.trace.snapshot('file://myprefix');
  6.  
  7. // Grab the binary data from the snapshot:
  8. var buffers = [];
  9. wtf.trace.snapshot(buffers);
  10. console.log(buffers[0]); // a Uint8Array of the data

Snapshots are saved on demand and can be either saved to a file, posted to a remote HTTP server, or grabbed in memory for custom saving.

If desired, an default save target can be passed when the library is initialized so that it needs not be passed to each snapshot call:

  1. // Specify a default save name (used for snapshots unless overridden).
  2. wtf.trace.prepare({
  3. 'wtf.trace.target': 'file://myname'
  4. });
  5.  
  6. // No need to pass a name here, the given target will be used:
  7. wtf.trace.snapshot();

Capture to UI

The easiest way to view a trace is to open it directly from the on-screen HUD. The 'Show in UI' button will capture a new trace and pop up a new UI window with the trace loaded, ready for inspection. If you wish to save this trace for later comparison or sharing you can use the 'Save' button.

Opening the UI

You can always use the hosted version available on Github:

http://google.github.io/tracing-framework/bin/app/maindisplay.html

If using the Chrome extension you can open the UI quickly:

Screenshot showing how to open the UI at any time.

Loading Traces

If you've saved a trace and have a .wtf-trace file there are several ways to load it.

  • Use the 'Open' button on the welcome screen.
  • Drag/drop a file anywhere in the UI.
  • Drag/drop a file from the Chrome downloads bar.
  • Use the wtf omnibar keyword in Chrome to open URLs directly.

TIP: Drag/drop new files into the UI to update with the latest data.

Screenshot showing drag/drop loading of a file.

Screenshot showing opening a URL from the omnibox.

Sharing Traces

It's possible to use the Github-hosted UI page to easily share traces with people who may not have the tracing framework extension installed. The simplest is to give them the URL to the UI, http://google.github.io/tracing-framework/bin/app/maindisplay.html, and tell them to open the file you've shared.

Alternatively, the UI page accepts a ?url=http://... query parameter that allows for loading a trace from a given source. Beware that the target must allow cross-origin XHRs via cross-origin resource sharing or it will fail to load.

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 尿检的时候遇上月经期怎么办 消防兵改革那新兵怎么办 教师资格证体检有问题怎么办 检兵合格还在上学怎么办 运动后肌肉肿了怎么办 打架用力过猛肌肉疼痛怎么办 军检只要一项不合格怎么办 体检身高差一厘米怎么办 体检身高差两公分怎么办 体检身高差10厘米怎么办 孩子的爸爸总是打游戏怎么办 拉屎屁眼疼还有血怎么办 家里冼澡要等好多冷水怎么办 孕晚期小孩头大怎么办 报考警校体检不合格退回怎么办? 入职体检视力0.1怎么办 屁扒骨折疼要怎么办 宝宝发高烧怎么办能快速退烧 屁股上长了纹路怎么办 手机充电头歪了怎么办 屁股挠烂了化脓怎么办 手机充电那坏了怎么办 孩子在学校被老师冤枉怎么办 初中学校不好我该怎么办 天气太热屁股淹了怎么办 骑车骑的屁股疼怎么办 爬山时屁股摔紫青了怎么办 宝宝不肯脱裤子拉粑粑怎么办 国家对无地农民怎么办 生完孩子骨架变大怎么办 17岁长高很慢怎么办? 出月子腿着凉了怎么办 脚着凉了脚疼怎么办 腿着凉了特别疼怎么办 孩子骨龄大2两年怎么办 和人吃饭很尴尬怎么办 头不自觉向右偏怎么办 靠墙站立腰疼怎么办 小腿酸痛乏力肌肉萎缩怎么办 搬重物后手臂疼怎么办 和尚鹦鹉吃了盐怎么办