|
| 1 | +/* |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) 2019-5/26/26, 5:48 PM The Polypheny Project |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | + * copy of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +package org.polypheny.simpleclient.scenario.vectorbench; |
| 26 | + |
| 27 | +import lombok.extern.slf4j.Slf4j; |
| 28 | +import org.polypheny.simpleclient.QueryMode; |
| 29 | +import org.polypheny.simpleclient.executor.Executor; |
| 30 | +import org.polypheny.simpleclient.executor.Executor.DatabaseInstance; |
| 31 | +import org.polypheny.simpleclient.executor.ExecutorException; |
| 32 | +import org.polypheny.simpleclient.main.CsvWriter; |
| 33 | +import org.polypheny.simpleclient.main.ProgressReporter; |
| 34 | +import org.polypheny.simpleclient.query.Query; |
| 35 | +import org.polypheny.simpleclient.query.QueryBuilder; |
| 36 | +import org.polypheny.simpleclient.query.QueryListEntry; |
| 37 | +import org.polypheny.simpleclient.query.RawQuery; |
| 38 | +import org.polypheny.simpleclient.scenario.PolyphenyScenario; |
| 39 | +import org.polypheny.simpleclient.scenario.vectorbench.queryBuilder.postgres.ddl.PgCreateRealFeature; |
| 40 | +import org.polypheny.simpleclient.scenario.vectorbench.queryBuilder.postgres.dql.PgSimpleKnnRealFeature; |
| 41 | +import org.polypheny.simpleclient.scenario.vectorbench.queryBuilder.postgres.dql.PgSimpleKnnRealFeatureFiltered; |
| 42 | +import java.io.File; |
| 43 | +import java.util.Collections; |
| 44 | +import java.util.LinkedList; |
| 45 | +import java.util.List; |
| 46 | +import java.util.Random; |
| 47 | +import java.util.Vector; |
| 48 | + |
| 49 | + |
| 50 | +@Slf4j |
| 51 | +public class PgVectorBench extends PolyphenyScenario { |
| 52 | + |
| 53 | + private final VectorBenchConfig config; |
| 54 | + |
| 55 | + |
| 56 | + public PgVectorBench( Executor.ExecutorFactory executorFactory, VectorBenchConfig config, |
| 57 | + boolean commitAfterEveryQuery, boolean dumpQueryList ) { |
| 58 | + super( executorFactory, commitAfterEveryQuery, dumpQueryList, QueryMode.TABLE ); |
| 59 | + this.config = config; |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + @Override |
| 64 | + public void createSchema( DatabaseInstance databaseInstance, boolean includingKeys ) { |
| 65 | + log.info( "Creating schema..." ); |
| 66 | + Executor executor = null; |
| 67 | + try { |
| 68 | + executor = executorFactory.createExecutorInstance(); |
| 69 | + executor.executeQuery( new RawQuery( "CREATE EXTENSION IF NOT EXISTS vector", null, false ) ); |
| 70 | + executor.executeQuery( new PgCreateRealFeature( config.dimensionFeatureVectors ).getNewQuery() ); |
| 71 | + } catch ( ExecutorException e ) { |
| 72 | + throw new RuntimeException( "Exception while creating schema", e ); |
| 73 | + } finally { |
| 74 | + commitAndCloseExecutor( executor ); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | + @Override |
| 80 | + public void generateData( DatabaseInstance databaseInstance, ProgressReporter progressReporter ) { |
| 81 | + log.info( "Generating data..." ); |
| 82 | + Executor executor = executorFactory.createExecutorInstance(); |
| 83 | + PgDataGenerator dataGenerator = new PgDataGenerator( executor, config, progressReporter ); |
| 84 | + try { |
| 85 | + dataGenerator.generateRealFeatures(); |
| 86 | + } catch ( ExecutorException e ) { |
| 87 | + throw new RuntimeException( "Exception while generating data", e ); |
| 88 | + } finally { |
| 89 | + commitAndCloseExecutor( executor ); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | + @Override |
| 95 | + public long execute( ProgressReporter progressReporter, CsvWriter csvWriter, File outputDirectory, int numberOfThreads ) { |
| 96 | + log.info( "Preparing query list..." ); |
| 97 | + List<QueryListEntry> queryList = new Vector<>(); |
| 98 | + addNumberOfTimes( queryList, new PgSimpleKnnRealFeature( config.randomSeedQuery, config.dimensionFeatureVectors, config.limitKnnQueries, config.distanceNorm ), config.numberOfSimpleKnnRealFeatureQueries ); |
| 99 | + addNumberOfTimes( queryList, new PgSimpleKnnRealFeatureFiltered( config.randomSeedQuery, config.dimensionFeatureVectors, config.limitKnnQueries, config.distanceNorm, "cat_A" ), config.numberOfSimpleKnnRealFeatureFilteredQueries ); |
| 100 | + return commonExecute( queryList, progressReporter, outputDirectory, numberOfThreads, |
| 101 | + Query::getSql, () -> executorFactory.createExecutorInstance( csvWriter ), new Random() ); |
| 102 | + } |
| 103 | + |
| 104 | + |
| 105 | + @Override |
| 106 | + public void warmUp( ProgressReporter progressReporter ) { |
| 107 | + log.info( "Warm-up..." ); |
| 108 | + PgSimpleKnnRealFeature knnBuilder = new PgSimpleKnnRealFeature( config.randomSeedQuery, config.dimensionFeatureVectors, config.limitKnnQueries, config.distanceNorm ); |
| 109 | + Executor executor = null; |
| 110 | + for ( int i = 0; i < config.numberOfWarmUpIterations; i++ ) { |
| 111 | + try { |
| 112 | + executor = executorFactory.createExecutorInstance(); |
| 113 | + if ( config.numberOfSimpleKnnRealFeatureQueries > 0 ) { |
| 114 | + executor.executeQuery( knnBuilder.getNewQuery() ); |
| 115 | + } |
| 116 | + } catch ( ExecutorException e ) { |
| 117 | + throw new RuntimeException( "Error during warm-up", e ); |
| 118 | + } finally { |
| 119 | + commitAndCloseExecutor( executor ); |
| 120 | + } |
| 121 | + try { |
| 122 | + Thread.sleep( 10000 ); |
| 123 | + } catch ( InterruptedException e ) { |
| 124 | + throw new RuntimeException( "Interrupted during warm-up", e ); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + |
| 130 | + @Override |
| 131 | + public int getNumberOfInsertThreads() { |
| 132 | + return 1; |
| 133 | + } |
| 134 | + |
| 135 | + |
| 136 | + private void addNumberOfTimes( List<QueryListEntry> list, QueryBuilder builder, int count ) { |
| 137 | + int id = queryTypes.size() + 1; |
| 138 | + queryTypes.put( id, builder.getNewQuery().getSql() ); |
| 139 | + measuredTimePerQueryType.put( id, Collections.synchronizedList( new LinkedList<>() ) ); |
| 140 | + for ( int i = 0; i < count; i++ ) { |
| 141 | + list.add( new QueryListEntry( builder.getNewQuery(), id ) ); |
| 142 | + } |
| 143 | + } |
| 144 | +} |
| 145 | + |
0 commit comments