-
Notifications
You must be signed in to change notification settings - Fork 7
add vector search benchmarks #311
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vkarpov15
wants to merge
4
commits into
main
Choose a base branch
from
vkarpov15/benchmarking
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import astraMongoose from '../dist/index.js'; | ||
| import mongoose from 'mongoose'; | ||
|
|
||
| const { driver, tableDefinitionFromSchema } = astraMongoose; | ||
|
|
||
| mongoose.set('autoCreate', false); | ||
| mongoose.set('autoIndex', false); | ||
| mongoose.setDriver(driver); | ||
|
|
||
| const isTable = !!process.env.IS_TABLE; | ||
|
|
||
| await mongoose.connect(process.env.ASTRA_URI, { isAstra: true, isTable }); | ||
|
|
||
| const vectorField = isTable ? 'vector' : '$vector'; | ||
|
|
||
| const contentSchema = new mongoose.Schema({ | ||
| text: { | ||
| type: String, | ||
| required: true | ||
| }, | ||
| [vectorField]: { | ||
| type: [Number], | ||
| validate: v => v == null || v.length === 512, | ||
| default: undefined, | ||
| dimension: 512, | ||
| index: { name: 'content_vector', vector: true } | ||
| } | ||
| }, { versionKey: false }); | ||
| const ContentModel = mongoose.model('Content', contentSchema, 'content'); | ||
|
|
||
| const content = await ContentModel.findOne().select({ '*': 1 }).orFail(); | ||
| const $meta = [...content[vectorField]]; | ||
| $meta[0] = 0.001; | ||
|
|
||
| const start = process.hrtime.bigint(); | ||
|
|
||
| const totalQueries = 2000; | ||
| const parallelism = 10; | ||
|
|
||
| for (let i = 0; i < totalQueries; i += parallelism) { | ||
| const batchSize = Math.min(parallelism, totalQueries - i); | ||
| await Promise.all( | ||
| Array.from({ length: batchSize }, () => | ||
| ContentModel.find().limit(10).select({ [vectorField]: 0 }).sort({ [vectorField]: { $meta } }) | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| const end = process.hrtime.bigint(); | ||
| const seconds = Number(end - start) / 1e9; | ||
|
|
||
| console.log(JSON.stringify({ | ||
| queries: totalQueries, | ||
| parallelism, | ||
| seconds: +seconds.toFixed(6), | ||
| queriesPerSecond: +(totalQueries / seconds).toFixed(6), | ||
| secondsPerBatch: +(seconds / (totalQueries / parallelism)).toFixed(6) | ||
| })); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| import astraMongoose from '../dist/index.js'; | ||
| import fs from 'node:fs/promises'; | ||
| import mongoose from 'mongoose'; | ||
|
|
||
| const { driver, tableDefinitionFromSchema } = astraMongoose; | ||
|
|
||
| mongoose.set('autoCreate', false); | ||
| mongoose.set('autoIndex', false); | ||
| mongoose.setDriver(driver); | ||
|
|
||
| const isTable = !!process.env.IS_TABLE; | ||
|
|
||
| await mongoose.connect(process.env.ASTRA_URI, { isAstra: true, isTable }); | ||
|
|
||
| const vectorField = isTable ? 'vector' : '$vector'; | ||
|
|
||
| const contentSchema = new mongoose.Schema({ | ||
| text: { | ||
| type: String, | ||
| required: true | ||
| }, | ||
| [vectorField]: { | ||
| type: [Number], | ||
| validate: v => v == null || v.length === 512, | ||
| default: undefined, | ||
| dimension: 512, | ||
| index: { name: 'content_vector', vector: true } | ||
| } | ||
| }, { | ||
| versionKey: false, | ||
| autoCreate: false, | ||
| collectionOptions: { vector: { dimension: 512, metric: 'cosine' } } | ||
| }); | ||
| const ContentModel = mongoose.model('Content', contentSchema, 'content'); | ||
|
|
||
| const tables = await mongoose.connection.listTables(); | ||
| const collections = await mongoose.connection.listCollections(); | ||
|
|
||
| console.log('Tables', tables); | ||
| console.log('Collections', collections); | ||
|
|
||
| if (isTable) { | ||
| if (collections.find(collection => collection.name === 'content')) { | ||
| await mongoose.connection.dropCollection('content'); | ||
| } | ||
| await mongoose.connection.collection('content').syncTable( | ||
| tableDefinitionFromSchema(contentSchema) | ||
| ); | ||
| } else { | ||
| const hasTable = tables.find(table => table.name === 'content'); | ||
| const hasCollection = collections.find(collection => collection.name === 'content'); | ||
|
|
||
| if (hasTable) { | ||
| await mongoose.connection.dropTable('content'); | ||
| } | ||
|
|
||
| if (!hasCollection) { | ||
| await ContentModel.createCollection(); | ||
| } | ||
| } | ||
|
|
||
| await ContentModel.deleteMany(); | ||
| if (isTable) { | ||
| await ContentModel.syncIndexes(); | ||
| } | ||
|
|
||
| const moviesPath = process.env.MOVIES_JSON_PATH || './movies.json'; | ||
|
|
||
| let movies; | ||
| try { | ||
| const moviesRaw = await fs.readFile(moviesPath, 'utf8'); | ||
| movies = JSON.parse(moviesRaw); | ||
| } catch (err) { | ||
| console.error(`Failed to load movies dataset from "${moviesPath}".`); | ||
| console.error('Set the MOVIES_JSON_PATH environment variable to point to a valid movies.json file.'); | ||
| console.error('Original error:', err?.message || err); | ||
| process.exit(1); | ||
| } | ||
| const batchSize = 20; | ||
| const start = process.hrtime.bigint(); | ||
|
|
||
| for (let i = 0; i < movies.length; i += batchSize) { | ||
| const batch = movies.slice(i, i + batchSize).map(m => ({ | ||
| text: [m?.title, m?.plot, m?.fullplot].filter(Boolean).join('\n\n').slice(0, 5000), | ||
| [vectorField]: m.vector | ||
| })); | ||
|
|
||
| await ContentModel.insertMany(batch, { ordered: false }); | ||
| } | ||
|
|
||
| const end = process.hrtime.bigint(); | ||
| const seconds = Number(end - start) / 1e9; | ||
|
|
||
| console.log(JSON.stringify({ | ||
| inserted: movies.length, | ||
| seconds: +seconds.toFixed(6), | ||
| docsPerSecond: +(movies.length / seconds).toFixed(6), | ||
| secondsPerBatch: +(seconds / (movies.length / batchSize)).toFixed(6) | ||
| })); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.