Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

test: optimize comparison (#4498)

Wenzhao Hue457daae92ac
diff --git a/e2e/memory/memory.spec.ts b/e2e/memory/memory.spec.ts
index 5c34087e1c..56dd5b8d3c 100644
--- a/e2e/memory/memory.spec.ts
+++ b/e2e/memory/memory.spec.ts
@@ -14,8 +14,6 @@
* limitations under the License.
*/
-/* eslint-disable no-console */
-
import { expect, test } from '@playwright/test';
import { getMetrics } from './util';
@@ -24,36 +22,48 @@ const MAX_UNIT_MEMORY_OVERFLOW = 1_000_000; // 1MB
// There are some compiled code and global cache, so we make some room
// for this. But we need to make sure that a Univer object cannot fit
// in this size.
-const MAX_UNIVER_MEMORY_OVERFLOW = 5_300_000; // TODO@wzhudev: temporarily added 300KB
+const MAX_UNIVER_MEMORY_OVERFLOW = 6_000_000; // TODO@wzhudev: temporarily added 300KB
// there is a memory leak in the univer object, so we need to make sure that
+const MAX_SECOND_INSTANCE_OVERFLOW = 100_000; // Only 100 KB
+
test('memory', async ({ page }) => {
test.setTimeout(60_000);
await page.goto('http://localhost:3000/sheets/');
await page.waitForTimeout(5000);
- const memoryBeforeLoad = (await getMetrics(page)).JSHeapUsedSize;
- console.log('Memory before load (B):', memoryBeforeLoad);
+ const memoryAfterFirstInstance = (await getMetrics(page)).JSHeapUsedSize;
await page.evaluate(() => window.E2EControllerAPI.loadAndRelease(1));
- await page.waitForTimeout(5000);
+ await page.waitForTimeout(2000);
const memoryAfterFirstLoad = (await getMetrics(page)).JSHeapUsedSize;
- console.log('Memory after first load (B):', memoryAfterFirstLoad);
await page.evaluate(() => window.E2EControllerAPI.loadAndRelease(2));
- await page.waitForTimeout(5000);
+ await page.waitForTimeout(2000);
const memoryAfterSecondLoad = (await getMetrics(page)).JSHeapUsedSize;
- console.log('Memory after second load (B):', memoryAfterSecondLoad);
+ expect(memoryAfterSecondLoad - memoryAfterFirstLoad)
+ .toBeLessThanOrEqual(MAX_UNIT_MEMORY_OVERFLOW);
await page.evaluate(() => window.univer.dispose());
- await page.waitForTimeout(5000);
- const memoryAfterDisposingUniver = (await getMetrics(page)).JSHeapUsedSize;
- console.log('Memory after disposing univer (B):', memoryAfterDisposingUniver);
+ await page.waitForTimeout(2000);
+ const memoryAfterDisposingFirstInstance = (await getMetrics(page)).JSHeapUsedSize;
- const noUnitLeaking = memoryAfterSecondLoad - memoryAfterFirstLoad <= MAX_UNIT_MEMORY_OVERFLOW;
- expect(noUnitLeaking).toBeTruthy();
+ await page.evaluate(() => window.createNewInstance());
+ await page.waitForTimeout(2000);
+ await page.evaluate(() => window.univer.dispose());
+ await page.waitForTimeout(2000);
+ const memoryAfterDisposingSecondUniver = (await getMetrics(page)).JSHeapUsedSize;
+ expect(memoryAfterDisposingSecondUniver - memoryAfterDisposingFirstInstance)
+ .toBeLessThanOrEqual(MAX_SECOND_INSTANCE_OVERFLOW);
- const noUniverLeaking = memoryAfterDisposingUniver - memoryBeforeLoad <= MAX_UNIVER_MEMORY_OVERFLOW;
- expect(noUniverLeaking).toBeTruthy();
+ expect(memoryAfterDisposingSecondUniver - memoryAfterFirstInstance)
+ .toBeLessThanOrEqual(MAX_UNIVER_MEMORY_OVERFLOW);
});
+
+declare global {
+ // eslint-disable-next-line ts/naming-convention
+ interface Window {
+ createNewInstance: () => void;
+ }
+}
diff --git a/examples/src/sheets/main.ts b/examples/src/sheets/main.ts
index af9560c486..2c1ebb099d 100644
--- a/examples/src/sheets/main.ts
+++ b/examples/src/sheets/main.ts
@@ -44,6 +44,7 @@ import { UniverThreadCommentUIPlugin } from '@univerjs/thread-comment-ui';
import { UniverUIPlugin } from '@univerjs/ui';
import { enUS, faIR, frFR, ruRU, viVN, zhCN, zhTW } from '../locales';
+
import '@univerjs/sheets/facade';
import '@univerjs/ui/facade';
import '@univerjs/docs-ui/facade';
@@ -54,13 +55,13 @@ import '@univerjs/sheets-filter/facade';
import '@univerjs/sheets-formula/facade';
import '@univerjs/sheets-numfmt/facade';
import '@univerjs/sheets-hyper-link-ui/facade';
-
import '@univerjs/sheets-thread-comment/facade';
import '@univerjs/sheets-conditional-formatting/facade';
import '@univerjs/sheets-find-replace/facade';
import '@univerjs/sheets-drawing-ui/facade';
import '@univerjs/sheets-zen-editor/facade';
import '@univerjs/sheets-source-binding/facade';
+
import '../global.css';
import './styles';
@@ -78,91 +79,98 @@ export const mockUser = {
canBindAnonymous: false,
};
-// univer
-const univer = new Univer({
- theme: defaultTheme,
- locale: LocaleType.ZH_CN,
- locales: {
- [LocaleType.ZH_CN]: zhCN,
- [LocaleType.EN_US]: enUS,
- [LocaleType.FR_FR]: frFR,
- [LocaleType.RU_RU]: ruRU,
- [LocaleType.ZH_TW]: zhTW,
- [LocaleType.VI_VN]: viVN,
- [LocaleType.FA_IR]: faIR,
- },
- logLevel: LogLevel.VERBOSE,
-});
-
-const worker = new Worker(new URL('./worker.js', import.meta.url), { type: 'module' });
-univer.registerPlugin(UniverRPCMainThreadPlugin, { workerURL: worker });
-
-univer.registerPlugin(UniverDocsPlugin);
-univer.registerPlugin(UniverRenderEnginePlugin);
-univer.registerPlugin(UniverUIPlugin, { container: 'app' });
-univer.registerPlugin(UniverDocsUIPlugin);
-univer.registerPlugin(UniverDocsDrawingUIPlugin);
-univer.registerPlugin(UniverDocsMentionUIPlugin);
-
-univer.registerPlugin(UniverSheetsPlugin, { notExecuteFormula: true });
-univer.registerPlugin(UniverSheetsUIPlugin);
-univer.registerPlugin(UniverSheetsNumfmtPlugin);
-univer.registerPlugin(UniverSheetsZenEditorPlugin);
-univer.registerPlugin(UniverFormulaEnginePlugin, { notExecuteFormula: true });
-univer.registerPlugin(UniverSheetsNumfmtUIPlugin);
-univer.registerPlugin(UniverSheetsFormulaPlugin, { notExecuteFormula: true });
-univer.registerPlugin(UniverSheetsFormulaUIPlugin);
-univer.registerPlugin(UniverSheetsDataValidationPlugin);
-univer.registerPlugin(UniverSheetsConditionalFormattingPlugin);
-univer.registerPlugin(UniverSheetsFilterPlugin);
-univer.registerPlugin(UniverSheetsSortPlugin);
-univer.registerPlugin(UniverSheetsHyperLinkPlugin);
-univer.registerPlugin(UniverThreadCommentUIPlugin);
-univer.registerPlugin(UniverSheetsThreadCommentPlugin);
-univer.registerPlugin(UniverSheetsThreadCommentUIPlugin);
-univer.registerPlugin(UniverSheetsBindingSourcePlugin);
-
-// If we are running in e2e platform, we should immediately register the debugger plugin.
-if (IS_E2E) {
- univer.registerPlugin(UniverDebuggerPlugin);
-}
+// eslint-disable-next-line max-lines-per-function
+function createNewInstance() {
+ // univer
+ const univer = new Univer({
+ theme: defaultTheme,
+ locale: LocaleType.ZH_CN,
+ locales: {
+ [LocaleType.ZH_CN]: zhCN,
+ [LocaleType.EN_US]: enUS,
+ [LocaleType.FR_FR]: frFR,
+ [LocaleType.RU_RU]: ruRU,
+ [LocaleType.ZH_TW]: zhTW,
+ [LocaleType.VI_VN]: viVN,
+ [LocaleType.FA_IR]: faIR,
+ },
+ logLevel: LogLevel.VERBOSE,
+ });
-const injector = univer.__getInjector();
-const userManagerService = injector.get(UserManagerService);
-userManagerService.setCurrentUser(mockUser);
+ const worker = new Worker(new URL('./worker.js', import.meta.url), { type: 'module' });
+ univer.registerPlugin(UniverRPCMainThreadPlugin, { workerURL: worker });
+
+ univer.registerPlugin(UniverDocsPlugin);
+ univer.registerPlugin(UniverRenderEnginePlugin);
+ univer.registerPlugin(UniverUIPlugin, { container: 'app' });
+ univer.registerPlugin(UniverDocsUIPlugin);
+ univer.registerPlugin(UniverDocsDrawingUIPlugin);
+ univer.registerPlugin(UniverDocsMentionUIPlugin);
+
+ univer.registerPlugin(UniverSheetsPlugin, { notExecuteFormula: true });
+ univer.registerPlugin(UniverSheetsUIPlugin);
+ univer.registerPlugin(UniverSheetsNumfmtPlugin);
+ univer.registerPlugin(UniverSheetsZenEditorPlugin);
+ univer.registerPlugin(UniverFormulaEnginePlugin, { notExecuteFormula: true });
+ univer.registerPlugin(UniverSheetsNumfmtUIPlugin);
+ univer.registerPlugin(UniverSheetsFormulaPlugin, { notExecuteFormula: true });
+ univer.registerPlugin(UniverSheetsFormulaUIPlugin);
+ univer.registerPlugin(UniverSheetsDataValidationPlugin);
+ univer.registerPlugin(UniverSheetsConditionalFormattingPlugin);
+ univer.registerPlugin(UniverSheetsFilterPlugin);
+ univer.registerPlugin(UniverSheetsSortPlugin);
+ univer.registerPlugin(UniverSheetsHyperLinkPlugin);
+ univer.registerPlugin(UniverThreadCommentUIPlugin);
+ univer.registerPlugin(UniverSheetsThreadCommentPlugin);
+ univer.registerPlugin(UniverSheetsThreadCommentUIPlugin);
+ univer.registerPlugin(UniverSheetsBindingSourcePlugin);
+
+ // If we are running in e2e platform, we should immediately register the debugger plugin.
+ if (IS_E2E) {
+ univer.registerPlugin(UniverDebuggerPlugin);
+ }
-// create univer sheet instance
-if (!IS_E2E) {
- univer.createUnit(UniverInstanceType.UNIVER_SHEET, DEFAULT_WORKBOOK_DATA_DEMO);
-}
+ const injector = univer.__getInjector();
+ const userManagerService = injector.get(UserManagerService);
+ userManagerService.setCurrentUser(mockUser);
-setTimeout(() => {
- import('./lazy').then((lazy) => {
- const plugins = lazy.default();
- plugins.forEach((p) => univer.registerPlugin(p[0], p[1]));
- });
-}, LOAD_LAZY_PLUGINS_TIMEOUT);
+ // create univer sheet instance
+ if (!IS_E2E) {
+ univer.createUnit(UniverInstanceType.UNIVER_SHEET, DEFAULT_WORKBOOK_DATA_DEMO);
+ }
-setTimeout(() => {
- import('./very-lazy').then((lazy) => {
- const plugins = lazy.default();
- plugins.forEach((p) => univer.registerPlugin(p[0], p[1]));
+ setTimeout(() => {
+ import('./lazy').then((lazy) => {
+ const plugins = lazy.default();
+ plugins.forEach((p) => univer.registerPlugin(p[0], p[1]));
+ });
+ }, LOAD_LAZY_PLUGINS_TIMEOUT);
+
+ setTimeout(() => {
+ import('./very-lazy').then((lazy) => {
+ const plugins = lazy.default();
+ plugins.forEach((p) => univer.registerPlugin(p[0], p[1]));
+ });
+ }, LOAD_VERY_LAZY_PLUGINS_TIMEOUT);
+
+ univer.onDispose(() => {
+ worker.terminate();
+ window.univer = undefined;
+ window.univerAPI = undefined;
});
-}, LOAD_VERY_LAZY_PLUGINS_TIMEOUT);
-univer.onDispose(() => {
- worker.terminate();
- window.univer = undefined;
- window.univerAPI = undefined;
-});
+ window.univer = univer;
+ window.univerAPI = FUniver.newAPI(univer);
+}
-window.univer = univer;
-window.univerAPI = FUniver.newAPI(univer);
+createNewInstance();
+window.createNewInstance = createNewInstance;
declare global {
// eslint-disable-next-line ts/naming-convention
interface Window {
univer?: Univer;
univerAPI?: ReturnType<typeof FUniver.newAPI>;
+ createNewInstance?: typeof createNewInstance;
}
}