tracer_test.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright 2006 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.debug.TraceTest');
  15. goog.setTestOnly('goog.debug.TraceTest');
  16. goog.require('goog.debug.Trace');
  17. goog.require('goog.testing.jsunit');
  18. function testTracer() {
  19. goog.debug.Trace.initCurrentTrace();
  20. var t = goog.debug.Trace.startTracer('foo');
  21. var sum = 0;
  22. for (var i = 0; i < 100000; i++) {
  23. sum += i;
  24. }
  25. goog.debug.Trace.stopTracer(t);
  26. var trace = goog.debug.Trace.getFormattedTrace();
  27. var lines = trace.split('\n');
  28. assertEquals(8, lines.length);
  29. assertNotNull(lines[0].match(/^\s*\d+\.\d+\s+Start\s+foo$/));
  30. assertNotNull(lines[1].match(/^\s*\d+\s+\d+\.\d+\s+Done\s+\d+ ms\s+foo$/));
  31. }
  32. function testPerf() {
  33. goog.debug.Trace.initCurrentTrace();
  34. var count = 1000;
  35. var start = goog.now();
  36. for (var i = 0; i < count; i++) {
  37. var t = goog.debug.Trace.startTracer('foo');
  38. var t2 = goog.debug.Trace.startTracer('foo.bar');
  39. var t3 = goog.debug.Trace.startTracer('foo.bar.baz');
  40. goog.debug.Trace.stopTracer(t3);
  41. var t4 = goog.debug.Trace.startTracer('foo.bar.bim');
  42. goog.debug.Trace.stopTracer(t4);
  43. goog.debug.Trace.stopTracer(t2);
  44. goog.debug.Trace.stopTracer(t);
  45. }
  46. count *= 4;
  47. var end = goog.now();
  48. }