profiler_test.go 871 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package goja
  2. import (
  3. "sync/atomic"
  4. "testing"
  5. "time"
  6. )
  7. func TestProfiler(t *testing.T) {
  8. err := StartProfile(nil)
  9. if err != nil {
  10. t.Fatal(err)
  11. }
  12. vm := New()
  13. go func() {
  14. _, err := vm.RunScript("test123.js", `
  15. const a = 2 + 2;
  16. function loop() {
  17. for(;;) {}
  18. }
  19. loop();
  20. `)
  21. if err != nil {
  22. if _, ok := err.(*InterruptedError); !ok {
  23. panic(err)
  24. }
  25. }
  26. }()
  27. time.Sleep(200 * time.Millisecond)
  28. atomic.StoreInt32(&globalProfiler.enabled, 0)
  29. pr := globalProfiler.p.stop()
  30. if len(pr.Sample) == 0 {
  31. t.Fatal("No samples were recorded")
  32. }
  33. var running bool
  34. for i := 0; i < 10; i++ {
  35. time.Sleep(100 * time.Millisecond)
  36. globalProfiler.p.mu.Lock()
  37. running = globalProfiler.p.running
  38. globalProfiler.p.mu.Unlock()
  39. if !running {
  40. break
  41. }
  42. }
  43. if running {
  44. t.Fatal("The profiler is still running")
  45. }
  46. vm.Interrupt(nil)
  47. }