Angular Http 请求极其慢

发布于 2025-01-20 20:53:07 字数 2531 浏览 1 评论 0原文

我的 Angular 2 应用程序连接到 Laravel api 服务。当我通过 Postman 拨打电话时,大约需要 500 毫秒。但是,当通过 Angular 应用程序执行调用时,加载时间大约需要 8 秒以上。

看起来该请求在很长一段时间内处于待处理状态。当您单击按钮导航到慢速页面时,请求将显示在网络选项卡中,并且应用程序会冻结,网络选项卡中的请求将显示待处理状态。

过了一会儿,当页面完全加载时,Edge 的网络选项卡中的请求显示 800 毫秒。看起来请求本身没问题,但由于某种原因需要很长时间才能执行。我在拦截器中添加了一个性能计数器。拦截器性能通知时间为 16299.5 毫秒。

经过研究,我已经实现了 Connection: close to headers 但在我的网站上出现错误: Refused to set unsafe header "Connection"

我还添加了 ngOnDestroy 但这似乎也不会影响速度。

编辑: chrom / firefox 和 Edge 之间存在显着差异。看起来 Edge 比其他 2 慢很多倍。

你知道为什么这么慢吗?

代码

:

getRequest(url: string, requestParams: any = {}): Observable<any> {
    return this.http.get(environment.apiUrl + url, {params: requestParams});
}

"

intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
  const begin = performance.now();
    const authData = this.authenticationService.getAuthenticationData();

    if (authData){
      const head = {'Authorization': 'Bearer ' + authData.plainTextToken};
      const modifiedRequest = request.clone({
        headers: new HttpHeaders(head).set('X-UserId', authData.curUser['UserId'].toString())
      });
      return next.handle(modifiedRequest).pipe(
        finalize(() => {
          this.logRequestTime(begin, request.url, request.method);
        })
      );
    }else{
      return next.handle(request).pipe(
        finalize(() => {
          this.logRequestTime(begin, request.url, request.method);
        })
      );
    }
}

private logRequestTime(startTime: number, url: string, method: string): void {
    const requestDuration = `${performance.now() - startTime}`;
}

api.service.tsauthentication.interceptor.tsoverview.component.tsPostman

ngOnInit(): void {
    this.apiSource = this.store.fetchApi().subscribe(responseData => {
        this.store.addArrayData(responseData);
    });
}

ngOnDestroy(): void {
    this.apiSource.unsubscribe();
}

<

a href="https://i.sstatic.net/YZQrP.jpg" rel="nofollow noreferrer >邮递员响应时间

网络待处理: 网络选项卡

编辑 13:11 我发现在请求期间 CPU 达到 100%。 CPU 性能

My angular 2 application connects to a laravel api service. When i make the call via Postman, it takes about 500ms. But when the call is executed via the Angular application it takes about 8+ seconds to load.

It looks like the request is in a pending state for a very long time. When you click the button to navigate to the slow page, the requests are shown in the network tab and the app freezes, the requests in the network tab show a pending state.

After a while when the page is fully loaded the request in the network tab of Edge says 800ms. It looks like the request itself is okay but for some reason it takes a long time before it executes. I've added a performance counter in the interceptor. The interceptor performance informs 16299.5 milliseconds.

After research I've implemented the Connection: close to the headers but on my site it errors: Refused to set unsafe header "Connection"

I've also added the ngOnDestroy but this also does not seem to affect the speed.

Edit: There is a sygnificant difference between chrom / firefox and Edge. Seems like Edge is many times more slow than the other 2.

Any ideas why it is this slow?

Code

api.service.ts

getRequest(url: string, requestParams: any = {}): Observable<any> {
    return this.http.get(environment.apiUrl + url, {params: requestParams});
}

authentication.interceptor.ts

intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
  const begin = performance.now();
    const authData = this.authenticationService.getAuthenticationData();

    if (authData){
      const head = {'Authorization': 'Bearer ' + authData.plainTextToken};
      const modifiedRequest = request.clone({
        headers: new HttpHeaders(head).set('X-UserId', authData.curUser['UserId'].toString())
      });
      return next.handle(modifiedRequest).pipe(
        finalize(() => {
          this.logRequestTime(begin, request.url, request.method);
        })
      );
    }else{
      return next.handle(request).pipe(
        finalize(() => {
          this.logRequestTime(begin, request.url, request.method);
        })
      );
    }
}

private logRequestTime(startTime: number, url: string, method: string): void {
    const requestDuration = `${performance.now() - startTime}`;
}

overview.component.ts

ngOnInit(): void {
    this.apiSource = this.store.fetchApi().subscribe(responseData => {
        this.store.addArrayData(responseData);
    });
}

ngOnDestroy(): void {
    this.apiSource.unsubscribe();
}

Postman:

postman repsponse time

Network pending:
network tab

Edit 13:11
ive found that during the request the cpu goes to 100%.
cpu performance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文