governify-tester
Advanced tools
Comparing version 0.0.1 to 0.0.2
/*! | ||
parallel-requests 2.2.0, built on: 2017-03-21 | ||
Copyright (C) 2017 Jose Enrique Ruiz Navarro | ||
governify-tester 0.0.2, built on: 2017-03-28 | ||
Copyright (C) 2017 ISA Group | ||
http://www.isa.us.es/ | ||
https://github.com/joseEnrique/parallel-requests | ||
https://github.com/isa-group/governify-tester | ||
@@ -7,0 +7,0 @@ This program is free software: you can redistribute it and/or modify |
{ | ||
"name": "governify-tester", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Testing endpoints via http requests", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/isa-group/governify-tester", |
198
README.md
@@ -1,15 +0,207 @@ | ||
![alt build:passed](https://travis-ci.org/joseEnrique/parallel-requests.svg?branch=master) | ||
![alt build:passed](https://travis-ci.org/isa-group/governify-tester.svg?branch=master) | ||
## 1. Install the package | ||
```bash | ||
npm install governify-tester | ||
npm install governify-tester --save | ||
``` | ||
## 2. Install the governify-cli and use it | ||
## 2. Install the governify-cli and use it in your shell | ||
```bash | ||
npm install governify-cli -g | ||
``` | ||
[see doc ](https://github.com/isa-group/governify-cli) | ||
## 3. Use from code | ||
Import library in you code | ||
```javascript | ||
var tester = require("governify-tester"); | ||
``` | ||
* All results generate a logfile into the directory where is launched. | ||
* Also you can obtain the results of each test. It is because all methods return a Promises | ||
### 3.1 Simple requests with duration | ||
* Params: URL, method , number of times, duration [body], [headers] | ||
```javascript | ||
tester.doParallelRequestWithDuration("https://www.google.es/", "GET", 2, 1); | ||
``` | ||
If you need the result, you can use the method **then** because it's a | ||
promise | ||
```javascript | ||
tester.doParallelRequestWithDuration("https://www.google.es/", "GET", 2, 1).then(function(success) { | ||
console.log(success); | ||
}); | ||
``` | ||
* You can pass body for test. Only you need to pass body like a string. | ||
* You could also pass headers in next params | ||
```javascript | ||
lib.doParallelRequestWithDuration("https://jsonplaceholder.typicode.com/posts", "POST", 2, 1, "{ \"isagroup\": 1}"); | ||
``` | ||
### 3.2 Simple requests once | ||
You only test endpoint n times once. | ||
* Params: URL, method , number of times, [body], [headers] | ||
```javascript | ||
tester.doOneStackOfRequest("http://google.es", "GET",2); | ||
``` | ||
It is also possible to pass body and headers in the same order that the first one. | ||
### 3.3 Simple requests infinite | ||
This one is special because not return any result a need that you stop it. | ||
* Params: URL, method , number of times, [body], [headers] | ||
```javascript | ||
tester.doRequests("http://google.es", "GET", 2); | ||
``` | ||
### 3.4 Read test from file | ||
This options is strict. | ||
It only has certain types that will be described below. | ||
You must write in yaml format and you only have to indicate the path of file. | ||
* Params: Path | ||
```javascript | ||
tester.doParallelRequestFromfile('./test.yaml'); | ||
``` | ||
### Types of tests for config file | ||
* #### long | ||
```bash | ||
testId: idoftest | ||
type: long | ||
url : https://twitter.com/ | ||
duration: 3 #seconds | ||
count: 2 #number of times | ||
method: GET | ||
``` | ||
* #### interval | ||
You have to define intervals a its duration necessarily. | ||
```bash | ||
testId: validation14 | ||
type: interval | ||
url: http://localhost:3000/test | ||
request: | ||
method: POST | ||
headers: | ||
content-type: application/json | ||
heade1: val | ||
body: | ||
id: MyID | ||
value: XXX | ||
tenants: | ||
- id: t1 | ||
intervals: | ||
- 8 | ||
- 5 | ||
duration: 1 | ||
- id: t2 | ||
intervals: | ||
- 6 | ||
- 4 | ||
duration: 1 | ||
- id: t3 | ||
intervals: | ||
- 1 | ||
- 8 | ||
duration: 1 | ||
- id: t4 | ||
intervals: | ||
- 3 | ||
- 3 | ||
duration: 1 | ||
``` | ||
* #### random | ||
You need to write an array within the body | ||
```bash | ||
testId: validation12 | ||
type: random | ||
url : https://jsonplaceholder.typicode.com/posts | ||
duration: 4 #seconds | ||
count: 10 | ||
request: | ||
method: POST | ||
headers: | ||
content-type: application/json | ||
body: #randomize in body | ||
id: | ||
- prueba | ||
- prueba1 | ||
- prueba2 | ||
- prueba3 | ||
- prueba4 | ||
value: | ||
- value | ||
- value1 | ||
- value2 | ||
- value3 | ||
- value4 | ||
date: | ||
- Date #currently date | ||
``` | ||
* #### randomTemplate | ||
```bash | ||
testId: validation11 | ||
type: randomTemplate | ||
url : https://jsonplaceholder.typicode.com/posts | ||
duration: 4 #seconds | ||
count: 10 | ||
request: | ||
method: POST | ||
randomFields: | ||
id: | ||
- prueba | ||
- prueba1 | ||
- prueba2 | ||
- prueba3 | ||
- prueba4 | ||
value: | ||
- value | ||
- value1 | ||
- value2 | ||
- value3 | ||
- value4 | ||
date: | ||
- Date #currently date | ||
headers: | ||
content-type: application/json | ||
#randomize in body | ||
body: > | ||
{ | ||
"id": "{{id}}", | ||
"value": "{{value}}", | ||
"date": "{{date}}" | ||
} | ||
``` | ||
## Latest release | ||
The version 0.0.2 is the latest stable version of governify-tester component. | ||
see [release note](https://github.com/isa-group/governify-tester/releases/tag/0.0.2) for details. | ||
For running: | ||
- Download latest version from [0.0.2](https://github.com/isa-group/governify-tester/releases/tag/0.0.2) |
/*! | ||
parallel-requests 2.2.0, built on: 2017-03-21 | ||
Copyright (C) 2017 Jose Enrique Ruiz Navarro | ||
governify-tester 0.0.2, built on: 2017-03-28 | ||
Copyright (C) 2017 ISA Group | ||
http://www.isa.us.es/ | ||
https://github.com/joseEnrique/parallel-requests | ||
https://github.com/isa-group/governify-tester | ||
@@ -7,0 +7,0 @@ This program is free software: you can redistribute it and/or modify |
162
src/index.js
/*! | ||
parallel-requests 2.2.0, built on: 2017-03-21 | ||
Copyright (C) 2017 Jose Enrique Ruiz Navarro | ||
governify-tester 0.0.2, built on: 2017-03-28 | ||
Copyright (C) 2017 ISA Group | ||
http://www.isa.us.es/ | ||
https://github.com/joseEnrique/parallel-requests | ||
https://github.com/isa-group/governify-tester | ||
@@ -28,3 +28,2 @@ This program is free software: you can redistribute it and/or modify | ||
var jsyaml = require('js-yaml'); | ||
//var cluster = require('cluster'); | ||
var moment = require('moment'); | ||
@@ -65,3 +64,3 @@ | ||
function _promisesRequests(url, method, number, body, headers) { | ||
var options; | ||
let options; | ||
@@ -79,3 +78,3 @@ if (method.toUpperCase() == "GET") { | ||
} | ||
var isJson = true; | ||
let isJson = true; | ||
if (headers && headers['content-type'] != "application/json") { | ||
@@ -106,3 +105,3 @@ isJson = false; | ||
function _doRequests(url, method, count, body) { | ||
function _doRequests(url, method, count, body, headers) { | ||
setInterval(function() { | ||
@@ -113,3 +112,3 @@ let total = 0; | ||
let start_date = moment(); | ||
_promisesRequests(url, method, count, body).then(function(success) { | ||
_promisesRequests(url, method, count, body, headers).then(function(success) { | ||
success.forEach(function(element) { | ||
@@ -144,2 +143,114 @@ if (element) { | ||
function _doRequestsRandom(url, method, count, duration, body, headers, fields) { | ||
return new Promise(function(resolve) { | ||
let total = 0; | ||
let successful = 0; | ||
let error = 0; | ||
let request_ack = 0; | ||
let counter_timer = 0; | ||
let start_date = moment(); | ||
let interval = setInterval(function() { | ||
let bodygenerated; | ||
if (fields) { | ||
if (body) { | ||
bodygenerated = generateRandomObjectTemplate(body, fields); | ||
console.log(bodygenerated); | ||
} else { | ||
throw new Error("Body's necessary for rendering"); | ||
} | ||
} else { | ||
if (body) { | ||
bodygenerated = generateRandomObject(body); | ||
console.log(bodygenerated); | ||
} | ||
} | ||
if (counter_timer == duration - 1) { | ||
clearInterval(interval); | ||
} | ||
_promisesRequests(url, method, count, bodygenerated, headers).then(function(success) { | ||
success.forEach(function(element) { | ||
if (element) { | ||
successful++; | ||
} else { | ||
error++; | ||
} | ||
total++; | ||
}); | ||
request_ack++; | ||
if (request_ack == duration) { | ||
let finish_date = moment(); | ||
let resobject = {}; | ||
resobject.totalRequest = total; | ||
resobject.success = successful; | ||
resobject.error = error; | ||
resobject.totalStack = request_ack; | ||
resobject.startTime = start_date.toISOString(); | ||
resobject.finishTime = finish_date.toISOString(); | ||
resobject.lastedFor = finish_date - start_date + " ms"; | ||
resolve(resobject); | ||
} | ||
}); | ||
counter_timer++; | ||
}, 1000); // setInterval | ||
}); | ||
} | ||
function generateRandomObject(body) { | ||
let obj_rand = {}; | ||
for (var key in body) { | ||
let objects = body[key]; | ||
if (!objects.indexOf("Date")) { | ||
obj_rand[key] = moment().toISOString(); | ||
} else { | ||
obj_rand[key] = objects[Math.floor(Math.random() * (objects.length - 1))]; | ||
} | ||
} | ||
return obj_rand; | ||
} | ||
function generateRandomObjectTemplate(body, fields) { | ||
for (var key in fields) { | ||
let objects = fields[key]; | ||
let charReplaced = '"{{' + key + '}}"'; | ||
var re = new RegExp(charReplaced, "g"); | ||
if (!objects.indexOf("Date")) { | ||
body = body.replace(re, '"' + moment().toISOString() + '"'); | ||
} else { | ||
body = body.replace(re, '"' + objects[Math.floor(Math.random() * (objects.length - 1))] + '"'); | ||
} | ||
} | ||
try { | ||
body = JSON.parse(body); | ||
} catch (e) { | ||
throw new Error("Render has not finished, please you should sure"); | ||
} | ||
return body; | ||
} | ||
function _doOneStackOfRequest(url, method, count, body) { | ||
@@ -268,3 +379,3 @@ return new Promise(function(resolve) { | ||
let singleDoc = newConfigurations[i]; | ||
_doc(singleDoc).then(function(success) { | ||
_switchdoc(singleDoc).then(function(success) { | ||
executed++; | ||
@@ -277,3 +388,2 @@ arrayResults.push(success); | ||
}); | ||
//cluster.fork(); | ||
} | ||
@@ -294,3 +404,3 @@ | ||
function _doc(testConfiguration) { | ||
function _switchdoc(testConfiguration) { | ||
return new Promise(function(resolve) { | ||
@@ -329,2 +439,32 @@ var type = testConfiguration.type; | ||
} else if (type == "random") { | ||
_doRequestsRandom(testConfiguration.url, testConfiguration.request.method, testConfiguration.count, | ||
testConfiguration.duration, testConfiguration.request.body, testConfiguration.headers, undefined).then(function(success) { | ||
success.url = testConfiguration.url; | ||
success.method = testConfiguration.request.method; | ||
success.idTest = testConfiguration.testId; | ||
console.log(success); | ||
writeLog(success); | ||
resolve(success); | ||
}); | ||
} else if (type == "randomTemplate") { | ||
_doRequestsRandom(testConfiguration.url, testConfiguration.request.method, testConfiguration.count, | ||
testConfiguration.duration, testConfiguration.request.body, testConfiguration.request.headers, testConfiguration.request.randomFields).then(function(success) { | ||
success.url = testConfiguration.url; | ||
success.method = testConfiguration.request.method; | ||
success.idTest = testConfiguration.testId; | ||
console.log(success); | ||
writeLog(success); | ||
resolve(success); | ||
}); | ||
} else { | ||
@@ -331,0 +471,0 @@ |
/*! | ||
parallel-requests 2.2.0, built on: 2017-03-21 | ||
Copyright (C) 2017 Jose Enrique Ruiz Navarro | ||
governify-tester 0.0.2, built on: 2017-03-28 | ||
Copyright (C) 2017 ISA Group | ||
http://www.isa.us.es/ | ||
https://github.com/joseEnrique/parallel-requests | ||
https://github.com/isa-group/governify-tester | ||
@@ -7,0 +7,0 @@ This program is free software: you can redistribute it and/or modify |
/*! | ||
parallel-requests 2.2.0, built on: 2017-03-21 | ||
Copyright (C) 2017 Jose Enrique Ruiz Navarro | ||
governify-tester 0.0.2, built on: 2017-03-28 | ||
Copyright (C) 2017 ISA Group | ||
http://www.isa.us.es/ | ||
https://github.com/joseEnrique/parallel-requests | ||
https://github.com/isa-group/governify-tester | ||
@@ -73,3 +73,3 @@ This program is free software: you can redistribute it and/or modify | ||
describe('By config file, 3 test successful', function() { | ||
describe('By config file, 6 test successful', function() { | ||
this.timeout(24000); | ||
@@ -79,3 +79,3 @@ it('Execute', (done) => { | ||
console.log(success); | ||
expect(success.length).to.be.equal(4); | ||
expect(success.length).to.be.equal(6); | ||
done(); | ||
@@ -82,0 +82,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
77590
777
207
19