Commit fcb8d750 authored by Your Name's avatar Your Name

add script

parent f2c00c79
Pipeline #715 canceled with stages
import http from 'k6/http';
import { check } from "k6";
export const options = {
// A number specifying the number of VUs to run concurrently.
vus: 10,
// A string specifying the total duration of the test run.
duration: '30s',
// The following section contains configuration options for execution of this
// test script in Grafana Cloud.
//
// See https://grafana.com/docs/grafana-cloud/k6/get-started/run-cloud-tests-from-the-cli/
// to learn about authoring and running k6 test scripts in Grafana k6 Cloud.
//
// ext: {
// loadimpact: {
// // The ID of the project to which the test is assigned in the k6 Cloud UI.
// // By default tests are executed in default project.
// projectID: "",
// // The name of the test in the k6 Cloud UI.
// // Test runs with the same name will be grouped.
// name: "script.js"
// }
// },
// Uncomment this section to enable the use of Browser API in your tests.
//
// See https://grafana.com/docs/k6/latest/using-k6-browser/running-browser-tests/ to learn more
// about using Browser API in your test scripts.
//
scenarios: {
// The scenario name appears in the result summary, tags, and so on.
// You can give the scenario any name, as long as each name in the script is unique.
ui: {
// Executor is a mandatory parameter for browser-based tests.
// Shared iterations in this case tells k6 to reuse VUs to execute iterations.
//
// See https://grafana.com/docs/k6/latest/using-k6/scenarios/executors/ for other executor types.
executor: 'shared-iterations',
options: {
browser: {
// This is a mandatory parameter that instructs k6 to launch and
// connect to a chromium-based browser, and use it to run UI-based
// tests.
type: 'chromium',
},
},
},
}
};
// The function that defines VU logic.
//
// See https://grafana.com/docs/k6/latest/examples/get-started-with-k6/ to learn more
// about authoring k6 scripts.
//
// curl http://192.168.1.10:8000/api/v1/demianhjw/aigic/0129 -H "Content-Type: application/json" -H "apikey: fhqU5VwqU2tdMxU6pX0tw0kj2E9wqdsF" -H "sync:true" -d '{
// "model_name": "Realistic_Vision_V1.4",
// "model_type": "tex2img",
// "desc": {
// "prompt": " a small car",
// "steps": 20
// }
// }'
export default function() {
// Send a JSON encoded POST request
let body = JSON.stringify({
"model_name": "Realistic_Vision_V1.4",
"model_type": "tex2img",
"desc": {
"prompt": " a small car",
"steps": 20
}
});
let res = http.post('http://192.168.1.10:8000/api/v1/demianhjw/aigic/0129', body, { headers:
{ "Content-Type": "application/json",
"apikey":"fhqU5VwqU2tdMxU6pX0tw0kj2E9wqdsF",
"sync":"true"}
});
// Use JSON.parse to deserialize the JSON (instead of using the r.json() method)
//let j = JSON.parse(res.body);
//console.log(res.body)
// Verify response
check(res, {
"status is 200": (r) => r.status === 200,
// "is key correct": (r) => j.json.key === "value",
});
}
import { check } from "k6";
import http from "k6/http";
import redis from "k6/x/redis";
import exec from "k6/execution";
import { textSummary } from "https://jslib.k6.io/k6-summary/0.0.1/index.js";
export const options = {
scenarios: {
redisPerformance: {
executor: "shared-iterations",
vus: 10,
iterations: 200,
exec: "measureRedisPerformance",
},
usingRedisData: {
executor: "shared-iterations",
vus: 10,
iterations: 200,
exec: "measureUsingRedisData",
},
},
};
// Instantiate a new Redis client using a URL
const redisClient = new redis.Client(
// URL in the form of redis[s]://[[username][:password]@][host][:port][/db-number
__ENV.REDIS_URL || "redis://192.168.1.10:6379",
);
// Prepare an array of crocodile ids for later use
// in the context of the measureUsingRedisData function.
const crocodileIDs = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
export function measureRedisPerformance() {
// VUs are executed in a parallel fashion,
// thus, to ensure that parallel VUs are not
// modifying the same key at the same time,
// we use keys indexed by the VU id.
const key = `foo-${exec.vu.idInTest}`;
redisClient
.set(`foo-${exec.vu.idInTest}`, 1)
.then(() => redisClient.get(`foo-${exec.vu.idInTest}`))
.then((value) => redisClient.incrBy(`foo-${exec.vu.idInTest}`, value))
.then((_) => redisClient.del(`foo-${exec.vu.idInTest}`))
.then((_) => redisClient.exists(`foo-${exec.vu.idInTest}`))
.then((exists) => {
if (exists !== 0) {
throw new Error("foo should have been deleted");
}
});
}
export function setup() {
redisClient.sadd("crocodile_ids", ...crocodileIDs);
}
export function measureUsingRedisData() {
// Pick a random crocodile id from the dedicated redis set,
// we have filled in setup().
redisClient
.srandmember("crocodile_ids")
.then((randomID) => {
const url = `https://test-api.k6.io/public/crocodiles/${randomID}`;
const res = http.get(url);
check(res, {
"status is 200": (r) => r.status === 200,
"content-type is application/json": (r) =>
r.headers["content-type"] === "application/json",
});
return url;
})
.then((url) => redisClient.hincrby("k6_crocodile_fetched", url, 1));
}
export function teardown() {
redisClient.del("crocodile_ids");
}
export function handleSummary(data) {
redisClient
.hgetall("k6_crocodile_fetched")
.then((fetched) => Object.assign(data, { k6_crocodile_fetched: fetched }))
.then((data) =>
redisClient.set(`k6_report_${Date.now()}`, JSON.stringify(data))
)
.then(() => redisClient.del("k6_crocodile_fetched"));
return {
stdout: textSummary(data, { indent: " ", enableColors: true }),
};
}
// Instantiate a new Redis client using a URL.
// The connection will be established on the first command call.
//const client = new redis.Client('redis://192.168.1.10:6379');
//export default function() {
// Once the SRANDMEMBER operation is succesfull,
// it resolves the promise and returns the random
// set member value to the caller of the resolve callback.
//
// The next promise performs the synchronous HTTP call, and
// returns a promise to the next operation, which uses the
// passed URL value to store some data in redis.
//client.srandmember('client_ids')
// .then((randomID) => {
// const url = `https://my.url/${randomID}`
// const res = http.get(url)
// Do something with the result...
// return a promise resolving to the URL
// return url
// })
// .then((url) => client.hincrby('k6_crocodile_fetched', url, 1));
//}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment