Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mriccia committed May 27, 2024
1 parent 38f5c96 commit d837b53
Showing 1 changed file with 77 additions and 8 deletions.
85 changes: 77 additions & 8 deletions test/unit/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,15 +350,15 @@ describe('Lambda Utils', () => {

describe('parseLogAndExtractDurations', () => {
const results = [
// 1s (will be discarded)
// Duration 1ms
{ StatusCode: 200, LogResult: 'U1RBUlQgUmVxdWVzdElkOiA0NzlmYjUxYy0xZTM4LTExZTctOTljYS02N2JmMTYzNjA4ZWQgVmVyc2lvbjogOTkKMjAxNy0wNC0xMFQyMTo1NDozMi42ODNaCTQ3OWZiNTFjLTFlMzgtMTFlNy05OWNhLTY3YmYxNjM2MDhlZAl2YWx1ZTEgPSB1bmRlZmluZWQKMjAxNy0wNC0xMFQyMTo1NDozMi42ODNaCTQ3OWZiNTFjLTFlMzgtMTFlNy05OWNhLTY3YmYxNjM2MDhlZAl2YWx1ZTIgPSB1bmRlZmluZWQKMjAxNy0wNC0xMFQyMTo1NDozMi42ODNaCTQ3OWZiNTFjLTFlMzgtMTFlNy05OWNhLTY3YmYxNjM2MDhlZAl2YWx1ZTMgPSB1bmRlZmluZWQKRU5EIFJlcXVlc3RJZDogNDc5ZmI1MWMtMWUzOC0xMWU3LTk5Y2EtNjdiZjE2MzYwOGVkClJFUE9SVCBSZXF1ZXN0SWQ6IDQ3OWZiNTFjLTFlMzgtMTFlNy05OWNhLTY3YmYxNjM2MDhlZAlEdXJhdGlvbjogMS4wIG1zCUJpbGxlZCBEdXJhdGlvbjogMSBtcyAJTWVtb3J5IFNpemU6IDEyOCBNQglNYXggTWVtb3J5IFVzZWQ6IDE1IE1C', Payload: 'null' },
// 1s
// Duration 1ms
{ StatusCode: 200, LogResult: 'U1RBUlQgUmVxdWVzdElkOiA0NzlmYjUxYy0xZTM4LTExZTctOTljYS02N2JmMTYzNjA4ZWQgVmVyc2lvbjogOTk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TM4LTExZTctOTljYS02N2JmMTYzNjA4ZWQgVmVyc2lvbjogOTkKMjAxNy0wNC0xMFQyMTo1NDozMi42ODNaCTQ3OWZiNTFjLTFlMzgtMTFlNy05OWNhLTY3YmYxNjM2MDhlZAl2YWx1ZTEgPSB1bmRlZmluZWQKMjAxNy0wNC0xMFQyMTo1NDozMi42ODNaCTQ3OWZiNTFjLTFlMzgtMTFlNy05OWNhLTY3YmYxNjM2MDhlZAl2YWx1ZTIgPSB1bmRlZmluZWQKMjAxNy0wNC0xMFQyMTo1NDozMi42ODNaCTQ3OWZiNTFjLTFlMzgtMTFlNy05OWNhLTY3YmYxNjM2MDhlZAl2YWx1ZTMgPSB1bmRlZmluZWQKRU5EIFJlcXVlc3RJZDogNDc5ZmI1MWMtMWUzOC0xMWU3LTk5Y2EtNjdiZjE2MzYwOGVkClJFUE9SVCBSZXF1ZXN0SWQ6IDQ3OWZiNTFjLTFlMzgtMTFlNy05OWNhLTY3YmYxNjM2MDhlZAlEdXJhdGlvbjogMy4wIG1zCUJpbGxlZCBEdXJhdGlvbjogMyBtcyAJTWVtb3J5IFNpemU6IDEyOCBNQglNYXggTWVtb3J5IFVzZWQ6IDE1IE1C', Payload: 'null' },
];

Expand All @@ -382,11 +382,80 @@ describe('Lambda Utils', () => {
expect(durations).to.eql([0]);
});

// TODO add test to verify it adds initDuration + duration
// TODO add test to verify it adds restoreDuration + duration
it('should give duration as initDuration + duration', () => {
const resultWithInitDuration =
{
StatusCode: 200,
// Duration: 469.40 ms Init Duration: 100.99 ms
LogResult: Buffer.from(textLog).toString('base64'),
};
const durations = utils.parseLogAndExtractDurations([resultWithInitDuration]);
expect(durations).to.be.a('array');
expect(durations.length).to.be(1);
expect(durations).to.eql([570.39]);
});

it('should give duration as restoreDuration + duration (for SnapStart)', () => {
const resultWithInitDuration =
{
StatusCode: 200,
// Duration: 469.40 ms - Restore Duration: 474.16 ms
LogResult: Buffer.from(textLogSnapStart).toString('base64'),
};
const durations = utils.parseLogAndExtractDurations([resultWithInitDuration]);
expect(durations).to.be.a('array');
expect(durations.length).to.be(1);
expect(durations).to.eql([943.56]);
});
});
describe('parseLogAndExtractBilledDurations', () => {
const results = [
// Billed Duration 1ms
{ StatusCode: 200, LogResult: 'U1RBUlQgUmVxdWVzdElkOiA0NzlmYjUxYy0xZTM4LTExZTctOTljYS02N2JmMTYzNjA4ZWQgVmVyc2lvbjogOTkKMjAxNy0wNC0xMFQyMTo1NDozMi42ODNaCTQ3OWZiNTFjLTFlMzgtMTFlNy05OWNhLTY3YmYxNjM2MDhlZAl2YWx1ZTEgPSB1bmRlZmluZWQKMjAxNy0wNC0xMFQyMTo1NDozMi42ODNaCTQ3OWZiNTFjLTFlMzgtMTFlNy05OWNhLTY3YmYxNjM2MDhlZAl2YWx1ZTIgPSB1bmRlZmluZWQKMjAxNy0wNC0xMFQyMTo1NDozMi42ODNaCTQ3OWZiNTFjLTFlMzgtMTFlNy05OWNhLTY3YmYxNjM2MDhlZAl2YWx1ZTMgPSB1bmRlZmluZWQKRU5EIFJlcXVlc3RJZDogNDc5ZmI1MWMtMWUzOC0xMWU3LTk5Y2EtNjdiZjE2MzYwOGVkClJFUE9SVCBSZXF1ZXN0SWQ6IDQ3OWZiNTFjLTFlMzgtMTFlNy05OWNhLTY3YmYxNjM2MDhlZAlEdXJhdGlvbjogMS4wIG1zCUJpbGxlZCBEdXJhdGlvbjogMSBtcyAJTWVtb3J5IFNpemU6IDEyOCBNQglNYXggTWVtb3J5IFVzZWQ6IDE1IE1C', Payload: 'null' },
// Billed Duration 1ms
{ StatusCode: 200, LogResult: 'U1RBUlQgUmVxdWVzdElkOiA0NzlmYjUxYy0xZTM4LTExZTctOTljYS02N2JmMTYzNjA4ZWQgVmVyc2lvbjogOTkKMjAxNy0wNC0xMFQyMTo1NDozMi42ODNaCTQ3OWZiNTFjLTFlMzgtMTFlNy05OWNhLTY3YmYxNjM2MDhlZAl2YWx1ZTEgPSB1bmRlZmluZWQKMjAxNy0wNC0xMFQy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1bmRlZmluZWQKMjAxNy0wNC0xMFQyMTo1NDozMi42ODNaCTQ3OWZiNTFjLTFlMzgtMTFlNy05OWNhLTY3YmYxNjM2MDhlZAl2YWx1ZTIgPSB1bmRlZmluZWQKMjAxNy0wNC0xMFQyMTo1NDozMi42ODNaCTQ3OWZiNTFjLTFlMzgtMTFlNy05OWNhLTY3YmYxNjM2MDhlZAl2YWx1ZTMgPSB1bmRlZmluZWQKRU5EIFJlcXVlc3RJZDogNDc5ZmI1MWMtMWUzOC0xMWU3LTk5Y2EtNjdiZjE2MzYwOGVkClJFUE9SVCBSZXF1ZXN0SWQ6IDQ3OWZiNTFjLTFlMzgtMTFlNy05OWNhLTY3YmYxNjM2MDhlZAlEdXJhdGlvbjogMy4wIG1zCUJpbGxlZCBEdXJhdGlvbjogMyBtcyAJTWVtb3J5IFNpemU6IDEyOCBNQglNYXggTWVtb3J5IFVzZWQ6IDE1IE1C', Payload: 'null' },
];

it('should return the list of billed durations', () => {
const durations = utils.parseLogAndExtractBilledDurations(results);
expect(durations).to.be.a('array');
expect(durations.length).to.be(5);
expect(durations).to.eql([1, 1, 2, 3, 3]);
});
it('should return empty list if empty results', () => {
const durations = utils.parseLogAndExtractBilledDurations([]);
expect(durations).to.be.an('array');
expect(durations.length).to.be(0);
});

// TODO add tests for parseLogAndExtractBilledDurations
it('should not explode if missing logs', () => {
const durations = utils.parseLogAndExtractBilledDurations([
{ StatusCode: 200, Payload: 'null' },
]);
expect(durations).to.be.an('array');
expect(durations).to.eql([0]);
});


it('should give duration as billedDuration + restoreDuration (for SnapStart)', () => {
const resultWithInitDuration =
{
StatusCode: 200,
// Billed Duration: 500 ms Billed Restore Duration: 75 ms
LogResult: Buffer.from(textLogSnapStart).toString('base64'),
};
const durations = utils.parseLogAndExtractBilledDurations([resultWithInitDuration]);
expect(durations).to.be.a('array');
expect(durations.length).to.be(1);
expect(durations).to.eql([575]);
});
});

describe('computeAverageDuration', () => {
const durations = [
Expand Down

0 comments on commit d837b53

Please sign in to comment.