-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add metrics collection framework with real-time overlay #141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
52eef5d
feat: add metrics collection framework with real-time overlay
forketyfork ec0b869
fix: always emit ToggleMetrics action for keyboard shortcut
forketyfork c8b7719
fix: address PR review comments
forketyfork 275046a
fix: move metrics overlay to bottom-right corner
forketyfork 37c0ab8
docs: explain O(n) eviction trade-off in glyph cache
forketyfork 0f8e3d4
refactor: clarify glyph cache metric display names
forketyfork 8e1a910
feat: add FPS display to metrics overlay
forketyfork File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| const std = @import("std"); | ||
|
|
||
| pub const MetricKind = enum(u8) { | ||
| glyph_cache_hits, | ||
| glyph_cache_misses, | ||
| glyph_cache_evictions, | ||
| glyph_cache_size, | ||
| frame_count, | ||
| }; | ||
|
|
||
| const METRIC_COUNT = @typeInfo(MetricKind).@"enum".fields.len; | ||
|
|
||
| pub const Metrics = struct { | ||
| values: [METRIC_COUNT]u64, | ||
| prev_values: [METRIC_COUNT]u64, | ||
| last_sample_ms: i64, | ||
|
|
||
| pub fn init() Metrics { | ||
| return .{ | ||
| .values = [_]u64{0} ** METRIC_COUNT, | ||
| .prev_values = [_]u64{0} ** METRIC_COUNT, | ||
| .last_sample_ms = 0, | ||
| }; | ||
| } | ||
|
|
||
| pub fn increment(self: *Metrics, kind: MetricKind) void { | ||
| self.values[@intFromEnum(kind)] +%= 1; | ||
| } | ||
|
|
||
| pub fn set(self: *Metrics, kind: MetricKind, value: u64) void { | ||
| self.values[@intFromEnum(kind)] = value; | ||
| } | ||
|
|
||
| pub fn get(self: *const Metrics, kind: MetricKind) u64 { | ||
| return self.values[@intFromEnum(kind)]; | ||
| } | ||
|
|
||
| /// Returns elapsed time since last sample. Call getRate() after this, | ||
| /// then commitSample() to prepare for the next interval. | ||
| pub fn sampleRates(self: *const Metrics, now_ms: i64) i64 { | ||
| return now_ms - self.last_sample_ms; | ||
| } | ||
|
|
||
| /// Copies current values to prev_values for the next rate calculation. | ||
| /// Call this AFTER getRate() to avoid zeroing the delta. | ||
| pub fn commitSample(self: *Metrics, now_ms: i64) void { | ||
| @memcpy(&self.prev_values, &self.values); | ||
| self.last_sample_ms = now_ms; | ||
| } | ||
|
|
||
| pub fn getRate(self: *const Metrics, kind: MetricKind, elapsed_ms: i64) f64 { | ||
| if (elapsed_ms <= 0) return 0.0; | ||
| const idx = @intFromEnum(kind); | ||
| const delta = self.values[idx] -% self.prev_values[idx]; | ||
| return @as(f64, @floatFromInt(delta)) / (@as(f64, @floatFromInt(elapsed_ms)) / 1000.0); | ||
| } | ||
| }; | ||
|
|
||
| pub var global: ?*Metrics = null; | ||
|
|
||
| pub inline fn increment(kind: MetricKind) void { | ||
| if (global) |m| m.increment(kind); | ||
| } | ||
|
|
||
| pub inline fn set(kind: MetricKind, value: u64) void { | ||
| if (global) |m| m.set(kind, value); | ||
| } | ||
|
|
||
| test "Metrics.increment" { | ||
| var m = Metrics.init(); | ||
| try std.testing.expectEqual(@as(u64, 0), m.get(.glyph_cache_hits)); | ||
| m.increment(.glyph_cache_hits); | ||
| try std.testing.expectEqual(@as(u64, 1), m.get(.glyph_cache_hits)); | ||
| m.increment(.glyph_cache_hits); | ||
| try std.testing.expectEqual(@as(u64, 2), m.get(.glyph_cache_hits)); | ||
| } | ||
|
|
||
| test "Metrics.set" { | ||
| var m = Metrics.init(); | ||
| m.set(.glyph_cache_size, 42); | ||
| try std.testing.expectEqual(@as(u64, 42), m.get(.glyph_cache_size)); | ||
| m.set(.glyph_cache_size, 100); | ||
| try std.testing.expectEqual(@as(u64, 100), m.get(.glyph_cache_size)); | ||
| } | ||
|
|
||
| test "Metrics.getRate" { | ||
| var m = Metrics.init(); | ||
| m.last_sample_ms = 0; | ||
| m.prev_values = [_]u64{0} ** METRIC_COUNT; | ||
| m.values[@intFromEnum(MetricKind.glyph_cache_hits)] = 10; | ||
| const rate = m.getRate(.glyph_cache_hits, 1000); | ||
| try std.testing.expectApproxEqAbs(@as(f64, 10.0), rate, 0.001); | ||
| } | ||
|
|
||
| test "global metrics null check" { | ||
| global = null; | ||
| increment(.frame_count); | ||
| set(.glyph_cache_size, 100); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.