k6支持的协议
- HTTP/1.1
- HTTP/2
- WebSockets
k6 创建请求
- get请求
```
import http from 'k6/http';
export default function () {
http.get('http://test.k6.io');
}
```
- post请求
```
import http from 'k6/http';
export default function () {
var url = 'http://test.k6.io/login';
var payload = JSON.stringify({
email: 'aaa',
password: 'bbb',
});
var params = {
headers: {
'Content-Type': 'application/json',
},
};
http.post(url, payload, params);
}
```
k6 支持的http模块的
- batch(): 并行请求,这个是k6和jmter不一样的地方,Jmter的请求只能是顺序执行,这也是基于VUS和基于线程的区别(当然有人也会把不通的请求放到不通的线程组但这样实现可以,但是从逻辑上讲是不通的)
```
import http from 'k6/http';
import { check } from 'k6';
export default function() {
let responses = http.batch([
['GET', 'https://test.k6.io', null, { tags: { ctype: 'html' } }],
[
'GET',
'https://test.k6.io/style.css',
null,
{ tags: { ctype: 'css' } },
],
[
'GET',
'https://test.k6.io/images/logo.png',
null,
{ tags: { ctype: 'images' } },
],
]);
check(responses[0], {
'main page status was 200': res => res.status === 200,
});
}
```
针对页面来进行的测试