Skip to content

Instantly share code, notes, and snippets.

@OlivierAlbertini
Created January 22, 2020 16:59
Show Gist options
  • Save OlivierAlbertini/9505254b713b9db5f60168ed3fa4e437 to your computer and use it in GitHub Desktop.
Save OlivierAlbertini/9505254b713b9db5f60168ed3fa4e437 to your computer and use it in GitHub Desktop.
'use strict';
const benchmark = require('./benchmark');
const { CanonicalCode } = require('@opentelemetry/types')
const parseResponseStatus = (
statusCode
) => {
if (statusCode < 400) {
return { code: CanonicalCode.OK };
} else {
switch (statusCode) {
case 404:
return { code: CanonicalCode.NOT_FOUND };
case 403:
return { code: CanonicalCode.PERMISSION_DENIED };
case 401:
return { code: CanonicalCode.UNAUTHENTICATED };
case 429:
return { code: CanonicalCode.RESOURCE_EXHAUSTED };
case 501:
return { code: CanonicalCode.UNIMPLEMENTED };
case 503:
return { code: CanonicalCode.UNAVAILABLE };
case 504:
return { code: CanonicalCode.DEADLINE_EXCEEDED };
default:
if (statusCode < 500) {
return { code: CanonicalCode.INVALID_ARGUMENT };
} else if (statusCode < 512 || statusCode === 598 || statusCode === 599) {
return { code: CanonicalCode.INTERNAL };
}
return { code: CanonicalCode.UNKNOWN };
}
}
};
const parseResponseStatusIfElse = (
statusCode
) => {
if (statusCode < 400) {
return { code: CanonicalCode.OK };
} else if (statusCode === 404) {
return { code: CanonicalCode.NOT_FOUND };
} else if (statusCode === 403) {
return { code: CanonicalCode.PERMISSION_DENIED };
} else if (statusCode === 401) {
return { code: CanonicalCode.UNAUTHENTICATED };
} else if (statusCode === 429) {
return { code: CanonicalCode.RESOURCE_EXHAUSTED };
} else if (statusCode === 501) {
return { code: CanonicalCode.UNIMPLEMENTED };
} else if (statusCode === 503) {
return { code: CanonicalCode.UNAVAILABLE };
} else if (statusCode === 504) {
return { code: CanonicalCode.DEADLINE_EXCEEDED };
} else if (statusCode < 500) {
return { code: CanonicalCode.INVALID_ARGUMENT };
} else if (statusCode < 512 || statusCode === 598 || statusCode === 599) {
return { code: CanonicalCode.INTERNAL };
}
return { code: CanonicalCode.UNKNOWN };
};
[100, 200, 302, 400, 401, 403, 404, 429, 501, 503, 504, 505, 599, 600].forEach((statusCode) => {
const suite = benchmark()
.add(`${statusCode} IfElseSwitch`, function () {
parseResponseStatus(statusCode)
})
.add(`${statusCode} IfElse`, function () {
parseResponseStatusIfElse(statusCode)
})
// run async
suite.run({ async: false });
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment