Skip to content

Commit

Permalink
Merge pull request #17 from HarrisChu/add_rate_example
Browse files Browse the repository at this point in the history
rate example
  • Loading branch information
HarrisChu committed Mar 2, 2022
2 parents 3c045e3 + 1645ccd commit 0007974
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions example/nebula-test-insert-limit-rate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// refer https://k6.io/blog/how-to-generate-a-constant-request-rate-with-the-new-scenarios-api/
// the example options means:
// 1. each second, 1000 iterations would be made.
// 2. max concurrent vu is 300.
// 3. last 30 seconds, so it would run 1000*30 = 30000 iterations.
// 4. batchSize is 1, so it would insert one recond per iteration.
import nebulaPool from 'k6/x/nebulagraph';
import { check } from 'k6';
import { Trend } from 'k6/metrics';
import { sleep } from 'k6';

var lantencyTrend = new Trend('latency');
var responseTrend = new Trend('responseTime');
// initial nebula connect pool
var pool = nebulaPool.initWithSize("192.168.8.61:9669,192.168.8.62:9669,192.168.8.63:9669", 400, 4000);

// set csv strategy, 1 means each vu has a separate csv reader.
pool.configCsvStrategy(1)

// initial session for every vu
var session = pool.getSession("root", "nebula")
session.execute("USE ldbc")

// concurrent 300, and each second, 1000 iterations would be made.
export const options = {
scenarios: {
constant_request_rate: {
executor: 'constant-arrival-rate',
rate: 1000,
timeUnit: '1s',
duration: '30s',
preAllocatedVUs: 300,
maxVUs: 300,
},
},
};

export function setup() {
// config csv file
pool.configCSV("person.csv", "|", false)
// config output file, save every query information
pool.configOutput("output.csv")
sleep(1)
}

export default function (data) {
// get csv data from csv file
let ngql = 'INSERT VERTEX Person(firstName, lastName, gender, birthday, creationDate, locationIP, browserUsed) VALUES '
let batches = []
let batchSize = 1
// batch size
for (let i = 0; i < batchSize; i++) {
let d = session.getData();
let values = []
// concat the insert value
for (let index = 1; index < 8; index++) {
let value = '"' + d[index] + '"'
values.push(value)
}
let batch = d[0] + ":(" + values.join(",") + ")"
batches.push(batch)
}
ngql = ngql + batches.join(',')
let response = session.execute(ngql)
check(response, {
"IsSucceed": (r) => r.isSucceed() === true
});
// add trend
lantencyTrend.add(response.getLatency());
responseTrend.add(response.getResponseTime());

};

export function teardown() {
pool.close()
}


0 comments on commit 0007974

Please sign in to comment.