Skip to content

feat: add advanced thread metrics (peak UPS, state transitions, queue depth, memory, jitter) - #63

Merged
buddhasaikia merged 5 commits into
mainfrom
feature/12-advanced-thread-metrics
Feb 27, 2026
Merged

buddhasaikia merged 5 commits into
mainfrom
feature/12-advanced-thread-metrics

Conversation

@buddhasaikia

Copy link
Copy Markdown
Owner

Summary

Closes #12

Adds five advanced thread metrics beyond basic update-count/avg-time tracking.

New Metrics

Metric How it's computed
Peak Updates/Sec 1-second sliding window over update timestamps
State Transitions Detects Thread.state changes between consecutive recordUpdate() calls
Queue Depth Atomic counter incremented on channel.send(), decremented on receive
Memory Debug.getNativeHeapAllocatedSize() (process-level; per-thread unavailable on Android)
Jitter Standard deviation of inter-update intervals in milliseconds

Changes

Core (ThreadMetrics.kt)

  • Extended ThreadMetrics data class with 5 new fields (all with defaults for backward compatibility)
  • Added tracking structures in ThreadMonitor: updateTimestamps, stateTransitionCounts, lastThreadStates, queue depth atomic counter
  • Implemented computePeakUpdatesPerSec(), computeJitter(), getThreadAllocatedBytes()
  • Added incrementQueueDepth() / decrementQueueDepth() public methods

ViewModel (HomeViewModel.kt)

  • Wired threadMonitor.incrementQueueDepth() after each channel.send() in all three fetch methods
  • Wired threadMonitor.decrementQueueDepth() in initChannel on each receive

UI (HomeScreenRoute.kt)

  • ThreadMetricsDisplay cards now show all 5 new metrics alongside existing ones

Export (MetricsExporter.kt)

  • Added 5 new fields to ExportedThreadMetric
  • Updated both buildCSVContent and buildJSONContent

Tests (ThreadMonitorTest.kt)

  • 6 new tests: peak UPS, queue depth, jitter, memory, clear resets, default values
  • Updated testThreadMetricsDataClass for new fields
  • All existing tests continue to pass

Verification

BUILD SUCCESSFUL in 12s
33 actionable tasks: 11 executed, 22 up-to-date

… depth, memory, jitter)

Closes #12

Expands ThreadMonitor with five new metrics:
- Peak updates/sec via 1-second sliding window
- Thread state transition counting (RUNNABLE↔WAITING etc.)
- Queue depth tracking via atomic counter wired to channel send/receive
- Process-wide native heap memory via Android Debug API
- Jitter as standard deviation of inter-update intervals

Updated MetricsExporter (CSV + JSON), ThreadMetricsDisplay UI, and
added 6 new unit tests covering all advanced metrics.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR implements five advanced thread metrics to enhance the monitoring capabilities of the ThreadSimulator app: peak updates per second (using 1-second sliding window), thread state transitions, queue depth tracking, process-level memory allocation, and jitter measurements (standard deviation of inter-update intervals). The feature addresses issue #12 and extends the existing ThreadMetrics data class with backward-compatible default values while adding corresponding tracking structures in ThreadMonitor, UI display components, export functionality, and comprehensive unit tests.

Changes:

  • Extended ThreadMetrics with 5 new fields, all with default values for backward compatibility
  • Implemented tracking structures and computation methods in ThreadMonitor for advanced metrics
  • Integrated queue depth tracking in HomeViewModel by adding increment/decrement calls around channel operations
  • Updated UI to display all new metrics in ThreadMetricsDisplay cards with appropriate formatting
  • Extended CSV and JSON export to include all new metric fields

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 14 comments.

Show a summary per file
File Description
app/src/main/java/com/bs/threadsimulator/common/ThreadMetrics.kt Added 5 new fields to ThreadMetrics data class, implemented tracking structures (updateTimestamps, stateTransitionCounts, lastThreadStates, queueDepthCounter), and computation methods (peak UPS, jitter, memory)
app/src/main/java/com/bs/threadsimulator/ui/screens/HomeViewModel.kt Integrated queue depth tracking by calling incrementQueueDepth() after channel.send() and decrementQueueDepth() on receive
app/src/main/java/com/bs/threadsimulator/ui/screens/HomeScreenRoute.kt Updated ThreadMetricsDisplay to show all 5 new metrics with formatting, increased card width to accommodate content
app/src/main/java/com/bs/threadsimulator/common/MetricsExporter.kt Extended ExportedThreadMetric with new fields and updated buildCSVContent/buildJSONContent to export them
app/src/test/java/com/bs/threadsimulator/common/ThreadMonitorTest.kt Added 6 new test cases for advanced metrics (peak UPS, queue depth, jitter, memory, clear resets, default values) and updated existing data class test

Comment thread app/src/main/java/com/bs/threadsimulator/common/ThreadMetrics.kt
Comment thread app/src/main/java/com/bs/threadsimulator/common/ThreadMetrics.kt
Comment thread app/src/main/java/com/bs/threadsimulator/ui/screens/HomeViewModel.kt Outdated
Comment thread app/src/main/java/com/bs/threadsimulator/ui/screens/HomeViewModel.kt Outdated
Comment thread app/src/main/java/com/bs/threadsimulator/common/ThreadMetrics.kt Outdated
Comment thread app/src/main/java/com/bs/threadsimulator/common/ThreadMetrics.kt Outdated
Comment thread app/src/main/java/com/bs/threadsimulator/common/ThreadMetrics.kt Outdated
Comment thread app/src/main/java/com/bs/threadsimulator/common/ThreadMetrics.kt Outdated
- Set queue depth increment before channel.send() wrapped in try-catch
- Optimize computePeakUpdatesPerSec to O(n) sliding window
- Bound updateTimestamps retention to 10 seconds to prevent OOM
- Remove unused perThreadTotalTime
- Format memory in UI to display with %.2f KB
- Fix computeJitter threshold
- Add queue depth edge-case tests
@buddhasaikia
buddhasaikia merged commit 904d31f into main Feb 27, 2026
8 checks passed
@buddhasaikia
buddhasaikia deleted the feature/12-advanced-thread-metrics branch February 27, 2026 16:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add advanced thread metrics

2 participants