|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "test_helper" |
| 4 | + |
| 5 | +class TestCustomHooks < Minitest::Test |
| 6 | + include FirefoxTestHelpers |
| 7 | + |
| 8 | + class SimpleHook |
| 9 | + def initialize(collector) |
| 10 | + @collector = collector |
| 11 | + end |
| 12 | + |
| 13 | + def enable |
| 14 | + @start_time = @collector.current_time |
| 15 | + end |
| 16 | + |
| 17 | + def disable |
| 18 | + @collector.add_marker( |
| 19 | + name: "Simple Hook", |
| 20 | + start: @start_time, |
| 21 | + finish: @collector.current_time, |
| 22 | + data: { type: "simple_hook" } |
| 23 | + ) |
| 24 | + end |
| 25 | + end |
| 26 | + |
| 27 | + class HookWithSchema |
| 28 | + def initialize(collector) |
| 29 | + @collector = collector |
| 30 | + end |
| 31 | + |
| 32 | + def enable |
| 33 | + @collector.add_marker( |
| 34 | + name: "custom_event", |
| 35 | + start: @collector.current_time, |
| 36 | + finish: @collector.current_time + 1000, |
| 37 | + data: { type: "custom_event", event_name: "test" } |
| 38 | + ) |
| 39 | + end |
| 40 | + |
| 41 | + def disable |
| 42 | + end |
| 43 | + |
| 44 | + def firefox_marker_schema |
| 45 | + [{ |
| 46 | + name: "custom_event", |
| 47 | + display: ["marker-chart", "marker-table"], |
| 48 | + data: [{ key: "event_name", format: "string" }] |
| 49 | + }] |
| 50 | + end |
| 51 | + end |
| 52 | + |
| 53 | + class HookWithCounters |
| 54 | + def initialize(collector) |
| 55 | + @collector = collector |
| 56 | + end |
| 57 | + |
| 58 | + def enable |
| 59 | + end |
| 60 | + |
| 61 | + def disable |
| 62 | + end |
| 63 | + |
| 64 | + def firefox_counters |
| 65 | + { |
| 66 | + name: "test_counter", |
| 67 | + category: "Test", |
| 68 | + description: "Test counter", |
| 69 | + samples: { |
| 70 | + time: [0, 1000], |
| 71 | + count: [10, 20], |
| 72 | + length: 2 |
| 73 | + } |
| 74 | + } |
| 75 | + end |
| 76 | + end |
| 77 | + |
| 78 | + def test_custom_hook_class |
| 79 | + result = Vernier.profile(hooks: [SimpleHook]) do |
| 80 | + sleep 0.01 |
| 81 | + end |
| 82 | + |
| 83 | + output = Vernier::Output::Firefox.new(result).output |
| 84 | + assert_valid_firefox_profile(output) |
| 85 | + end |
| 86 | + |
| 87 | + def test_custom_hook_with_schema |
| 88 | + result = Vernier.profile(hooks: [HookWithSchema]) do |
| 89 | + sleep 0.01 |
| 90 | + end |
| 91 | + |
| 92 | + output = Vernier::Output::Firefox.new(result).output |
| 93 | + assert_valid_firefox_profile(output) |
| 94 | + |
| 95 | + data = JSON.parse(output) |
| 96 | + schemas = data.dig("meta", "markerSchema") || [] |
| 97 | + assert schemas.any? { |s| s["name"] == "custom_event" } |
| 98 | + end |
| 99 | + |
| 100 | + def test_mixed_hooks |
| 101 | + result = Vernier.profile(hooks: [:memory_usage, SimpleHook]) do |
| 102 | + sleep 0.01 |
| 103 | + end |
| 104 | + |
| 105 | + output = Vernier::Output::Firefox.new(result).output |
| 106 | + assert_valid_firefox_profile(output) |
| 107 | + assert_equal 2, result.hooks.size |
| 108 | + end |
| 109 | + |
| 110 | + def test_custom_hook_with_counters |
| 111 | + result = Vernier.profile(hooks: [HookWithCounters]) do |
| 112 | + sleep 0.01 |
| 113 | + end |
| 114 | + |
| 115 | + output = Vernier::Output::Firefox.new(result).output |
| 116 | + assert_valid_firefox_profile(output) |
| 117 | + |
| 118 | + data = JSON.parse(output) |
| 119 | + counters = data["counters"] |
| 120 | + assert counters.any? { |c| c["name"] == "test_counter" } |
| 121 | + end |
| 122 | +end |
0 commit comments