- Dart:Scalable Application Development
- Davy Mitchell Sergey Akopkokhyants Ivo Balbaert
- 314字
- 2021-07-09 18:56:22
Load testing
It is good to know the limits of an application long before you ever reach them. The HttpClientRequest
function can be used to create requests to the blog server, and the HttpClientResponse
function can be used to receive incoming data from the blog server.
The statusCode
property will be checked to ensure that the page request was successfully handled by the server.
Building a simple load tool
A simple benchmark for the blog server would be the time taken to serve 1000 files. The getUrl
method triggers the actual request with the first then
clause, closing the request to the server. The following then
clause handles the actual response from the server.
This method can be used to monitor a live website and perhaps trigger a notification if a status other than HttpStatus.OK
is received:
import 'dart:io'; main() { print("Starting..."); var url = "http://127.0.0.1:8080/index.html"; var hc = new HttpClient(); var watch = new Stopwatch(); int attemptedRequests = 1000; print("Starting testing..."); watch.start(); for (int i=0;i<attemptedRequests;i++) { hc.getUrl(Uri.parse(url)) .then((HttpClientRequest request) => request.close()) .then((HttpClientResponse response) { if (response.statusCode==HttpStatus.OK) print("$i, ${response.statusCode}, ${watch.elapsed.inMilliseconds}"); }); } }
The Stopwatch
class can be used to measure the time taken and reported to standard output via a print statement. As the responses arrive asynchronously, the status is printed after each response. The request number, status code, and elapsed time are printed with a comma between each value, so that the data can easily be manipulated in a spreadsheet application.
Try putting a print
statement directly after the loop has finished and you will see the print run before the first response is received from the server (or soon after).
On my modest Linux laptop, the server was able to serve index.html
2000 times in 3.8 seconds—not too bad! Try experimenting with the request number; however, you are likely to hit a limit on open files, as this simple benchmark fires many simultaneous requests.
- Boost.Asio C++ Network Programming(Second Edition)
- PostgreSQL技術(shù)內(nèi)幕:事務(wù)處理深度探索
- Practical Game Design
- Mastering Swift 2
- Monitoring Elasticsearch
- The Data Visualization Workshop
- C程序設(shè)計(jì)案例教程
- 程序員修煉之道:通向務(wù)實(shí)的最高境界(第2版)
- Tableau 10 Bootcamp
- LabVIEW虛擬儀器入門與測(cè)控應(yīng)用100例
- R用戶Python學(xué)習(xí)指南:數(shù)據(jù)科學(xué)方法
- HoloLens與混合現(xiàn)實(shí)開(kāi)發(fā)
- 從零開(kāi)始學(xué)Unity游戲開(kāi)發(fā):場(chǎng)景+角色+腳本+交互+體驗(yàn)+效果+發(fā)布
- Building Scalable Apps with Redis and Node.js
- PHP從入門到精通(第7版)