onnxruntime/js/web/test/test-main.ts
Yulong Wang 4ebc9c3b5e
[JS] onnxruntime-web (#7394)
* add web

* add script and test

* fix lint

* add test/data/ops

* add test/data/node/ to gitignore

* modify scripts

* add onnxjs

* fix tests

* fix test-runner

* fix sourcemap

* fix onnxjs profiling

* update test list

* update README

* resolve comments

* set wasm as default backend

* rename package

* update copyright header

* do not use class "Buffer" in browser context

* revise readme
2021-04-27 00:04:25 -07:00

113 lines
3.1 KiB
TypeScript

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import '../lib/index'; // this need to be the first line
import * as ort from 'onnxruntime-common';
import * as platform from 'platform';
import {Logger} from '../lib/onnxjs/instrument';
import {Test} from './test-types';
const ONNX_JS_TEST_CONFIG = (ort.env as any).ORT_WEB_TEST_DATA as Test.Config;
// Set logging configuration
for (const logConfig of ONNX_JS_TEST_CONFIG.log) {
Logger.set(logConfig.category, logConfig.config);
}
import {ModelTestContext, OpTestContext, runModelTestSet, runOpTest} from './test-runner';
import {readJsonFile} from './test-shared';
// Unit test
if (ONNX_JS_TEST_CONFIG.unittest) {
// tslint:disable-next-line:no-require-imports
require('./unittests');
}
// Set file cache
if (ONNX_JS_TEST_CONFIG.fileCacheUrls) {
before('prepare file cache', async () => {
const allJsonCache = await Promise.all(ONNX_JS_TEST_CONFIG.fileCacheUrls!.map(readJsonFile)) as Test.FileCache[];
for (const cache of allJsonCache) {
ModelTestContext.setCache(cache);
}
});
}
function shouldSkipTest(test: Test.ModelTest|Test.OperatorTest) {
if (!test.cases || test.cases.length === 0) {
return true;
}
if (!test.condition) {
return false;
}
if (!platform.description) {
throw new Error('failed to check current platform');
}
const regex = new RegExp(test.condition);
return !regex.test(platform.description);
}
// ModelTests
for (const group of ONNX_JS_TEST_CONFIG.model) {
describe(`#ModelTest# - ${group.name}`, () => {
for (const test of group.tests) {
const describeTest = shouldSkipTest(test) ? describe.skip : describe;
describeTest(`[${test.backend}] ${test.name}`, () => {
let context: ModelTestContext;
before('prepare session', async () => {
context = await ModelTestContext.create(test, ONNX_JS_TEST_CONFIG.profile);
});
after('release session', () => {
if (context) {
context.release();
}
});
for (const testCase of test.cases) {
it(testCase.name, async () => {
await runModelTestSet(context, testCase);
});
}
});
}
});
}
// OpTests
for (const group of ONNX_JS_TEST_CONFIG.op) {
describe(`#OpTest# - ${group.name}`, () => {
for (const test of group.tests) {
const describeTest = shouldSkipTest(test) ? describe.skip : describe;
describeTest(`[${test.backend!}]${test.operator} - ${test.name}`, () => {
let context: OpTestContext;
before('Initialize Context', async () => {
context = new OpTestContext(test);
await context.init();
if (ONNX_JS_TEST_CONFIG.profile) {
OpTestContext.profiler.start();
}
});
after('Dispose Context', () => {
if (ONNX_JS_TEST_CONFIG.profile) {
OpTestContext.profiler.stop();
}
context.dispose();
});
for (const testCase of test.cases) {
it(testCase.name, async () => {
await runOpTest(testCase, context);
});
}
});
}
});
}