profiler.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.startProfiling = startProfiling;
  6. exports.stopProfiling = stopProfiling;
  7. var fs = _interopRequireWildcard(require("fs"));
  8. var path = _interopRequireWildcard(require("path"));
  9. function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
  10. function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  11. /**
  12. * Copyright Microsoft Corporation. All rights reserved.
  13. *
  14. * Licensed under the Apache License, Version 2.0 (the "License");
  15. * you may not use this file except in compliance with the License.
  16. * You may obtain a copy of the License at
  17. *
  18. * http://www.apache.org/licenses/LICENSE-2.0
  19. *
  20. * Unless required by applicable law or agreed to in writing, software
  21. * distributed under the License is distributed on an "AS IS" BASIS,
  22. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. * See the License for the specific language governing permissions and
  24. * limitations under the License.
  25. */
  26. const profileDir = process.env.PWTEST_PROFILE_DIR || '';
  27. let session;
  28. async function startProfiling() {
  29. if (!profileDir) return;
  30. session = new (require('inspector').Session)();
  31. session.connect();
  32. await new Promise(f => {
  33. session.post('Profiler.enable', () => {
  34. session.post('Profiler.start', f);
  35. });
  36. });
  37. }
  38. async function stopProfiling(profileName) {
  39. if (!profileDir) return;
  40. await new Promise(f => session.post('Profiler.stop', (err, {
  41. profile
  42. }) => {
  43. if (!err) {
  44. fs.mkdirSync(profileDir, {
  45. recursive: true
  46. });
  47. fs.writeFileSync(path.join(profileDir, profileName + '.json'), JSON.stringify(profile));
  48. }
  49. f();
  50. }));
  51. }