forked from help-me-mom/ng-mocks
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(mock-render): caching generated component help-me-mom#731
- Loading branch information
Showing
13 changed files
with
673 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
import { | ||
Component, | ||
Directive, | ||
Injectable, | ||
NgModule, | ||
} from '@angular/core'; | ||
import { TestBed } from '@angular/core/testing'; | ||
import { MockBuilder, ngMocks } from 'ng-mocks'; | ||
|
||
@Injectable() | ||
class TargetService { | ||
public readonly name = 'target'; | ||
} | ||
|
||
@Directive({ | ||
selector: 'target', | ||
}) | ||
class TargetDirective {} | ||
|
||
@Component({ | ||
selector: 'target', | ||
template: '{{ service.name }}', | ||
}) | ||
class TargetComponent { | ||
public constructor(public readonly service: TargetService) {} | ||
} | ||
|
||
@NgModule({ | ||
declarations: [TargetComponent, TargetDirective], | ||
exports: [TargetComponent], | ||
providers: [TargetService], | ||
}) | ||
class TargetModule {} | ||
|
||
describe('performance:MockBuilder', () => { | ||
let timeStandard = 0; | ||
let timeMockBuilder = 0; | ||
let timeFasterBeforeEach = 0; | ||
let timeFasterBeforeAll = 0; | ||
|
||
jasmine.getEnv().addReporter({ | ||
jasmineDone: () => { | ||
// tslint:disable-next-line no-console | ||
console.log(`performance:MockBuilder`); | ||
// tslint:disable-next-line no-console | ||
console.log(`Time standard: ${timeStandard}`); | ||
// tslint:disable-next-line no-console | ||
console.log(`Time MockBuilder: ${timeMockBuilder}`); | ||
// tslint:disable-next-line no-console | ||
console.log( | ||
`Ration standard / MockBuilder: ${ | ||
timeStandard / timeMockBuilder | ||
}`, | ||
); | ||
// tslint:disable-next-line no-console | ||
console.log(`Time beforeEach: ${timeFasterBeforeEach}`); | ||
// tslint:disable-next-line no-console | ||
console.log( | ||
`Ratio standard / beforeEach: ${ | ||
timeStandard / timeFasterBeforeEach | ||
}`, | ||
); | ||
// tslint:disable-next-line no-console | ||
console.log(`Time beforeAll: ${timeFasterBeforeAll}`); | ||
// tslint:disable-next-line no-console | ||
console.log( | ||
`Ratio beforeEach / beforeAll: ${ | ||
timeFasterBeforeEach / timeFasterBeforeAll | ||
}`, | ||
); | ||
}, | ||
}); | ||
|
||
describe('standard', () => { | ||
beforeAll(() => (timeStandard = Date.now())); | ||
afterAll(() => (timeStandard = Date.now() - timeStandard)); | ||
|
||
beforeEach(() => | ||
TestBed.configureTestingModule({ | ||
imports: [TargetModule], | ||
}).compileComponents(), | ||
); | ||
|
||
for (let i = 0; i < 100; i += 1) { | ||
it(`#${i}`, () => { | ||
const fixture = TestBed.createComponent(TargetComponent); | ||
fixture.detectChanges(); | ||
expect(ngMocks.formatText(fixture)).toEqual('target'); | ||
}); | ||
} | ||
}); | ||
|
||
describe('faster:MockBuilder', () => { | ||
beforeAll(() => (timeMockBuilder = Date.now())); | ||
afterAll(() => (timeMockBuilder = Date.now() - timeMockBuilder)); | ||
|
||
beforeEach(() => | ||
MockBuilder([TargetComponent, TargetService], TargetModule), | ||
); | ||
|
||
for (let i = 0; i < 100; i += 1) { | ||
it(`#${i}`, () => { | ||
const fixture = TestBed.createComponent(TargetComponent); | ||
fixture.detectChanges(); | ||
expect(ngMocks.formatText(fixture)).toEqual('target'); | ||
}); | ||
} | ||
}); | ||
|
||
describe('faster:beforeEach', () => { | ||
ngMocks.faster(); | ||
|
||
beforeAll(() => (timeFasterBeforeEach = Date.now())); | ||
afterAll( | ||
() => | ||
(timeFasterBeforeEach = Date.now() - timeFasterBeforeEach), | ||
); | ||
|
||
beforeEach(() => | ||
MockBuilder([TargetComponent, TargetService], TargetModule), | ||
); | ||
|
||
for (let i = 0; i < 100; i += 1) { | ||
it(`#${i}`, () => { | ||
const fixture = TestBed.createComponent(TargetComponent); | ||
fixture.detectChanges(); | ||
expect(ngMocks.formatText(fixture)).toEqual('target'); | ||
}); | ||
} | ||
}); | ||
|
||
describe('faster:beforeAll', () => { | ||
ngMocks.faster(); | ||
|
||
beforeAll(() => (timeFasterBeforeAll = Date.now())); | ||
afterAll( | ||
() => (timeFasterBeforeAll = Date.now() - timeFasterBeforeAll), | ||
); | ||
|
||
beforeAll(() => | ||
MockBuilder([TargetComponent, TargetService], TargetModule), | ||
); | ||
|
||
for (let i = 0; i < 100; i += 1) { | ||
it(`#${i}`, () => { | ||
const fixture = TestBed.createComponent(TargetComponent); | ||
fixture.detectChanges(); | ||
expect(ngMocks.formatText(fixture)).toEqual('target'); | ||
}); | ||
} | ||
}); | ||
|
||
it('ensures that faster is faster', () => { | ||
// Usually, it is faster, but it is fine if we are down for 25% | ||
expect(timeStandard / timeFasterBeforeEach).toBeGreaterThan(0.75); | ||
|
||
// beforeEach should be definitely slower than beforeAll | ||
expect( | ||
timeFasterBeforeEach / timeFasterBeforeAll, | ||
).toBeGreaterThan(0.75); | ||
|
||
// without faster should be always slower | ||
expect(timeMockBuilder / timeFasterBeforeEach).toBeGreaterThan( | ||
0.75, | ||
); | ||
}); | ||
}); |
Oops, something went wrong.