Skip to content
Snippets Groups Projects
Select Git revision
  • b69416ce1745f67c9fc1d149738e2f52feb4f732
  • main default protected
  • renovate/main-linters
  • renovate/next-ghcr.io-renovatebot-base-image-12.x
  • renovate/main-majornode-24.x
  • next-major
  • docs/mra-disable
  • fix/drop-legacy-encryption
  • feat/bcpgp
  • renovate/main-semver-7.x
  • renovate/main-ghcr.io-renovatebot-base-image-12.x
  • fix/validator
  • feat/mra-update-type
  • jamietanna-patch-1
  • copilot/sub-pr-39032
  • renovate/main-docs-renovate-renovate-41.x
  • template/version
  • chore/hosttype
  • docs/major
  • next
  • chore/span-split
  • 41.173.0
  • 41.172.2
  • 41.172.1
  • 41.172.0
  • 41.171.9
  • 41.171.8
  • 41.171.7
  • 41.171.6
  • 41.171.5
  • 41.171.4
  • 41.171.3
  • 41.171.2
  • 41.171.1
  • 41.171.0
  • 41.170.1
  • 41.170.0
  • 41.169.4
  • 41.169.3
  • 41.169.2
  • 41.169.1
41 results

stats.spec.ts

Blame
  • user avatar
    Rhys Arkins authored
    36e2b328
    History
    stats.spec.ts 6.28 KiB
    import { logger, mocked } from '../../../test/util';
    import type { Logger } from '../../logger/types';
    import * as _memCache from '../../util/cache/memory';
    import type { LookupStats } from '../../util/cache/memory/types';
    import type { RequestStats } from '../../util/http/types';
    import { printLookupStats, printRequestStats } from './stats';
    
    jest.mock('../../util/cache/memory');
    
    const memCache = mocked(_memCache);
    const log = logger.logger as jest.Mocked<Logger>;
    
    describe('workers/repository/stats', () => {
      describe('printLookupStats()', () => {
        it('runs', () => {
          const stats: LookupStats[] = [
            {
              datasource: 'npm',
              duration: 100,
            },
            {
              datasource: 'npm',
              duration: 200,
            },
            {
              datasource: 'docker',
              duration: 1000,
            },
          ];
          memCache.get.mockImplementationOnce(() => stats as any);
          expect(printLookupStats()).toBeUndefined();
          expect(log.debug).toHaveBeenCalledTimes(1);
          expect(log.debug.mock.calls[0][0]).toMatchInlineSnapshot(`
            {
              "docker": {
                "averageMs": 1000,
                "count": 1,
                "maximumMs": 1000,
                "totalMs": 1000,
              },
              "npm": {
                "averageMs": 150,
                "count": 2,
                "maximumMs": 200,
                "totalMs": 300,
              },
            }
          `);
        });
      });
    
      describe('printRequestStats()', () => {
        it('runs', () => {
          const getStats: number[] = [30, 100, 10, 20];
          // TODO: fix types, jest is using wrong overload (#22198)
          memCache.get.mockImplementationOnce(() => getStats as any);
          const setStats: number[] = [110, 80, 20];
          // TODO: fix types, jest is using wrong overload (#22198)
          memCache.get.mockImplementationOnce(() => setStats as any);
          const httpStats: RequestStats[] = [
            {
              method: 'get',
              url: 'https://api.github.com/api/v3/user',
              duration: 100,
              queueDuration: 0,
              statusCode: 200,
            },
            {
              method: 'post',
              url: 'https://api.github.com/graphql',
              duration: 130,
              queueDuration: 0,
              statusCode: 401,
            },
            {
              method: 'post',
              url: 'https://api.github.com/graphql',
              duration: 150,
              queueDuration: 0,
              statusCode: 200,
            },
            {
              method: 'post',
              url: 'https://api.github.com/graphql',
              duration: 20,
              queueDuration: 10,
              statusCode: 200,
            },
            {
              method: 'get',
              url: 'https://api.github.com/api/v3/repositories',
              duration: 500,
              queueDuration: 0,
              statusCode: 500,
            },
            {
              method: 'get',
              url: 'https://auth.docker.io',
              duration: 200,
              queueDuration: 0,
              statusCode: 401,
            },
          ];
          // TODO: fix types, jest is using wrong overload (#22198)
          memCache.get.mockImplementationOnce(() => httpStats as any);
          expect(printRequestStats()).toBeUndefined();
          expect(log.trace).toHaveBeenCalledOnce();
          expect(log.debug).toHaveBeenCalledTimes(2);
          expect(log.trace.mock.calls[0][0]).toMatchInlineSnapshot(`
            {
              "allRequests": [
                "GET https://api.github.com/api/v3/repositories 500 500 0",
                "GET https://api.github.com/api/v3/user 200 100 0",
                "POST https://api.github.com/graphql 401 130 0",
                "POST https://api.github.com/graphql 200 150 0",
                "POST https://api.github.com/graphql 200 20 10",
                "GET https://auth.docker.io 401 200 0",
              ],
              "requestHosts": {
                "api.github.com": [
                  {
                    "duration": 500,
                    "method": "get",
                    "queueDuration": 0,
                    "statusCode": 500,
                    "url": "https://api.github.com/api/v3/repositories",
                  },
                  {
                    "duration": 100,
                    "method": "get",
                    "queueDuration": 0,
                    "statusCode": 200,
                    "url": "https://api.github.com/api/v3/user",
                  },
                  {
                    "duration": 130,
                    "method": "post",
                    "queueDuration": 0,
                    "statusCode": 401,
                    "url": "https://api.github.com/graphql",
                  },
                  {
                    "duration": 150,
                    "method": "post",
                    "queueDuration": 0,
                    "statusCode": 200,
                    "url": "https://api.github.com/graphql",
                  },
                  {
                    "duration": 20,
                    "method": "post",
                    "queueDuration": 10,
                    "statusCode": 200,
                    "url": "https://api.github.com/graphql",
                  },
                ],
                "auth.docker.io": [
                  {
                    "duration": 200,
                    "method": "get",
                    "queueDuration": 0,
                    "statusCode": 401,
                    "url": "https://auth.docker.io",
                  },
                ],
              },
            }
          `);
          expect(log.debug.mock.calls[0][0]).toMatchInlineSnapshot(`
            {
              "get": {
                "avgMs": 40,
                "count": 4,
                "maxMs": 100,
                "medianMs": 20,
              },
              "set": {
                "avgMs": 70,
                "count": 3,
                "maxMs": 110,
                "medianMs": 80,
              },
            }
          `);
          expect(log.debug.mock.calls[1][0]).toMatchInlineSnapshot(`
            {
              "hostStats": {
                "api.github.com": {
                  "queueAvgMs": 2,
                  "requestAvgMs": 180,
                  "requestCount": 5,
                },
                "auth.docker.io": {
                  "queueAvgMs": 0,
                  "requestAvgMs": 200,
                  "requestCount": 1,
                },
              },
              "totalRequests": 6,
              "urls": {
                "https://api.github.com/api/v3/repositories (GET,500)": 1,
                "https://api.github.com/api/v3/user (GET,200)": 1,
                "https://api.github.com/graphql (POST,200)": 2,
                "https://api.github.com/graphql (POST,401)": 1,
                "https://auth.docker.io (GET,401)": 1,
              },
            }
          `);
        });
      });
    });