利用Grafana K6,快速了解性能测试中的动态关联性

355 阅读4分钟

目录

阅读时间: 4 分钟

大家好。在这篇博客中,我们将讨论并学习更多关于Grafana K6中的动态关联,这是我们最喜欢的性能测试工具。你可能想知道这些情况是什么,以及为什么我们要利用它们。那么,让我们从性能测试中的基本关联原则开始。

从先前的HTTP响应中检索一个值并在进一步的HTTP请求中使用该值的技术被称为相关性。

在阅读完定义之后,我希望你对相关性有一个基本的了解。在这篇博客中,我们将发现更多关于它的内容。但首先,让我们逐一浏览一下K6的每个基本要素。

K6中的相关性

负载测试场景中的相关性是指从一个请求的响应中获取一个或多个值,并在随后的请求中重新使用它们。为了完成用户旅程中的一系列步骤,经常需要获得一个令牌或其他形式的标识。

我们为什么要在k6中使用关联性?

当利用Chrome扩展或HAR转换器来产生你的测试脚本时,经常需要关联性。这是由于这些工具将记录会话ID、CSRF令牌、ViewState、wpnonce和其他来自你特定会话的动态数据。这些令牌通常有很短的寿命。这意味着你将不得不处理从HTML/表单中提取这些数据,以便在随后的请求中使用它。这是任何使用表单的网站的一个相当普遍的问题,它可以通过一些脚本来解决。

基本上,我们有三种类型的技术,所以让我们逐一探讨这些脚本技术。

1.从JSON响应中提取值/符号

基本上,当我们有一个JSON格式的输出响应时,我们使用这种技术,就像下面这样。

{"page":2,"per_page":6,"total":12,"total_pages":2,"data":

[{"id":7,"email":"michael.lawson@reqres.in","first_name":"Michael","last_name

":"Lawson","avatar":"https://reqres.in/img/faces/7-image.jpg"},

{"id":8,"email":"lindsay.ferguson@reqres.in","first_name":"Lindsay","last_nam

e":"Ferguson","avatar":"https://reqres.in/img/faces/8-image.jpg"},

{"id":9,"email":"tobias.funke@reqres.in","first_name":"Tobias",

javascript k6脚本:-


 export default function () {


  // Make a request that returns some JSON data

  const res = http.get('https://reqres.in/api/users?page=2');

  // Extract data from that JSON data by first parsing it

  // using a call to "json()" and then accessing properties by

  // navigating the JSON data as a JS object with dot notation.

  const ListUser0 = res.json().data[0];

  check(ListUser0, {

    'first_name in list is correct' : (s) => s.first_name === 'Michael',

    'last_name in list is correct': (s) => s.last_name === 'Lawson',

    'email in list is correct': (s) => s.email === 'michael.lawson@reqres.in'
  
  });
  

  // Now we could use the "ListUser0" variable in subsequent requests...
}
 

相关输出:-

2.从表单字段中提取值/符号

基本上,当我们有一个表单字段格式的输出响应时,我们会使用这种技术,如下图所示。

当我们检查页面或特定的元素时,我们会得到下面的情况。

基本上,我们在下面的字段中精确一个值。

javascript k6 script:-


export default function () {


  // Request the page containing a form and save the response. This gives you 

access

  //to the response object, `res`.

  const res = http.get('https://www.mantissa.co.uk/main/IEForm/contact.php');

  // Query the HTML for an input field named "Firstname","Email". We want the 

value of "maxlength" and "size"

  const element1 =res.html().find('input[name=Firstname]');

  const element2 = res.html().find('input[name=Email]');

  // Get the value of the attribute "value" and save it to a variable.

  const val1 = element1.attr('size');

  const val2 = element2.attr('maxlength');


  // Now you can concatenate this extracted value in subsequent requests that 

require it.

  // console.log() works when executing k6 scripts locally and is handy for 

debugging purposes

  console.log('The value of field size is: ' + val1);

  console.log('The value of name field is:' + val2);

  sleep(1);

}

相关输出:-

值/符号的通用提取

当响应有时可能既不是JSON也不是HTML时,我们会使用这种技术,在这种情况下,上述提取方法将不适用。在这些情况下,你可能应该直接在Response上工作。我们可以通过使用FindBetween内置函数来处理这种类型的情况。

jslib utils库中有一个这种函数的例子,叫做findBetween。该函数利用了JavaScript内置的String.indexOf,因此不依赖于使用潜在的昂贵的正则表达式操作。

让我们以XML响应为例,以便更好地理解

<travelers>

<Travelerinformation>

<id>1403</id>

<name>Mayank Narayana</name>

<email>mayank-leo@hotmail.com</email>

<adderes>USA</adderes>

<createdat>2020-12-16T09:57:10.9389781</createdat>

</Travelerinformation>
</travelers>

javascript k6脚本:-

import { findBetween } from 'https://jslib.k6.io/k6-utils/1.2.0/index.js';

export default function () {

  // This request returns XML:

  const res = http.get('http://restapi.adequateshop.com/api/Traveler?

page=6');

  // Use findBetween to extract the first title encountered:

  const name = findBetween(res.body, '<name>','</name>');

  const email= findBetween(res.body, '<email>','</email>');
 

  check(name, {

    'name is correct': (ast) => ast === 'Mayank Narayana',

  });
  check(email, {

    'email is correct': (ast) => ast === 'mayank-leo@hotmail.com',

  });
  console.log('The value of name field is:' + name);

  console.log('The value of name field is:' + email);

}

相关输出:-

本博客到此结束。我希望你喜欢并了解了K6中的关联性。如果你需要上述代码,请随时与我们联系:knoldus techub.com

谢谢你!!

参考资料。

k6.io/

k6.io/docs/exampl…

分享Knol。

相关信息