Skip to content

Commit

Permalink
Add test suite for loader latency
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulLeCam committed Feb 6, 2024
1 parent 4a2d328 commit 1754451
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 106 deletions.
9 changes: 9 additions & 0 deletions packages/loader/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,17 @@ import type { CommitID, StreamID } from '@ceramicnetwork/streamid'
export type DocID = CommitID | StreamID | string

export type LoadKey = {
/**
* Document ID
*/
id: DocID
/**
* Optional genesis commit for deterministic streams
*/
genesis?: GenesisCommit
/**
* Stream load options
*/
opts?: LoadOpts
}

Expand Down
4 changes: 2 additions & 2 deletions packages/loader/test/deterministic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ describe('deterministic', () => {

beforeAll(async () => {
const [favoriteComposite, profileComposite] = await Promise.all([
await Composite.create({ ceramic, schema: favoriteSchema }),
await Composite.create({ ceramic, schema: profilesSchema }),
Composite.create({ ceramic, schema: favoriteSchema }),
Composite.create({ ceramic, schema: profilesSchema }),
])
favoriteModelID = favoriteComposite.getModelID('Favorite')!
profileModelID = profileComposite.getModelID('PersonProfile')!
Expand Down
76 changes: 76 additions & 0 deletions packages/loader/test/latency.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* @jest-environment composedb
*/

import { SyncOptions } from '@ceramicnetwork/common'
import { ModelInstanceDocument } from '@ceramicnetwork/stream-model-instance'
import { StreamID } from '@ceramicnetwork/streamid'
import { Composite } from '@composedb/devtools'
import { favoriteSchema } from '@composedb/test-schemas'
import type { CeramicAPI } from '@composedb/types'

import { createDeterministicKey } from '../src/deterministic'
import { DocumentLoader } from '../src/loader'

declare global {
const ceramic: CeramicAPI
}

describe('latency', () => {
const controller = ceramic.did!.id
let modelID: string

beforeAll(async () => {
const composite = await Composite.create({ ceramic, schema: favoriteSchema })
modelID = composite.getModelID('Favorite')!
})

test('using ModelInstanceDocument.set()', async () => {
const model = StreamID.fromString(modelID)
console.time('using ModelInstanceDocument.set()')
await ModelInstanceDocument.set(ceramic, { controller, model }, ['one', 'one'])
console.timeLog('using ModelInstanceDocument.set()', 'first load')
await ModelInstanceDocument.set(ceramic, { controller, model }, ['one', 'one'])
console.timeEnd('using ModelInstanceDocument.set()')
})

test('using ceramic.multiQuery()', async () => {
const { id, genesis } = await createDeterministicKey({
controller,
model: modelID,
unique: ['two', 'two'],
})
console.time('using ceramic.multiQuery()')
await ceramic.multiQuery([{ streamId: id, genesis, opts: { sync: SyncOptions.NEVER_SYNC } }])
console.timeLog('using ceramic.multiQuery()', 'first load')
await ceramic.multiQuery([{ streamId: id, genesis, opts: { sync: SyncOptions.NEVER_SYNC } }])
console.timeEnd('using ceramic.multiQuery()')
})

test('using loader.loadSet()', async () => {
const loader = new DocumentLoader({ ceramic })
console.time('using loader.loadSet()')
await loader.loadSet(controller, modelID, ['three', 'three'])
console.timeLog('using loader.loadSet()', 'first load')
await loader.loadSet(controller, modelID, ['three', 'three'])
console.timeEnd('using loader.loadSet()')
})

test('using ceramic.index.query()', async () => {
console.time('ceramic.index.query()')
await ceramic.index.query({
account: controller,
models: [modelID],
first: 1,
queryFilters: { where: { docID: { equalTo: 'four' }, tag: { equalTo: 'four' } } },
})
console.timeLog('ceramic.index.query()', 'first load')
await ceramic.index.query({
account: controller,
models: [modelID],
first: 1,
queryFilters: { where: { docID: { equalTo: 'four' }, tag: { equalTo: 'four' } } },
})
console.timeEnd('ceramic.index.query()')
})
})
10 changes: 5 additions & 5 deletions website/docs/api/modules/loader.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ ___

#### Type declaration

| Name | Type |
| :------ | :------ |
| `genesis?` | `GenesisCommit` |
| `id` | [`DocID`](loader.md#docid) |
| `opts?` | `LoadOpts` |
| Name | Type | Description |
| :------ | :------ | :------ |
| `genesis?` | `GenesisCommit` | Optional genesis commit for deterministic streams |
| `id` | [`DocID`](loader.md#docid) | Document ID |
| `opts?` | `LoadOpts` | Stream load options |

___

Expand Down
Loading

0 comments on commit 1754451

Please sign in to comment.