Skip to content

Commit a0fe1cb

Browse files
committed
Improve performance of SingleRestrictionEstimatedRowCountTest
Reduces amount of created tables by creating all needed tables in advance. As the result the test can be placed into single test function. This improves local test execution time from 5.5 seconds down to 1.4 seconds.
1 parent b870d38 commit a0fe1cb

File tree

1 file changed

+38
-36
lines changed

1 file changed

+38
-36
lines changed

test/unit/org/apache/cassandra/index/sai/plan/SingleRestrictionEstimatedRowCountTest.java

+38-36
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@
4545

4646
public class SingleRestrictionEstimatedRowCountTest extends SAITester
4747
{
48-
static protected Map<Map.Entry<Version, CQL3Type.Native>, ColumnFamilyStore> map = new HashMap<>();
48+
static protected Map<Map.Entry<Version, CQL3Type.Native>, ColumnFamilyStore> tables = new HashMap<>();
49+
static Version[] versions = new Version[]{ Version.DB, Version.EB };
50+
static CQL3Type.Native[] types = new CQL3Type.Native[]{ INT, DECIMAL, VARINT };
4951

5052
static protected Object getFilterValue(CQL3Type.Native type, int value)
5153
{
@@ -62,66 +64,74 @@ static protected Object getFilterValue(CQL3Type.Native type, int value)
6264
return null;
6365
}
6466

67+
static Map.Entry<Version, CQL3Type.Native> tablesEntrykey(Version version, CQL3Type.Native type)
68+
{
69+
return new AbstractMap.SimpleEntry<>(version, type);
70+
}
71+
6572
@Test
66-
public void testInequality()
73+
public void testMemtablesSAI()
6774
{
75+
createTables();
76+
6877
var test = new RowCountTest(Operator.NEQ, 25);
6978
test.doTest(Version.DB, INT, 97.0);
7079
test.doTest(Version.EB, INT, 97.0);
7180
// Truncated numeric types planned differently
7281
test.doTest(Version.DB, DECIMAL, 97.0);
7382
test.doTest(Version.EB, DECIMAL, 97.0);
7483
test.doTest(Version.EB, VARINT, 97.0);
75-
}
7684

77-
@Test
78-
public void testHalfRangeMiddle()
79-
{
80-
var test = new RowCountTest(Operator.LT, 50);
85+
test = new RowCountTest(Operator.LT, 50);
8186
test.doTest(Version.DB, INT, 48);
8287
test.doTest(Version.EB, INT, 48);
8388
test.doTest(Version.DB, DECIMAL, 48);
8489
test.doTest(Version.EB, DECIMAL, 48);
85-
}
8690

87-
@Test
88-
public void testHalfRangeEverything()
89-
{
90-
var test = new RowCountTest(Operator.LT, 150);
91+
test = new RowCountTest(Operator.LT, 150);
9192
test.doTest(Version.DB, INT, 97);
9293
test.doTest(Version.EB, INT, 97);
9394
test.doTest(Version.DB, DECIMAL, 97);
9495
test.doTest(Version.EB, DECIMAL, 97);
95-
}
9696

97-
@Test
98-
public void testEquality()
99-
{
100-
var test = new RowCountTest(Operator.EQ, 31);
97+
test = new RowCountTest(Operator.EQ, 31);
10198
test.doTest(Version.DB, INT, 15);
10299
test.doTest(Version.EB, INT, 0);
103100
test.doTest(Version.DB, DECIMAL, 15);
104101
test.doTest(Version.EB, DECIMAL, 0);
105102
}
106103

107-
protected ColumnFamilyStore prepareTable(CQL3Type.Native type)
104+
105+
void createTables()
108106
{
109-
createTable("CREATE TABLE %s (pk text PRIMARY KEY, age " + type + ')');
110-
createIndex("CREATE CUSTOM INDEX ON %s(age) USING 'StorageAttachedIndex'");
111-
ColumnFamilyStore cfs = getCurrentColumnFamilyStore();
107+
for (Version version : versions)
108+
{
109+
SAIUtil.setLatestVersion(version);
110+
for (CQL3Type.Native type : types)
111+
{
112+
createTable("CREATE TABLE %s (pk text PRIMARY KEY, age " + type + ')');
113+
createIndex("CREATE CUSTOM INDEX ON %s(age) USING 'StorageAttachedIndex'");
114+
tables.put(tablesEntrykey(version, type), getCurrentColumnFamilyStore());
115+
}
116+
}
117+
flush();
118+
for (ColumnFamilyStore cfs : tables.values())
119+
populateTable(cfs);
120+
}
112121

113-
// Avoid race condition of flushing after the index creation
122+
void populateTable(ColumnFamilyStore cfs)
123+
{
124+
// Avoid race condition of starting before flushing completed
114125
cfs.unsafeRunWithoutFlushing(() -> {
115126
for (int i = 0; i < 100; i++)
116127
{
117-
execute("INSERT INTO %s (pk, age) VALUES (?," + i + ')', "key" + i);
128+
String query = String.format("INSERT INTO %s (pk, age) VALUES (?, " + i + ')', cfs.keyspace.getName() + '.' + cfs.name);
129+
executeFormattedQuery(query, "key" + i);
118130
}
119131
});
120-
121-
return cfs;
122132
}
123133

124-
class RowCountTest
134+
static class RowCountTest
125135
{
126136
final Operator op;
127137
final int filterValue;
@@ -134,15 +144,8 @@ class RowCountTest
134144

135145
void doTest(Version version, CQL3Type.Native type, double expectedRows)
136146
{
137-
Version latest = Version.latest();
138-
SAIUtil.setLatestVersion(version);
139-
140-
var key = new AbstractMap.SimpleEntry<>(version, type);
141-
ColumnFamilyStore cfs = map.computeIfAbsent(key, k -> prepareTable(type));
142-
143-
// ColumnFamilyStore cfs = prepareTable(type);
147+
ColumnFamilyStore cfs = tables.get(new AbstractMap.SimpleEntry<>(version, type));
144148
Object filter = getFilterValue(type, filterValue);
145-
146149
ReadCommand rc = Util.cmd(cfs)
147150
.columns("age")
148151
.filterOn("age", op, filter)
@@ -152,6 +155,7 @@ void doTest(Version version, CQL3Type.Native type, double expectedRows)
152155
version.onDiskFormat().indexFeatureSet(),
153156
new QueryContext(),
154157
null);
158+
155159
long totalRows = controller.planFactory.tableMetrics.rows;
156160
assertEquals(0, cfs.metrics().liveSSTableCount.getValue().intValue());
157161
assertEquals(97, totalRows);
@@ -165,8 +169,6 @@ void doTest(Version version, CQL3Type.Native type, double expectedRows)
165169
assertEquals(expectedRows, root.expectedRows(), 0.1);
166170
assertEquals(expectedRows, planNode.expectedKeys(), 0.1);
167171
assertEquals(expectedRows / totalRows, planNode.selectivity(), 0.001);
168-
169-
SAIUtil.setLatestVersion(latest);
170172
}
171173
}
172174
}

0 commit comments

Comments
 (0)