mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-16 03:31:45 +08:00

* chore: move tns-core-modules to nativescript-core * chore: preparing compat generate script * chore: add missing definitions * chore: no need for http-request to be private * chore: packages chore * test: generate tests for tns-core-modules * chore: add anroid module for consistency * chore: add .npmignore * chore: added privateModulesWhitelist * chore(webpack): added bundle-entry-points * chore: scripts * chore: tests changed to use @ns/core * test: add scoped-packages test project * test: fix types * test: update test project * chore: build scripts * chore: update build script * chore: npm scripts cleanup * chore: make the compat pgk work with old wp config * test: generate diff friendly tests * chore: create barrel exports * chore: move files after rebase * chore: typedoc config * chore: compat mode * chore: review of barrels * chore: remove tns-core-modules import after rebase * chore: dev workflow setup * chore: update developer-workflow * docs: experiment with API extractor * chore: api-extractor and barrel exports * chore: api-extractor configs * chore: generate d.ts rollup with api-extractor * refactor: move methods inside Frame * chore: fic tests to use Frame static methods * refactor: create Builder class * refactor: use Builder class in tests * refactor: include Style in ui barrel * chore: separate compat build script * chore: fix tslint errors * chore: update NATIVESCRIPT_CORE_ARGS * chore: fix compat pack * chore: fix ui-test-app build with linked modules * chore: Application, ApplicationSettings, Connectivity and Http * chore: export Trace, Profiling and Utils * refactor: Static create methods for ImageSource * chore: fix deprecated usages of ImageSource * chore: move Span and FormattedString to ui * chore: add events-args and ImageSource to index files * chore: check for CLI >= 6.2 when building for IOS * chore: update travis build * chore: copy Pod file to compat package * chore: update error msg ui-tests-app * refactor: Apply suggestions from code review Co-Authored-By: Martin Yankov <m.i.yankov@gmail.com> * chore: typings and refs * chore: add missing d.ts files for public API * chore: adress code review FB * chore: update api-report * chore: dev-workflow for other apps * chore: api update * chore: update api-report
212 lines
5.2 KiB
TypeScript
212 lines
5.2 KiB
TypeScript
import { assert, assertEqual, assertFalse, assertNull, assertTrue, assertThrows } from "../tk-unit";
|
|
import { enable, disable, profile, time, start, stop, timer, isRunning, resetProfiles } from "@nativescript/core/profiling";
|
|
|
|
enable();
|
|
class TestClass {
|
|
@profile("__func_decorator__")
|
|
doNothing() {
|
|
//noop
|
|
}
|
|
|
|
@profile("__func_decorator_error__")
|
|
throwError() {
|
|
throw new Error("This error is expected");
|
|
}
|
|
|
|
@profile
|
|
unnamed1() {
|
|
// noop
|
|
}
|
|
|
|
@profile()
|
|
unnamed2() {
|
|
// noop
|
|
}
|
|
|
|
private isInReentrant = false;
|
|
|
|
@profile
|
|
reentrant() {
|
|
try {
|
|
if (!this.isInReentrant) {
|
|
this.isInReentrant = true;
|
|
this.reentrant();
|
|
}
|
|
} finally {
|
|
this.isInReentrant = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
const testFunction1 = profile(function testFunction1() {
|
|
// noop
|
|
});
|
|
const testFunction2 = profile("testFunction2", () => {
|
|
// noop
|
|
});
|
|
disable();
|
|
|
|
function retry(count: number, action: () => void) {
|
|
for (var i = 1; i <= count; i++) {
|
|
try {
|
|
action();
|
|
|
|
return;
|
|
} catch (e) {
|
|
if (i === count) {
|
|
throw e;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
export function test_time_returns_number() {
|
|
assertEqual(typeof time(), "number");
|
|
}
|
|
|
|
export function test_isRunning() {
|
|
resetProfiles();
|
|
const name = "test_isRunning";
|
|
assertFalse(isRunning(name), "isRunning should be false before start");
|
|
|
|
start(name);
|
|
assertTrue(isRunning(name), "isRunning should be true after start");
|
|
|
|
stop(name);
|
|
assertFalse(isRunning(name), "isRunning should be false after stop");
|
|
|
|
start(name);
|
|
assertTrue(isRunning(name), "isRunning should be true after second start");
|
|
|
|
stop(name);
|
|
assertFalse(isRunning(name), "isRunning should be false after second stop");
|
|
}
|
|
|
|
export function test_isRunning_withReentrancy() {
|
|
resetProfiles();
|
|
const name = "test_isRunning";
|
|
assertFalse(isRunning(name), "isRunning should be false before start");
|
|
|
|
start(name);
|
|
assertTrue(isRunning(name), "isRunning should be true after start");
|
|
|
|
start(name);
|
|
assertTrue(isRunning(name), "isRunning should be true after second start");
|
|
|
|
stop(name);
|
|
assertTrue(isRunning(name), "isRunning should be true after first stop");
|
|
|
|
stop(name);
|
|
assertFalse(isRunning(name), "isRunning should be false after second stop");
|
|
}
|
|
|
|
export function test_reset_profiles() {
|
|
resetProfiles();
|
|
const name = "test_reset_profiles";
|
|
|
|
start(name);
|
|
stop(name);
|
|
resetProfiles();
|
|
|
|
const res = timer(name);
|
|
|
|
assertNull(res);
|
|
}
|
|
|
|
export function test_start_stop() {
|
|
resetProfiles();
|
|
const name = "test_start_stop";
|
|
|
|
start(name);
|
|
stop(name);
|
|
const res = timer(name);
|
|
|
|
assertEqual(res.count, 1);
|
|
}
|
|
|
|
export function test_start_stop_count() {
|
|
resetProfiles();
|
|
const name = "test_start_stop_count";
|
|
|
|
for (var i = 0; i < 10; i++) {
|
|
start(name);
|
|
stop(name);
|
|
}
|
|
|
|
const res = timer(name);
|
|
assertEqual(res.count, 10);
|
|
}
|
|
|
|
export function test_profile_decorator_count() {
|
|
resetProfiles();
|
|
const test = new TestClass();
|
|
for (var i = 0; i < 10; i++) {
|
|
test.doNothing();
|
|
test.unnamed1();
|
|
test.unnamed2();
|
|
testFunction1();
|
|
testFunction2();
|
|
}
|
|
|
|
["__func_decorator__", "TestClass.unnamed1", "TestClass.unnamed2", "testFunction1", "testFunction2"].forEach(key => {
|
|
const res = timer(key);
|
|
assertEqual(res.count, 10, "Expected profile with name ${key} to have traced 10 calls.");
|
|
});
|
|
}
|
|
|
|
export function test_profile_decorator_handles_exceptions() {
|
|
resetProfiles();
|
|
const test = new TestClass();
|
|
|
|
assertThrows(() => test.throwError());
|
|
assertFalse(isRunning("__func_decorator_error__"), "Timer should be stopped on exception.");
|
|
assertEqual(timer("__func_decorator_error__").count, 1, "Timer should be called once");
|
|
}
|
|
|
|
export function test_start_stop_performance() {
|
|
retry(5, () => {
|
|
resetProfiles();
|
|
|
|
const count = 10000;
|
|
const name = "test_start_stop_performance";
|
|
|
|
for (var i = 0; i < count; i++) {
|
|
start(name);
|
|
stop(name);
|
|
}
|
|
|
|
const res = timer(name);
|
|
assertEqual(res.count, count);
|
|
assert(res.totalTime <= 500, `Total time for ${count} timer operations is too much: ${res.totalTime}`);
|
|
});
|
|
}
|
|
|
|
export function test_profile_decorator_performance() {
|
|
retry(5, () => {
|
|
resetProfiles();
|
|
|
|
var start = Date.now();
|
|
const count = 10000;
|
|
const test = new TestClass();
|
|
for (var i = 0; i < count; i++) {
|
|
test.doNothing();
|
|
}
|
|
|
|
const res = timer("__func_decorator__");
|
|
assertEqual(res.count, count);
|
|
|
|
assert(res.totalTime <= 500, `Total time for ${count} timer operations is too much: ${res.totalTime}`);
|
|
var end = Date.now();
|
|
assert(end - start <= 1000, `Total time for test execution is too much: ${end - start}ms`);
|
|
});
|
|
}
|
|
|
|
export function test_reentrancy() {
|
|
// reentrant
|
|
retry(5, () => {
|
|
resetProfiles();
|
|
const test = new TestClass();
|
|
test.reentrant();
|
|
});
|
|
}
|