- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我使用 Angular TestBed 进行了一些单元测试。即使测试非常简单,它们运行起来也非常慢(平均每秒 1 个测试 Assets )。
即使在重新阅读 Angular 文档后,我也找不到性能如此差的原因。
不使用 TestBed 的独立测试在几分之一秒内运行。
单元测试
import { Component } from "@angular/core";
import { ComponentFixture, TestBed, async } from "@angular/core/testing";
import { By } from "@angular/platform-browser";
import { DebugElement } from "@angular/core";
import { DynamicFormDropdownComponent } from "./dynamicFormDropdown.component";
import { NgbModule } from "@ng-bootstrap/ng-bootstrap";
import { FormsModule } from "@angular/forms";
import { DropdownQuestion } from "../../element/question/questionDropdown";
import { TranslateService } from "@ngx-translate/core";
import { TranslatePipeMock } from "../../../../tests-container/translate-pipe-mock";
describe("Component: dynamic drop down", () => {
let component: DynamicFormDropdownComponent;
let fixture: ComponentFixture<DynamicFormDropdownComponent>;
let expectedInputQuestion: DropdownQuestion;
const emptySelectedObj = { key: "", value: ""};
const expectedOptions = {
key: "testDropDown",
value: "",
label: "testLabel",
disabled: false,
selectedObj: { key: "", value: ""},
options: [
{ key: "key_1", value: "value_1" },
{ key: "key_2", value: "value_2" },
{ key: "key_3", value: "value_3" },
],
};
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [NgbModule.forRoot(), FormsModule],
declarations: [DynamicFormDropdownComponent, TranslatePipeMock],
providers: [TranslateService],
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(DynamicFormDropdownComponent);
component = fixture.componentInstance;
expectedInputQuestion = new DropdownQuestion(expectedOptions);
component.question = expectedInputQuestion;
});
it("should have a defined component", () => {
expect(component).toBeDefined();
});
it("Must have options collapsed by default", () => {
expect(component.optionsOpen).toBeFalsy();
});
it("Must toggle the optionsOpen variable calling openChange() method", () => {
component.optionsOpen = false;
expect(component.optionsOpen).toBeFalsy();
component.openChange();
expect(component.optionsOpen).toBeTruthy();
});
it("Must have options available once initialized", () => {
expect(component.question.options.length).toEqual(expectedInputQuestion.options.length);
});
it("On option button click, the relative value must be set", () => {
spyOn(component, "propagateChange");
const expectedItem = expectedInputQuestion.options[0];
fixture.detectChanges();
const actionButtons = fixture.debugElement.queryAll(By.css(".dropdown-item"));
actionButtons[0].nativeElement.click();
expect(component.question.selectedObj).toEqual(expectedItem);
expect(component.propagateChange).toHaveBeenCalledWith(expectedItem.key);
});
it("writeValue should set the selectedObj once called (pass string)", () => {
expect(component.question.selectedObj).toEqual(emptySelectedObj);
const expectedItem = component.question.options[0];
component.writeValue(expectedItem.key);
expect(component.question.selectedObj).toEqual(expectedItem);
});
it("writeValue should set the selectedObj once called (pass object)", () => {
expect(component.question.selectedObj).toEqual(emptySelectedObj);
const expectedItem = component.question.options[0];
component.writeValue(expectedItem);
expect(component.question.selectedObj).toEqual(expectedItem);
});
});
目标组件(带模板)
import { Component, Input, OnInit, ViewChild, ElementRef, forwardRef } from "@angular/core";
import { FormGroup, ControlValueAccessor, NG_VALUE_ACCESSOR } from "@angular/forms";
import { DropdownQuestion } from "../../element/question/questionDropdown";
@Component({
selector: "df-dropdown",
templateUrl: "./dynamicFormDropdown.component.html",
styleUrls: ["./dynamicFormDropdown.styles.scss"],
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => DynamicFormDropdownComponent),
multi: true,
},
],
})
export class DynamicFormDropdownComponent implements ControlValueAccessor {
@Input()
public question: DropdownQuestion;
public optionsOpen: boolean = false;
public selectItem(key: string, value: string): void {
this.question.selectedObj = { key, value };
this.propagateChange(this.question.selectedObj.key);
}
public writeValue(object: any): void {
if (object) {
if (typeof object === "string") {
this.question.selectedObj = this.question.options.find((item) => item.key === object) || { key: "", value: "" };
} else {
this.question.selectedObj = object;
}
}
}
public registerOnChange(fn: any) {
this.propagateChange = fn;
}
public propagateChange = (_: any) => { };
public registerOnTouched() {
}
public openChange() {
if (!this.question.disabled) {
this.optionsOpen = !this.optionsOpen;
}
}
private toggle(dd: any) {
if (!this.question.disabled) {
dd.toggle();
}
}
}
-----------------------------------------------------------------------
<div>
<div (openChange)="openChange();" #dropDown="ngbDropdown" ngbDropdown class="wrapper" [ngClass]="{'disabled-item': question.disabled}">
<input type="text"
[disabled]="question.disabled"
[name]="controlName"
class="select btn btn-outline-primary"
[ngModel]="question.selectedObj.value | translate"
[title]="question.selectedObj.value"
readonly ngbDropdownToggle #selectDiv/>
<i (click)="toggle(dropDown);" [ngClass]="optionsOpen ? 'arrow-down' : 'arrow-up'" class="rchicons rch-003-button-icon-referenzen-pfeil-akkordon"></i>
<div ngbDropdownMenu aria-labelledby="dropdownBasic1" class="option-wrapper">
<button *ngFor="let opt of question.options; trackBy: opt?.key" (click)="selectItem(opt.key, opt.value); dropDown.close();"
class="dropdown-item option" [disabled]="question.disabled">{{opt.value | translate}}</button>
</div>
</div>
</div>
业力配置
var webpackConfig = require('./webpack/webpack.dev.js');
module.exports = function (config) {
config.set({
basePath: '',
frameworks: ['jasmine'],
plugins: [
require('karma-webpack'),
require('karma-jasmine'),
require('karma-phantomjs-launcher'),
require('karma-sourcemap-loader'),
require('karma-tfs-reporter'),
require('karma-junit-reporter'),
],
files: [
'./app/polyfills.ts',
'./tests-container/test-bundle.spec.ts',
],
exclude: [],
preprocessors: {
'./app/polyfills.ts': ['webpack', 'sourcemap'],
'./tests-container/test-bundle.spec.ts': ['webpack', 'sourcemap'],
'./app/**/!(*.spec.*).(ts|js)': ['sourcemap'],
},
webpack: {
entry: './tests-container/test-bundle.spec.ts',
devtool: 'inline-source-map',
module: webpackConfig.module,
resolve: webpackConfig.resolve
},
mime: {
'text/x-typescript': ['ts', 'tsx']
},
reporters: ['progress', 'junit', 'tfs'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['PhantomJS'],
singleRun: false,
concurrency: Infinity
})
}
最佳答案
事实证明问题出在 Angular 上,如 Github 所述
下面是来自 Github 讨论的解决方法,该方法将我们项目中运行测试的时间从 40 多秒减少到仅 1 秒 (!)。
const oldResetTestingModule = TestBed.resetTestingModule;
beforeAll((done) => (async () => {
TestBed.resetTestingModule();
TestBed.configureTestingModule({
// ...
});
function HttpLoaderFactory(http: Http) {
return new TranslateHttpLoader(http, "/api/translations/", "");
}
await TestBed.compileComponents();
// prevent Angular from resetting testing module
TestBed.resetTestingModule = () => TestBed;
})()
.then(done)
.catch(done.fail));
关于Angular 4 单元测试(TestBed)非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47691705/
自己试试看: import pandas as pd s=pd.Series(xrange(5000000)) %timeit s.loc[[0]] # You need pandas 0.15.1
我最近开始使用 Delphi 中的 DataSnap 来生成 RESTful Web 服务。在遵循 Marco Cantu 本人和互联网上其他几个人的指导后,我成功地使整个“链条”正常工作。 但是有一
我一直在为操作系统类(class)编写以下代码,但结果有些奇怪。该代码创建x线程并同时运行它们,以便将两个平方矩阵相乘。每个线程将输入矩阵的Number_of_rows/Number_of_threa
我正在尝试确定何时使用 parallel包以加快运行某些分析所需的时间。我需要做的一件事是创建矩阵,比较具有不同行数的两个数据框中的变量。我在 StackOverflow 上问了一个关于有效方法的问题
我最近对我的代码进行了一些清理,并在此过程中更改了此内容(不完全是真实的代码): read = act readSTRef test1 term i var = do t v^!terms.
我正在计时查询和同一个查询的执行时间,分页。 foreach (var x in productSource.OrderBy(p => p.AdminDisplayName) .Wher
我正在开发一个项目 (WPF),我有一个 Datagrid 从数据库加载超过 5000 条记录,所以我使用 BackgroundWorker 来通知用户数据正在加载,但它太慢了,我需要等待将近 2分钟
我在查询中添加 ORDER BY 时遇到问题。没有 ORDER BY 查询大约需要 26ms,一旦我添加 ORDER BY,它大约需要 20s。 我尝试了几种不同的方法,但似乎可以减少时间。 尝试 F
我是 Android 开发新手,遇到了性能问题。当我的 GridView 有太多项目时,它会变得有点慢。有什么方法可以让它运行得更快一些吗? 这是我使用的代码: 适配器: public class C
这里的要点是: 1.设置query_cache_type = 0;重置查询缓存; 2.在 heidisql(或任何其他客户端 UI)中运行任何查询 --> 执行,例如 45 毫秒 3.使用以下代码运行
想象下表: CREATE TABLE drops( id BIGSERIAL PRIMARY KEY, loc VARCHAR(5) NOT NULL, tag INT NOT
我的表 test_table 中的示例数据: date symbol value created_time 2010-01-09 symbol1
首先,如果已经有人问过这个问题,我深表歉意,至少我找不到任何东西。 无论如何,我将每 5 分钟运行一次 cron 任务。该脚本加载 79 个外部页面,而每个页面包含大约 200 个我需要在数据库中检查
我有下面的 SQL 代码,它来自 MySQL 数据库。现在它给了我期望的结果,但是查询很慢,我想我应该在进一步之前加快这个查询的速度。 表agentstatusinformation有: PKEY(主
我需要获取一个对象在 Core Data 中数千个其他对象之间的排名。现在,这是我的代码: - (void)rankMethod { //Fetch all objects NSFet
我正在编写一个应用程序,我需要在其中读取用户的地址簿并显示他所有联系人的列表。我正在测试的 iPhone 有大约 100 个联系人,加载联系人确实需要很多时间。 ABAddressBookRef ad
我正在使用 javascript 将 160 行添加到包含 10 列的表格中。如果我这样做: var cellText = document.createTextNode(value); cell.a
我是 Swift 的新手,我已经设置了一个 tableView,它从 JSON 提要中提取数据并将其加载到表中。 表格加载正常,但是当表格中有超过 10 个单元格时,它会变得缓慢且有些滞后,特别是它到
我在 InitializeCulture 和 Page_PreInit 事件之间的 asp.net 页面中遇到性能问题。当我重写 DeterminePostBackMode() 时,我发现问题出在 b
我在 Hetzner 上有一个带有 256GB RAM 6 个 CPU(12 个线程) 的专用服务器,它位于德国。我有 CENTOS 7.5。 EA4。 我的问题是 SSL。每天大约 2 小时,我们在
我是一名优秀的程序员,十分优秀!