|
| 1 | +import { MUTATION_TIMEOUT, QUERY_TIMEOUT } from "../types.js"; |
| 2 | +import { Scenario, nowSeconds, IScenario, Config } from "../scenario.js"; |
| 3 | +import { api } from "../convex/_generated/api.js"; |
| 4 | +import { ScenarioError } from "../metrics.js"; |
| 5 | +import { ConvexClient } from "convex/browser"; |
| 6 | +import { MessagesTable, rand } from "../convex/common.js"; |
| 7 | +import { writeFileSync } from "node:fs"; |
| 8 | + |
| 9 | +/** |
| 10 | + * Subscribe to many queries and invalidate a small number of them. |
| 11 | + */ |
| 12 | +export class ManyIntersections extends Scenario implements IScenario { |
| 13 | + table: MessagesTable; |
| 14 | + numSubscriptions: number; |
| 15 | + numDocumentsPerMutation: number; |
| 16 | + |
| 17 | + constructor(config: Config, numSubscriptions = 1000) { |
| 18 | + const name = "ManyIntersections"; |
| 19 | + super(name as any, config); |
| 20 | + this.table = "messages"; |
| 21 | + this.numSubscriptions = numSubscriptions; |
| 22 | + this.numDocumentsPerMutation = 100; |
| 23 | + } |
| 24 | + |
| 25 | + async sendMutation(client: ConvexClient, startTime: number, rand: number) { |
| 26 | + await client.mutation(api.insert.insertMessagesWithArgs, { |
| 27 | + channel: "global", |
| 28 | + timestamp: startTime, |
| 29 | + rand, |
| 30 | + ballastCount: 0, |
| 31 | + count: 1, |
| 32 | + table: this.table, |
| 33 | + n: this.numDocumentsPerMutation, |
| 34 | + }); |
| 35 | + } |
| 36 | + |
| 37 | + async run(client: ConvexClient) { |
| 38 | + writeFileSync( |
| 39 | + "/Users/tomb/log.txt", |
| 40 | + `Making ${this.numDocumentsPerMutation} document writes after ${this.numSubscriptions} subs at ${new Date().toTimeString().split(" ")[0]}...`, |
| 41 | + { |
| 42 | + encoding: "utf-8", |
| 43 | + }, |
| 44 | + ); |
| 45 | + const startTime = nowSeconds(); |
| 46 | + const _ = range(this.numSubscriptions).map(() => { |
| 47 | + void this.subscribe(client, startTime, rand(), `channel-${rand()}`); |
| 48 | + }); |
| 49 | + |
| 50 | + // Wait a sec |
| 51 | + await new Promise((r) => setTimeout(r, 1000)); |
| 52 | + |
| 53 | + const t0 = nowSeconds(); |
| 54 | + const randomNumber = rand(); |
| 55 | + const subscribe = this.subscribe(client, startTime, randomNumber); |
| 56 | + |
| 57 | + // Send the replace mutation or timeout |
| 58 | + await this.executeOrTimeoutWithLatency( |
| 59 | + this.sendMutation(client, startTime, randomNumber), |
| 60 | + MUTATION_TIMEOUT * 50, |
| 61 | + "mutation_send_timeout", |
| 62 | + "mutation_completed", |
| 63 | + t0, |
| 64 | + ); |
| 65 | + |
| 66 | + // Wait until the mutation is observed or timeout |
| 67 | + await this.executeOrTimeoutWithLatency( |
| 68 | + subscribe!, |
| 69 | + QUERY_TIMEOUT * 50, |
| 70 | + "mutation_observed_timeout", |
| 71 | + "mutation_observed", |
| 72 | + t0, |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + // Watch a query on the given offset of the table to observe the insert mutation |
| 77 | + async subscribe( |
| 78 | + client: ConvexClient, |
| 79 | + startTime: number, |
| 80 | + rand: number, |
| 81 | + channel = "global", |
| 82 | + ): Promise<void> { |
| 83 | + await this.waitForQuery( |
| 84 | + client, |
| 85 | + api.query_index.queryMessagesWithArgs, |
| 86 | + { |
| 87 | + channel, |
| 88 | + rand, |
| 89 | + limit: 10, |
| 90 | + table: this.table, |
| 91 | + }, |
| 92 | + (result) => { |
| 93 | + for (const doc of result || []) { |
| 94 | + if (doc.rand === rand && doc.timestamp! >= startTime) { |
| 95 | + return true; |
| 96 | + } |
| 97 | + } |
| 98 | + return false; |
| 99 | + }, |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + defaultErrorName(): ScenarioError { |
| 104 | + return "mutation"; |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +function range(n: number): number[] { |
| 109 | + return Array.from({ length: n }, (_, i) => i); |
| 110 | +} |
0 commit comments