From 7253b97d42aa78ef43257bb84b29be74fee322c4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Stephan=20M=C3=BCller?= Date: Fri, 1 Jun 2018 17:11:35 +0200 Subject: [PATCH] mgr/dashboard: Resolve TestBed performance issue MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit With this helper function you can easily resolve the TestBed resetting performance issue. If more tests exists in a test suite, it makes sense to configure TestBed only once if you are not doing a lot of TestBed specific stuff (haven't hit the limitation). It will reduce the test run time by around $tests * 50 %. In my case it was a test suite with 47 tests with a run time of over 30s after using the static test bed method it ran in 1.2s. The run time was reduced to 0.04 %! This is equivalent to a speed increase of 2500% (100/0.04)! For our own security the normal way will be taken if you not set the _DEV_ configuration variable to true. It will be false when "run-frontend-unittests.sh" is run. Signed-off-by: Stephan Müller --- src/pybind/mgr/dashboard/HACKING.rst | 4 +++ src/pybind/mgr/dashboard/frontend/.gitignore | 1 + .../src/app/shared/unit-test-helper.ts | 25 +++++++++++++++++++ .../dashboard/frontend/src/tsconfig.app.json | 1 + .../src/unit-test-configuration.ts.sample | 1 + .../mgr/dashboard/run-frontend-unittests.sh | 11 ++++++++ 6 files changed, 43 insertions(+) create mode 100644 src/pybind/mgr/dashboard/frontend/src/app/shared/unit-test-helper.ts create mode 100644 src/pybind/mgr/dashboard/frontend/src/unit-test-configuration.ts.sample diff --git a/src/pybind/mgr/dashboard/HACKING.rst b/src/pybind/mgr/dashboard/HACKING.rst index 79f34dc8b94..55841270da3 100644 --- a/src/pybind/mgr/dashboard/HACKING.rst +++ b/src/pybind/mgr/dashboard/HACKING.rst @@ -66,6 +66,10 @@ production build. Navigate to ``https://localhost:8443``. Running Unit Tests ~~~~~~~~~~~~~~~~~~ +Create ``unit-test-configuration.ts`` file based on +``unit-test-configuration.ts.sample`` in directory +``src/pybind/mgr/dashboard/frontend/src``. + Run ``npm run test`` to execute the unit tests via `Jest `_. diff --git a/src/pybind/mgr/dashboard/frontend/.gitignore b/src/pybind/mgr/dashboard/frontend/.gitignore index 2e55dc6354d..8f33628cb88 100644 --- a/src/pybind/mgr/dashboard/frontend/.gitignore +++ b/src/pybind/mgr/dashboard/frontend/.gitignore @@ -32,6 +32,7 @@ npm-debug.log testem.log /typings +/src/unit-test-configuration.ts # e2e /e2e/*.js diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/unit-test-helper.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/unit-test-helper.ts new file mode 100644 index 00000000000..6348045684c --- /dev/null +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/unit-test-helper.ts @@ -0,0 +1,25 @@ +import { async, TestBed } from '@angular/core/testing'; + +import { _DEV_ } from '../../unit-test-configuration'; + +export function configureTestBed(configuration, useOldMethod?) { + if (_DEV_ && !useOldMethod) { + const resetTestingModule = TestBed.resetTestingModule; + beforeAll((done) => + (async () => { + TestBed.resetTestingModule(); + TestBed.configureTestingModule(configuration); + // prevent Angular from resetting testing module + TestBed.resetTestingModule = () => TestBed; + })() + .then(done) + .catch(done.fail)); + afterAll(() => { + TestBed.resetTestingModule = resetTestingModule; + }); + } else { + beforeEach(async(() => { + TestBed.configureTestingModule(configuration); + })); + } +} diff --git a/src/pybind/mgr/dashboard/frontend/src/tsconfig.app.json b/src/pybind/mgr/dashboard/frontend/src/tsconfig.app.json index 39ba8dbacbb..719f72b9f83 100644 --- a/src/pybind/mgr/dashboard/frontend/src/tsconfig.app.json +++ b/src/pybind/mgr/dashboard/frontend/src/tsconfig.app.json @@ -7,6 +7,7 @@ "types": [] }, "exclude": [ + "app/shared/unit-test-helper.ts", "test.ts", "**/*.spec.ts" ] diff --git a/src/pybind/mgr/dashboard/frontend/src/unit-test-configuration.ts.sample b/src/pybind/mgr/dashboard/frontend/src/unit-test-configuration.ts.sample new file mode 100644 index 00000000000..74dbf2c0792 --- /dev/null +++ b/src/pybind/mgr/dashboard/frontend/src/unit-test-configuration.ts.sample @@ -0,0 +1 @@ +export const _DEV_ = false; diff --git a/src/pybind/mgr/dashboard/run-frontend-unittests.sh b/src/pybind/mgr/dashboard/run-frontend-unittests.sh index ea7522e4c73..010dc83ab97 100755 --- a/src/pybind/mgr/dashboard/run-frontend-unittests.sh +++ b/src/pybind/mgr/dashboard/run-frontend-unittests.sh @@ -4,10 +4,21 @@ set -e cd $CEPH_ROOT/src/pybind/mgr/dashboard/frontend +config='src/unit-test-configuration.ts' +if [ -e $config ]; then + mv $config ${config}_old +fi +cp ${config}.sample $config + . $CEPH_ROOT/build/src/pybind/mgr/dashboard/node-env/bin/activate npm run build -- --prod npm run test:ci npm run lint +rm $config +if [ -e ${config}_old ]; then + mv ${config}_old $config +fi + deactivate -- 2.39.5