200 lines
6.1 KiB
TypeScript
200 lines
6.1 KiB
TypeScript
import { PerformanceMonitor, monitor } from './monitoring';
|
|
|
|
describe('PerformanceMonitor', () => {
|
|
beforeEach(() => {
|
|
PerformanceMonitor['instance'] = null as any;
|
|
});
|
|
|
|
describe('getInstance', () => {
|
|
it('should return singleton instance', () => {
|
|
const instance1 = PerformanceMonitor.getInstance();
|
|
const instance2 = PerformanceMonitor.getInstance();
|
|
|
|
expect(instance1).toBe(instance2);
|
|
});
|
|
});
|
|
|
|
describe('recordMetric', () => {
|
|
it('should record a metric value', () => {
|
|
const instance = PerformanceMonitor.getInstance();
|
|
instance.recordMetric('test-metric', 100);
|
|
|
|
expect(instance.getCount('test-metric')).toBe(1);
|
|
});
|
|
|
|
it('should record multiple metric values', () => {
|
|
const instance = PerformanceMonitor.getInstance();
|
|
instance.recordMetric('test-metric', 100);
|
|
instance.recordMetric('test-metric', 200);
|
|
instance.recordMetric('test-metric', 300);
|
|
|
|
expect(instance.getCount('test-metric')).toBe(3);
|
|
});
|
|
|
|
it('should maintain max 1000 values per metric', () => {
|
|
const instance = PerformanceMonitor.getInstance();
|
|
for (let i = 0; i < 1005; i++) {
|
|
instance.recordMetric('test-metric', i);
|
|
}
|
|
|
|
expect(instance.getCount('test-metric')).toBe(1000);
|
|
});
|
|
});
|
|
|
|
describe('getAverage', () => {
|
|
it('should return 0 for empty metric', () => {
|
|
const instance = PerformanceMonitor.getInstance();
|
|
expect(instance.getAverage('empty-metric')).toBe(0);
|
|
});
|
|
|
|
it('should calculate average correctly', () => {
|
|
const instance = PerformanceMonitor.getInstance();
|
|
instance.recordMetric('test-metric', 100);
|
|
instance.recordMetric('test-metric', 200);
|
|
instance.recordMetric('test-metric', 300);
|
|
|
|
expect(instance.getAverage('test-metric')).toBe(200);
|
|
});
|
|
});
|
|
|
|
describe('getPercentile', () => {
|
|
it('should return 0 for empty metric', () => {
|
|
const instance = PerformanceMonitor.getInstance();
|
|
expect(instance.getPercentile('empty-metric', 50)).toBe(0);
|
|
});
|
|
|
|
it('should calculate 50th percentile correctly', () => {
|
|
const instance = PerformanceMonitor.getInstance();
|
|
for (let i = 1; i <= 100; i++) {
|
|
instance.recordMetric('test-metric', i);
|
|
}
|
|
|
|
expect(instance.getPercentile('test-metric', 50)).toBe(50);
|
|
});
|
|
|
|
it('should calculate 95th percentile correctly', () => {
|
|
const instance = PerformanceMonitor.getInstance();
|
|
for (let i = 1; i <= 100; i++) {
|
|
instance.recordMetric('test-metric', i);
|
|
}
|
|
|
|
expect(instance.getPercentile('test-metric', 95)).toBe(95);
|
|
});
|
|
|
|
it('should calculate 99th percentile correctly', () => {
|
|
const instance = PerformanceMonitor.getInstance();
|
|
for (let i = 1; i <= 100; i++) {
|
|
instance.recordMetric('test-metric', i);
|
|
}
|
|
|
|
expect(instance.getPercentile('test-metric', 99)).toBe(99);
|
|
});
|
|
});
|
|
|
|
describe('getCount', () => {
|
|
it('should return 0 for non-existent metric', () => {
|
|
const instance = PerformanceMonitor.getInstance();
|
|
expect(instance.getCount('non-existent')).toBe(0);
|
|
});
|
|
|
|
it('should return count for existing metric', () => {
|
|
const instance = PerformanceMonitor.getInstance();
|
|
instance.recordMetric('test-metric', 100);
|
|
instance.recordMetric('test-metric', 200);
|
|
|
|
expect(instance.getCount('test-metric')).toBe(2);
|
|
});
|
|
});
|
|
|
|
describe('getMin', () => {
|
|
it('should return 0 for empty metric', () => {
|
|
const instance = PerformanceMonitor.getInstance();
|
|
expect(instance.getMin('empty-metric')).toBe(0);
|
|
});
|
|
|
|
it('should return minimum value', () => {
|
|
const instance = PerformanceMonitor.getInstance();
|
|
instance.recordMetric('test-metric', 100);
|
|
instance.recordMetric('test-metric', 50);
|
|
instance.recordMetric('test-metric', 200);
|
|
|
|
expect(instance.getMin('test-metric')).toBe(50);
|
|
});
|
|
});
|
|
|
|
describe('getMax', () => {
|
|
it('should return 0 for empty metric', () => {
|
|
const instance = PerformanceMonitor.getInstance();
|
|
expect(instance.getMax('empty-metric')).toBe(0);
|
|
});
|
|
|
|
it('should return maximum value', () => {
|
|
const instance = PerformanceMonitor.getInstance();
|
|
instance.recordMetric('test-metric', 100);
|
|
instance.recordMetric('test-metric', 200);
|
|
instance.recordMetric('test-metric', 50);
|
|
|
|
expect(instance.getMax('test-metric')).toBe(200);
|
|
});
|
|
});
|
|
|
|
describe('getStats', () => {
|
|
it('should return stats for empty metric', () => {
|
|
const instance = PerformanceMonitor.getInstance();
|
|
const stats = instance.getStats('empty-metric');
|
|
|
|
expect(stats).toEqual({
|
|
count: 0,
|
|
avg: 0,
|
|
min: 0,
|
|
max: 0,
|
|
p50: 0,
|
|
p95: 0,
|
|
p99: 0,
|
|
});
|
|
});
|
|
|
|
it('should return complete stats for metric', () => {
|
|
const instance = PerformanceMonitor.getInstance();
|
|
for (let i = 1; i <= 100; i++) {
|
|
instance.recordMetric('test-metric', i);
|
|
}
|
|
|
|
const stats = instance.getStats('test-metric');
|
|
|
|
expect(stats.count).toBe(100);
|
|
expect(stats.avg).toBe(50.5);
|
|
expect(stats.min).toBe(1);
|
|
expect(stats.max).toBe(100);
|
|
expect(stats.p50).toBe(50);
|
|
expect(stats.p95).toBe(95);
|
|
expect(stats.p99).toBe(99);
|
|
});
|
|
});
|
|
|
|
describe('clearMetrics', () => {
|
|
it('should clear specific metric', () => {
|
|
const instance = PerformanceMonitor.getInstance();
|
|
instance.recordMetric('metric1', 100);
|
|
instance.recordMetric('metric2', 200);
|
|
|
|
instance.clearMetrics('metric1');
|
|
|
|
expect(instance.getCount('metric1')).toBe(0);
|
|
expect(instance.getCount('metric2')).toBe(1);
|
|
});
|
|
|
|
it('should clear all metrics when no name provided', () => {
|
|
const instance = PerformanceMonitor.getInstance();
|
|
instance.recordMetric('metric1', 100);
|
|
instance.recordMetric('metric2', 200);
|
|
instance.recordMetric('metric3', 300);
|
|
|
|
instance.clearMetrics();
|
|
|
|
expect(instance.getCount('metric1')).toBe(0);
|
|
expect(instance.getCount('metric2')).toBe(0);
|
|
expect(instance.getCount('metric3')).toBe(0);
|
|
});
|
|
});
|
|
}); |