Skip to content

Instantly share code, notes, and snippets.

@syusui-s
Last active November 23, 2023 09:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save syusui-s/c9e69f6ffcf3d5e6bcd616f1f46070d0 to your computer and use it in GitHub Desktop.
Save syusui-s/c9e69f6ffcf3d5e6bcd616f1f46070d0 to your computer and use it in GitHub Desktop.
xk6-nostr バッチ化・集約化されたクエリの負荷テスト用
/*
Copyright 2023 Moyatani Shusui
original code is made by akiomik
https://github.com/akiomik/xk6-nostr
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import nostr from 'k6/x/nostr';
import event from 'k6/x/nostr/event';
import { check, fail } from 'k6';
const relay = nostr.relayConnect("ws://127.0.0.1:8234");
function sample(as) {
return as[Math.floor(Math.random() * as.length)];
}
function range(n) {
return [...Array(n).keys()];
}
function sampleUniq(as, count) {
const indicies = range(as.length);
const result = [];
range(Math.min(count, as.length)).forEach(() => {
const index = Math.floor(Math.random() * indicies.length);
indicies.splice(index, 1);
result.push(as[index]);
});
return result;
}
function concat() {
let result = [];
for (const k in arguments) {
result = result.concat(arguments[k]);
}
return result;
}
export function setup() {
// 50人のユーザがいる
const keys = range(50).map(() => {
const sk = nostr.generatePrivateKey();
const pk = nostr.getPublicKey(sk);
return [sk, pk];
});
const notes = range(100).map(() => {
const sk = sample(keys)[0];
const now = Math.round(new Date().getTime() / 1000);
return event.sign({ content: Math.random(), kind: 1, created_at: now }, sk);
});
const reactions = range(10000).map(() => {
const sk = sample(keys)[0];
const note = sample(notes).id;
const now = Math.round(new Date().getTime() / 1000);
return event.sign({
content: Math.random(),
kind: 7,
tags: [['e', note, '']],
created_at: now,
}, sk);
});
const reactionsCount = {};
reactions.forEach((reaction) => {
const id = reaction.tags[0][1];
const count = reactionsCount[id];
reactionsCount[id] = count != null ? count + 1 : 0;
})
concat(notes, reactions).forEach((ev) => {
relay.publish(ev);
});
return { keys, notes, reactionsCount };
}
function getCountForIds(ids, reactionsCount) {
return ids.reduce((sum, id) => {
const countForId = reactionsCount[id];
return sum + (countForId != null ? countForId : 0);
}, 0);
}
export default function ({ notes, reactionsCount }) {
const ids = sampleUniq(notes, 20).map(note => note.id);
relay.querySync({ kinds: [7], '#e': ids, limit: 100 });
/*
if (!check(result, {
'length match': (r) => r.length == getCountForIds(ids, reactionsCount)
})) fail('length didnt match: ' + result.length + ', expected: ' + getCountForIds(ids, reactionsCount));
*/
}
export function teardown() {
relay.close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment