1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
101.
102.
103.
104.
105.
106.
107.
108.
109.
110.
111.
112.
113.
114.
115.
116.
117.
118.
119.
120.
121.
122.
123.
124.
125.
126.
127.
128.
129.
130.
131.
132.
133.
134.
135.
136.
137.
138.
139.
140.
141.
142.
143.
144.
145.
146.
147.
148.
149.
150.
151.
152.
153.
154.
155.
156.
157.
158.
159.
160.
161.
162.
163.
164.
165.
166.
167.
168.
169.
170.
171.
172.
173.
174.
175.
176.
177.
178.
179.
180.
181.
182.
183.
184.
185.
186.
187.
188.
189.
190.
191.
192.
193.
194.
195.
196.
197.
198.
199.
200.
201.
202.
203.
204.
205.
206.
207.
208.
209.
210.
211.
212.
213.
214.
215.
216.
217.
218.
219.
220.
221.
222.
223.
224.
225.
226.
227.
228.
229.
230.
231.
232.
233.
234.
235.
236.
237.
238.
239.
240.
241.
242.
243.
244.
245.
246.
247.
248.
249.
250.
251.
252.
253.
254.
255.
256.
257.
258.
259.
260.
261.
262.
263.
264.
265.
266.
267.
268.
269.
270.
271.
272.
273.
274.
275.
276.
277.
278.
279.
280.
281.
282.
283.
284.
285.
286.
287.
288.
289.
290.
291.
292.
293.
294.
295.
296.
297.
298.
299.
300.
301.
302.
303.
304.
305.
306.
307.
308.
309.
310.
311.
312.
313.
314.
315.
316.
317.
318.
319.
320.
321.
322.
323.
324.
325.
326.
327.
328.
329.
330.
331.
332.
333.
334.
335.
336.
337.
338.
339.
340.
341.
342.
343.
344.
345.
346.
347.
348.
349.
350.
351.
352.
353.
354.
355.
356.
357.
358.
359.
360.
361.
362.
363.
364.
365.
366.
367.
368.
369.
370.
371.
372.
373.
374.
375.
376.
377.
378.
379.
380.
381.
382.
383.
384.
385.
386.
387.
388.
389.
390.
391.
392.
393.
394.
395.
396.
397.
398.
399.
400.
401.
402.
403.
404.
405.
406.
407.
408.
409.
410.
411.
412.
413.
414.
415.
416.
417.
418.
419.
420.
421.
422.
423.
424.
425.
426.
427.
428.
429.
430.
431.
432.
433.
434.
435.
436.
437.
438.
439.
440.
441.
442.
443.
444.
445.
446.
447.
448.
449.
450.
451.
452.
453.
454.
455.
456.
457.
458.
459.
460.
461.
462.
463.
464.
465.
466.
467.
468.
469.
470.
471.
472.
473.
474.
475.
476.
477.
478.
479.
480.
481.
482.
483.
484.
485.
486.
487.
488.
489.
490.
491.
492.
493.
494.
495.
496.
497.
498.
499.
500.
501.
502.
503.
504.
505.
506.
507.
508.
509.
510.
511.
512.
513.
514.
515.
516.
517.
518.
519.
520.
521.
522.
523.
524.
525.
526.
527.
528.
529.
530.
531.
532.
533.
534.
535.
536.
537.
538.
539.
540.
541.
542.
543.
544.
545.
546.
547.
548.
549.
550.
551.
552.
553.
554.
555.
556.
557.
558.
559.
560.
561.
562.
563.
564.
565.
566.
567.
568.
569.
570.
571.
572.
573.
574.
575.
576.
577.
578.
579.
580.
581.
582.
583.
584.
585.
586.
587.
588.
589.
590.
591.
592.
593.
594.
595.
596.
597.
598.
599.
600.
601.
602.
603.
604.
605.
606.
607.
608.
609.
610.
611.
612.
613.
614.
615.
616.
617.
618.
619.
620.
621.
622.
623.
624.
625.
626.
627.
628.
629.
630.
631.
632.
633.
634.
635.
636.
637.
638.
639.
640.
641.
642.
643.
644.
645.
646.
647.
648.
649.
650.
651.
652.
653.
654.
655.
656.
657.
658.
659.
660.
661.
662.
663.
664.
665.
666.
667.
668.
669.
670.
671.
672.
673.
674.
675.
676.
677.
678.
679.
680.
681.
682.
683.
684.
685.
686.
687.
688.
689.
690.
691.
692.
693.
694.
695.
696.
697.
698.
699.
700.
701.
702.
703.
704.
705.
706.
707.
708.
709.
710.
711.
712.
713.
714.
715.
716.
717.
718.
719.
720.
721.
722.
723.
724.
725.
726.
727.
728.
729.
730.
731.
732.
733.
734.
735.
736.
737.
738.
739.
740.
741.
742.
743.
744.
745.
746.
747.
748.
749.
750.
751.
752.
753.
754.
755.
756.
757.
758.
759.
760.
761.
762.
763.
764.
765.
766.
767.
768.
769.
770.
771.
772.
773.
774.
775.
776.
777.
778.
779.
780.
781.
782.
783.
784.
785.
786.
787.
788.
789.
790.
791.
792.
793.
794.
795.
796.
797.
798.
799.
800.
801.
802.
803.
804.
805.
806.
807.
808.
809.
810.
811.
812.
813.
814.
815.
816.
817.
818.
819.
820.
821.
822.
823.
824.
825.
826.
827.
828.
829.
830.
831.
832.
833.
834.
835.
836.
837.
838.
839.
840.
841.
842.
843.
844.
845.
846.
847.
848.
849.
850.
851.
852.
853.
854.
855.
856.
857.
858.
859.
860.
861.
862.
863.
864.
865.
866.
867.
868.
869.
870.
871.
872.
873.
874.
875.
876.
877.
878.
879.
880.
881.
882.
883.
884.
885.
886.
887.
888.
889.
890.
891.
892.
893.
894.
895.
896.
897.
898.
899.
900.
901.
902.
903.
904.
905.
906.
907.
908.
909.
910.
911.
912.
913.
914.
915.
916.
917.
918.
919.
920.
921.
922.
923.
924.
925.
926.
927.
928.
929.
930.
931.
932.
933.
934.
935.
936.
937.
938.
939.
940.
941.
942.
943.
944.
945.
946.
947.
948.
949.
950.
951.
952.
953.
954.
955.
956.
957.
958.
959.
960.
961.
962.
963.
964.
965.
966.
967.
968.
969.
970.
971.
972.
973.
974.
975.
976.
977.
978.
979.
980.
981.
982.
983.
984.
985.
986.
987.
988.
989.
990.
991.
992.
993.
994.
995.
996.
997.
998.
999.
1000.
1001.
1002.
1003.
1004.
1005.
1006.
1007.
1008.
1009.
1010.
1011.
1012.
1013.
1014.
1015.
1016.
1017.
1018.
1019.
1020.
1021.
1022.
1023.
1024.
1025.
1026.
1027.
1028.
1029.
1030.
1031.
1032.
1033.
1034.
1035.
1036.
1037.
1038.
1039.
1040.
1041.
1042.
1043.
1044.
1045.
1046.
1047.
1048.
1049.
1050.
1051.
1052.
1053.
1054.
1055.
1056.
1057.
1058.
1059.
1060.
1061.
1062.
1063.
1064.
1065.
1066.
1067.
1068.
1069.
1070.
1071.
1072.
1073.
1074.
1075.
1076.
1077.
1078.
1079.
1080.
1081.
1082.
1083.
1084.
1085.
1086.
1087.
1088.
1089.
1090.
1091.
1092.
1093.
1094.
1095.
1096.
1097.
1098.
1099.
1100.
1101.
1102.
1103.
1104.
1105.
1106.
1107.
1108.
1109.
1110.
1111.
1112.
1113.
1114.
1115.
1116.
1117.
1118.
1119.
1120.
1121.
1122.
1123.
1124.
1125.
1126.
1127.
1128.
1129.
1130.
1131.
1132.
1133.
1134.
1135.
1136.
1137.
1138.
1139.
1140.
1141.
1142.
1143.
1144.
1145.
1146.
1147.
1148.
1149.
1150.
1151.
1152.
1153.
1154.
1155.
1156.
1157.
1158.
1159.
1160.
1161.
1162.
1163.
1164.
1165.
1166.
1167.
1168.
1169.
1170.
1171.
1172.
1173.
1174.
1175.
1176.
1177.
1178.
1179.
1180.
1181.
1182.
1183.
1184.
1185.
1186.
1187.
1188.
1189.
1190.
1191.
1192.
1193.
1194.
1195.
1196.
1197.
1198.
1199.
1200.
1201.
1202.
1203.
1204.
1205.
1206.
1207.
1208.
1209.
1210.
1211.
1212.
1213.
1214.
1215.
1216.
1217.
1218.
1219.
1220.
1221.
1222.
1223.
1224.
1225.
1226.
1227.
1228.
1229.
1230.
1231.
1232.
1233.
1234.
1235.
1236.
1237.
1238.
1239.
1240.
1241.
1242.
1243.
1244.
1245.
1246.
1247.
1248.
1249.
1250.
1251.
1252.
1253.
1254.
1255.
1256.
1257.
1258.
1259.
package com.borland.samples.dx.benchmark;
import com.borland.datastore.*;
import com.borland.dx.dataset.*;
import com.borland.dx.sql.dataset.*;
import com.borland.dx.sql.metadata.MetaData;
import java.io.*;
import java.sql.*;
import java.math.BigDecimal;
import java.util.TooManyListenersException;
public class JDBCBenchMark
{
static String tempDir = getTempDir(); // Directory of temporary JDataStore used to store benchmark test results (defaults to user's home directory)
public static void main(String args[]) {
new JDBCBenchMark().runTest(args);
}
public final void runTest(String args[]) {
batchSize = 50; // number of rows to operate on per batch operation.
batchCount = 5; // number of batch operations to perform on a batch size batch.
%af_src_comm_0
fast = true; // take measures for optimal performance.
cacheStatements = false; // JDBC test cases will cache statments. Disabled by default
// because not as valid with optimistic concurrency insert/delete/update
// operations and there is a resource overhead for holding onto statements.
// Probably a more valid optimization for fetch operations.
noResetStatus = true ; // Reset RowStatus so secondary saveChanges can be performed
// without refetching a fresh copy of the data.
garbageCollect = true; // force garbage collection inbetween each timing.
verbose = false; // verbose output
useDataStore = false; // Use a dataStore component for storage.
sleepInterval = 0; // sleep interval inbetween each timing. (To quiesce the system.)
JDBCDriver driver;
if (args == null || args.length < 1)
driver = new DataStoreDriver();
else if (args[0].equalsIgnoreCase("interbase"))
driver = new JDBCDriver();
else if (args[0].equalsIgnoreCase("mssql"))
driver = new MSSQLDriver();
else if (args[0].equalsIgnoreCase("db2"))
driver = new DB2Driver();
else if (args[0].equalsIgnoreCase("sybase"))
driver = new SybaseSQLDriver();
else if (args[0].equalsIgnoreCase("datastore"))
driver = new DataStoreDriver();
else
driver = new DataStoreDriver();
driver.connect();
database = driver.database;
connection = driver.connection;
reportProperties(driver, args);
init(driver);
// Shows that there is minimal overhead for accessing a dataSet the way
// the jdbc* tests do.
//
dataSetAccessOverhead();
System.out.println("\n*** JDBC insert/update/delete");
jdbcInsertRows(batchCount);
jdbcUpdateRows();
jdbcDeleteRows();
report();
// Quiesce the system before starting the DataExpress tests.
//
sleep(1000);
init(driver);
System.out.println("*** insert/update/delete");
// First call to saveChanges() is slower due to meta data calls made
// against java.sql.DatabaseMetaData such as getIdentifierQuoteString()
// and getMaxStatements(). Note that this is a one time cost for the
// life time of the Database component.
//
insertRows();
updateRows();
deleteRows();
report();
// Add rows back in, but empty timings out since jdbcInsertRows already timed.
// Note that use only batch count of 1 so queries are done for batchSize
// rows.
//
jdbcInsertRows(1);
try {
timings.empty();
}
catch (Exception ex) {
fail(ex);
}
System.out.println("\n*** JDBC fetch tests");
jdbcFetchRows();
report();
System.out.println("\n*** DataExpress fetch tests");
fetchRows();
refetchRows();
fetchRowsUncached();
fetchRowsMetaDataUpdateNone();
report();
System.out.println("*** JDBCBenchMark test end");
System.exit(1);
}
private final void fetchRows() {
try {
for (int index = 0; index < batchCount; ++index) {
genDataSet.close();
genDataSet.setQuery(new QueryDescriptor(database, sqlQuery));
genDataSet.setMetaDataUpdate(MetaDataUpdate.ALL);
start();
// java.sql.DriverManager.setLogStream(System.out);
genDataSet.open();
if (fast)
connection.commit();
// java.sql.DriverManager.setLogStream(null);
complete();
record("fetchRows");
}
}
catch (Exception ex) {
fail(ex);
}
}
private final void refetchRows() {
try {
for (int index = 0; index < batchCount; ++index) {
genDataSet.close();
genDataSet.setQuery(new QueryDescriptor(database, sqlQuery));
genDataSet.setMetaDataUpdate(MetaDataUpdate.ALL);
genDataSet.open();
start();
// java.sql.DriverManager.setLogStream(System.out);
genDataSet.executeQuery();
if (fast)
connection.commit();
// java.sql.DriverManager.setLogStream(null);
complete();
record("refetchRows");
}
}
catch (Exception ex) {
fail(ex);
}
}
private final void fetchRowsMetaDataUpdateNone() {
try {
for (int index = 0; index < batchCount; ++index) {
genDataSet.close();
genDataSet.setQuery(new QueryDescriptor(database, sqlQuery));
genDataSet.setMetaDataUpdate(MetaDataUpdate.NONE);
start();
// java.sql.DriverManager.setLogStream(System.out);
genDataSet.open();
if (fast)
connection.commit();
// java.sql.DriverManager.setLogStream(null);
complete();
record("fetchRowsMetaDataUpdateNone");
}
}
catch (Exception ex) {
fail(ex);
}
}
private final void fetchRowsUncached() {
try {
for (int index = 0; index < batchCount; ++index) {
genDataSet.close();
genDataSet.setQuery(new QueryDescriptor(database, sqlQuery, null, true, Load.UNCACHED));
genDataSet.setMetaDataUpdate(MetaDataUpdate.NONE);
start();
// java.sql.DriverManager.setLogStream(System.out);
genDataSet.open();
while (genDataSet.next())
;
if (fast)
connection.commit();
// java.sql.DriverManager.setLogStream(null);
complete();
record("fetchRowsUncached");
}
}
catch (Exception ex) {
fail(ex);
}
}
private final void jdbcFetchRows() {
try {
for (int index = 0; index < batchCount; ++index) {
if (!cacheStatements)
start();
// java.sql.DriverManager.setLogStream(System.out);
PreparedStatement statement = connection.prepareStatement(sqlQuery);
if (cacheStatements)
start();
ResultSet result = statement.executeQuery();
while(result.next()) {
result.getInt(1); result.wasNull();
result.getInt(2); result.wasNull();
result.getString(3); result.wasNull();
result.getString(4); result.wasNull();
result.getString(5); result.wasNull();
result.getDouble(6); result.wasNull();
result.getInt(7); result.wasNull();
result.getInt(8); result.wasNull();
result.getInt(9); result.wasNull();
result.getString(10); result.wasNull();
result.getString(11); result.wasNull();
result.getString(12); result.wasNull();
result.getString(13); result.wasNull();
}
if (fast)
connection.commit();
if (cacheStatements)
complete();
statement.close();
// java.sql.DriverManager.setLogStream(null);
if (!cacheStatements)
complete();
record("jdbcFetchRows");
}
}
catch (Exception ex) {
fail(ex);
}
}
private final void insertRows() {
try {
for (int index = 0; index < batchCount; ++index) {
// Pass in starting key value for next batch.
//
generateData(batchSize*index);
// java.sql.DriverManager.setLogStream(System.out);
start();
if (fast) {
database.saveChanges(new DataSet[] {genDataSet}, false, true, noResetStatus);
connection.commit();
}
else
database.saveChanges(genDataSet);
// java.sql.DriverManager.setLogStream(null);
complete();
record("insertRows");
}
}
catch (Exception ex) {
fail(ex);
}
}
private final void deleteRows() {
try {
genDataSet.close();
genDataSet.setQuery(new QueryDescriptor(database, sqlQuery));
genDataSet.open();
for (int index = 0; index < batchCount; ++index) {
for (int row = 0; row < batchSize; ++row) {
genDataSet.deleteRow();
}
start();
// java.sql.DriverManager.setLogStream(System.out);
if (fast) {
database.saveChanges(new DataSet[] {genDataSet}, false, true, noResetStatus);
connection.commit();
}
else
database.saveChanges(genDataSet);
// java.sql.DriverManager.setLogStream(null);
complete();
// This takes the cost of updating status out of saveChanges call assuming
// that the genDataSet would not be edited after the call to saveChanges. If
// genDataSet were to be edited after the saveChanges, then resetChanges
// should be set to false so that this cost is included in the timing.
//
if (fast && !noResetStatus)
genDataSet.resetPendingStatus(true);
record("deleteRows");
}
}
catch (Exception ex) {
fail(ex);
}
}
private final void dataSetAccessOverhead() {
try {
start();
genDataSet.first();
while (genDataSet.inBounds()) {
genDataSet.getInt(0);
genDataSet.getInt(1);
genDataSet.getString(2);
genDataSet.getString(3);
genDataSet.getString(4);
genDataSet.getDouble(5);
genDataSet.getInt(6);
genDataSet.getInt(7);
genDataSet.getInt(8);
genDataSet.getString(9);
genDataSet.getString(10);
genDataSet.getString(11);
genDataSet.getString(12);
genDataSet.next();
}
complete();
record("dataSetAccessOverhead");
}
catch (Exception ex) {
fail(ex);
}
}
private final void jdbcInsertRows(int batchCount) {
try {
for (int index = 0; index < batchCount; ++index) {
// Pass in starting key value for next batch.
//
generateData(index*batchSize);
// java.sql.DriverManager.setLogStream(System.out);
if (!cacheStatements)
start();
String insertString = "INSERT INTO "+table+" (" +
int1+", "+
int2+", "+
string3+", "+
string4+", "+
string5+", "+
double6+", "+
int7+", "+
int8+", "+
int9+", "+
string10+", "+
string11+", "+
string12+", "+
string13+") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
PreparedStatement insertStatement = connection.prepareStatement(insertString);
if (cacheStatements)
start();
genDataSet.first();
while (genDataSet.inBounds()) {
insertStatement.setInt(1, genDataSet.getInt(0));
insertStatement.setInt(2, genDataSet.getInt(1));
insertStatement.setString(3, genDataSet.getString(2));
insertStatement.setString(4, genDataSet.getString(3));
insertStatement.setString(5, genDataSet.getString(4));
insertStatement.setDouble(6, genDataSet.getDouble(5));
insertStatement.setInt(7, genDataSet.getInt(6));
insertStatement.setInt(8, genDataSet.getInt(7));
insertStatement.setInt(9, genDataSet.getInt(8));
insertStatement.setString(10, genDataSet.getString(9));
insertStatement.setString(11, genDataSet.getString(10));
insertStatement.setString(12, genDataSet.getString(11));
insertStatement.setString(13, genDataSet.getString(12));
insertStatement.executeUpdate();
genDataSet.next();
}
if (fast)
connection.commit();
if (cacheStatements)
complete();
insertStatement.close();
if (!cacheStatements)
complete();
// java.sql.DriverManager.setLogStream(null);
record("jdbcInsertRows");
}
}
catch (Exception ex) {
fail(ex);
}
}
private final void jdbcUpdateRows() {
try {
genDataSet.close();
genDataSet.setQuery(new QueryDescriptor(database, sqlQuery));
genDataSet.open();
for (int index = 0; index < batchCount; ++index) {
genDataSet.goToRow(index*batchSize);
if (!cacheStatements)
start();
// java.sql.DriverManager.setLogStream(System.out);
// System.out.println("=============================================");
String updateString = "UPDATE "+table+" SET INT2 = ? WHERE " +
int1+" = ?";
if (!fast)
updateString += " AND "+
int2+" = ? AND "+
string3+" = ? AND "+
string4+" = ? AND "+
string5+" = ? AND "+
double6+" = ? AND "+
int7+" = ? AND "+
int8+" = ? AND "+
int9+" = ? AND "+
string10+" = ? AND "+
string11+" = ? AND "+
string12+" = ? AND "+
string13+" = ?";
PreparedStatement updateStatement = connection.prepareStatement(updateString);
if (cacheStatements)
start();
for (int row = 0; row < batchSize; ++row) {
// System.out.println("int1: "+genDataSet.getInt(0));
updateStatement.setInt(1, genDataSet.getInt(1)+5000);
updateStatement.setInt(2, genDataSet.getInt(0));
if (!fast) {
updateStatement.setInt(3, genDataSet.getInt(1));
updateStatement.setString(4, genDataSet.getString(2));
updateStatement.setString(5, genDataSet.getString(3));
updateStatement.setString(6, genDataSet.getString(4));
updateStatement.setDouble(7, genDataSet.getDouble(5));
updateStatement.setInt(8, genDataSet.getInt(6));
updateStatement.setInt(9, genDataSet.getInt(7));
updateStatement.setInt(10, genDataSet.getInt(8));
updateStatement.setString(11, genDataSet.getString(9));
updateStatement.setString(12, genDataSet.getString(10));
updateStatement.setString(13, genDataSet.getString(11));
updateStatement.setString(14, genDataSet.getString(12));
}
updateStatement.executeUpdate();
genDataSet.next();
}
if (fast)
connection.commit();
if (cacheStatements)
complete();
updateStatement.close();
if (!cacheStatements)
complete();
// java.sql.DriverManager.setLogStream(null);
record("jdbcUpdateRows");
}
}
catch (Exception ex) {
fail(ex);
}
}
private final void jdbcDeleteRows() {
try {
genDataSet.close();
genDataSet.setQuery(new QueryDescriptor(database, sqlQuery));
genDataSet.open();
for (int index = 0; index < batchCount; ++index) {
genDataSet.goToRow(index*batchSize);
if (!cacheStatements)
start();
String deleteString = "DELETE FROM "+table+" WHERE " +int1+" = ?";
if (!fast)
deleteString += " AND "+
int2+" = ? AND "+
string3+" = ? AND "+
string4+" = ? AND "+
string5+" = ? AND "+
double6+" = ? AND "+
int7+" = ? AND "+
int8+" = ? AND "+
int9+" = ? AND "+
string10+" = ? AND "+
string11+" = ? AND "+
string12+" = ? AND "+
string13+" = ?";
// System.out.println("deleteString: "+deleteString);
// java.sql.DriverManager.setLogStream(System.out);
PreparedStatement deleteStatement = connection.prepareStatement(deleteString);
if (cacheStatements)
start();
for (int row = 0; row < batchSize; ++row) {
deleteStatement.setInt(1, genDataSet.getInt(0));
if (!fast) {
deleteStatement.setInt(2, genDataSet.getInt(1));
deleteStatement.setString(3, genDataSet.getString(2));
deleteStatement.setString(4, genDataSet.getString(3));
deleteStatement.setString(5, genDataSet.getString(4));
deleteStatement.setDouble(6, genDataSet.getDouble(5));
deleteStatement.setInt(7, genDataSet.getInt(6));
deleteStatement.setInt(8, genDataSet.getInt(7));
deleteStatement.setInt(9, genDataSet.getInt(8));
deleteStatement.setString(10, genDataSet.getString(9));
deleteStatement.setString(11, genDataSet.getString(10));
deleteStatement.setString(12, genDataSet.getString(11));
deleteStatement.setString(13, genDataSet.getString(12));
}
deleteStatement.executeUpdate();
genDataSet.next();
}
if (fast)
connection.commit();
if (cacheStatements)
complete();
deleteStatement.close();
if (!cacheStatements)
complete();
// java.sql.DriverManager.setLogStream(null);
record("jdbcDeleteRows");
}
}
catch (Exception ex) {
fail(ex);
}
}
private final void updateRows() {
try {
genDataSet.close();
genDataSet.setQuery(new QueryDescriptor(database, sqlQuery));
genDataSet.open();
// System.out.println("updateRows: "+genDataSet.getRowCount());
for (int index = 0; index < batchCount; ++index) {
genDataSet.goToRow(index*batchSize);
for (int row = 0; row < batchSize; ++row) {
genDataSet.setInt(1, genDataSet.getInt(1)+5000);
genDataSet.next();
}
start();
if (fast) {
// java.sql.DriverManager.setLogStream(System.out);
database.saveChanges(new DataSet[] {genDataSet}, false, true, noResetStatus);
connection.commit();
// java.sql.DriverManager.setLogStream(null);
}
else {
database.saveChanges(genDataSet);
}
complete();
// This takes the cost of updating status out of saveChanges call assuming
// that the genDataSet would not be edited after the call to saveChanges. If
// genDataSet were to be edited after the saveChanges, then resetChanges
// should be set to false so that this cost is included in the timing.
//
if (fast && !noResetStatus)
genDataSet.resetPendingStatus(true);
record("updateRows");
}
}
catch (Exception ex) {
fail(ex);
}
}
// Pass in starting key value for next batch.
//
private final void generateData(int row) {
try {
if (useDataStore) {
if (store != null)
((DataStore)store).close();
DataStore dataStore = new DataStore();
dataStore.setFileName(tempDir+"/bench");
new File(tempDir+"/bench.jds").delete();
if (fast)
dataStore.setSaveMode(0);
dataStore.create();
store = dataStore;
}
genDataSet = new QueryDataSet();
genDataSet.setStore(store);
genDataSet.setStoreName(table);
genDataSet.setTableName(table);
if (fast) {
QueryResolver resolver = new QueryResolver();
resolver.setUpdateMode(UpdateMode.KEY_COLUMNS);
genDataSet.setResolver(resolver);
}
genDataSet.addColumn(int1, int1, Variant.INT);
genDataSet.getColumn(0).setRowId(true);
genDataSet.addColumn(int2, int2, Variant.INT);
genDataSet.addColumn(string3, string3, Variant.STRING);
genDataSet.addColumn(string4, string4, Variant.STRING);
genDataSet.addColumn(string5, string3, Variant.STRING);
genDataSet.addColumn(double6, double6, Variant.DOUBLE);
genDataSet.addColumn(int7, int7, Variant.INT);
genDataSet.addColumn(int8, int8, Variant.INT);
genDataSet.addColumn(int9, int9, Variant.INT);
genDataSet.addColumn(string10, string10, Variant.STRING);
genDataSet.addColumn(string11, string11, Variant.STRING);
genDataSet.addColumn(string12, string12, Variant.STRING);
genDataSet.addColumn(string13, string13, Variant.STRING);
genDataSet.open();
DataRow dataRow = new DataRow(genDataSet);
int lastRow = row + batchSize;
for (; row < lastRow; ++row) {
dataRow.setInt(int1, row);
dataRow.setInt(int2, makeInt(row));
dataRow.setString(string3, makeFirstName(row));
dataRow.setString(string4, makeLastName(row));
dataRow.setString(string5, makePhone(row));
dataRow.setDouble(double6, makeDouble(row));
dataRow.setInt(int7, makeInt(row));
dataRow.setInt(int8, makeInt(row));
dataRow.setInt(int9, makeInt(row));
dataRow.setString(string10, makePhone(row));
dataRow.setString(string11, makeLastName(row));
dataRow.setString(string12, makeFirstName(row));
dataRow.setString(string13, makePhone(row));
genDataSet.addRow(dataRow);
}
}
catch(Exception ex) {
ex.printStackTrace();
}
}
private final void init(JDBCDriver driver) {
try {
start();
// Pass in starting key value for next batch.
//
generateData(0);
// some SQL DDL can't be part of multi-statement transactions
//
database.setAutoCommit(true);
driver.createBenchTable(genDataSet);
if (fast) {
try {
database.setAutoCommit(false);
}
catch(Exception ex) {
fail(ex);
}
}
else {
try {
database.setAutoCommit(true);
}
catch(Exception ex) {
fail(ex);
}
}
timings = new TableDataSet();
timings.addColumn( "test" , Variant.STRING);
timings.addColumn( "time" , Variant.LONG);
timings.open();
complete();
record( "createTables" );
}
catch (Exception ex) {
fail(ex);
}
}
static String[] phones = {
"(408 ) 555 - 6598 ", "(415 ) 666 - 7893 ", "(234 ) 757 - 8923 ",
"(407 ) 655 - 6998 ", "(115 ) 664 - 78493 ", "(233 ) 750 - 8323 ",
"(407 ) 653 - 6998 ", "(115 ) 264 - 78493 ", "(233 ) 758 - 9323 ",
};
private final String makePhone(int row) {
int index = ((row % phones.length) % phones.length) + (row % phones.length);
if (index >= phones.length)
index -= phones.length;
return phones[index];
}
private final static int[] ints = { 0 , 0x7FFF, 0x7F, 0x7FFFffff, 0x8000, 0x80000000, 1 , - 1 };
private final int makeInt(int row) {
return ints[row%ints.length];
}
private final static String[] lastNames = { "Nelson" , "Young" , "Lambert" , "Johnson" , "Forest" , "Weston" ,
"Lee" , "Hall" , "Young" , "Papadopoulos" , "Fisher" , "Bennet" ,
"De Souza" , "Baldwin" , "Reeves" , "Stansbury" , "Phong" , "Ramanathan" ,
"Steadman" , "Nordstrom" , "Leung" , "O'Brien" , "Burbank" , "Sutherland" ,
"Bishop" , "MacDonald" , "Williams" , "Bender" , "Cook" , "Brown" ,
"Ichida" , "Page" , "Parker" , "Yamamoto" , "Ferrari" , "Yanowski" ,
"Glon" , "Johnson" , "Green" , "Osborne" , "Montgomery" , "Guckenheimer" ,
// This one is useful for partial locate tests becuase it
// starts with another Montgomery entry above.
"Mont"
};
private final String makeLastName(int row) {
return lastNames[row % lastNames.length];
}
static String[] firstNames = { "Robert" , "Bruce" , "Kim" , "Leslie" , "Phil" , "K. J." ,
"Terri" , "Stewart" , "Katherine" , "Chris" , "Pete" , "Ann" ,
"Roger" , "Janet" , "Roger" , "Willie" , "Leslie" , "Ashok" ,
"Walter" , "Carol" , "Luke" , "Sue Anne" , "Jennifer M." , "Claudia" ,
"Dana" , "Mary S." , "Randy" , "Oliver H." , "Kevin" , "Kelly" ,
"Yuki" , "Mary" , "Bill" , "Takashi" , "Roberto" , "Michael" ,
"Jacques" , "Scott" , "T.J." , "Pierre" , "John" , "Mark" ,
};
private final String makeFirstName(int row) {
int index = ((row / firstNames.length) % firstNames.length) + (row % firstNames.length);
if (index >= firstNames.length)
index -= firstNames.length;
return firstNames[index];
}
private final static double[] doubles = { 0 . 0 , 55 . 78 , 99 . 99 , 143 . 30 , 166 . 29 , 32 . 08 , 864 . 01 };
private final double makeDouble(int row) {
return row * (doubles[row % doubles.length]);
}
private long allocatedMemory() {
Runtime runTime = Runtime.getRuntime();
return runTime.totalMemory() - runTime.freeMemory();
}
private void start() {
duration = 0 ;
memoryConsumed = 0 ;
success = false;
if (garbageCollect) {
System.gc();
System.runFinalization();
}
// startMemory = allocatedMemory();
startTime = System.currentTimeMillis();
}
private void complete() {
duration = System.currentTimeMillis() - startTime;
if (garbageCollect) {
System.gc();
System.runFinalization();
}
// memoryConsumed = allocatedMemory() - startMemory;
}
private boolean succeeded() { return success; }
private long getDuration() { return duration; }
private long getMemoryConsumed() { return memoryConsumed; }
private void record(String message)
throws Exception
{
timings.insertRow(true);
timings.setString( 0 , message);
timings.setLong( 1 , getDuration());
if (sleepInterval != 0 )
Thread.currentThread().sleep(sleepInterval);
}
private final void sleep(long interval) {
try {
Thread.currentThread().sleep(interval);
}
catch(Exception ex) {
fail(ex);
}
}
private final void fail(Exception ex) {
ex.printStackTrace();
System.exit( 1 );
}
private void reportProperties(JDBCDriver driver, String[] args) {
String argStr = "";
if (args != null) {
for (int i = 0 ; i < args.length; i++)
argStr = argStr + args[i] + " ";
}
System.out.println(" Command Line Args: " + argStr);
System.out.println(" \nConnection Informaiton ");
System.out.println(" Url: " + driver.getUrl());
System.out.println(" User: " + driver.getUser());
System.out.println(" Pwd: " + driver.getPassword());
System.out.println(" Driver: " + driver.getDriver());
System.out.println(" \nTest Settings ");
System.out.println(" batchSize: " + batchSize);
System.out.println(" batchCount: " + batchCount);
System.out.println(" fast: " + fast);
System.out.println(" cacheStatements: " + cacheStatements);
System.out.println(" garbageCollect: " + garbageCollect);
System.out.println(" verbose: " + verbose);
System.out.println(" useDataStore: " + useDataStore);
System.out.println(" sleepInterval: " + sleepInterval);
System.out.println(" noResetStatus: " + noResetStatus);
}
private void report()
{
try {
if (timings.getRowCount() > 0 ) {
timings.first();
String message = " ";
int count = 0 ;
double total = 0 ;
double min = - 1 ;
double max = 0 ;
while (timings.inBounds()) {
if (!message.equals(timings.getString( 0 ))) {
if (count > 1 )
System.out.println("Min/Max/Average after first run for "+message+" : "+min+" "+max+" "+(total/count));
System.out.println(" ");
message = timings.getString(0 );
System.out.println("First run for "+message+" : "+timings.getLong(1 ));
min = - 1 ;
max = 0 ;
count = 0 ;
total = 0 ;
}
else {
if (verbose)
System.out.println("Secondary run for "+message+" : "+timings.getLong(1 ));
++count;
double t = (double) timings.getLong( 1 );
total += t;
if (t > max)
max = t;
if (t < min || min < 0 )
min = t;
}
timings.next();
}
if (count > 1 )
System.out.println("Min/Max/Average after first run for "+message+" : "+min+" "+max+" "+(total/count));
timings.empty();
}
}
catch(Exception ex) {
ex.printStackTrace();
System.exit(1 );
}
}
public static String getTempDir() {
String homePath = System.getProperties().getProperty("user.home ") + File.separator + " benchmark ";
File directory = new File(homePath);
if (!directory.exists()) {
directory.mkdir();
}
return homePath;
}
private long startTime;
private long startMemory;
private long duration;
private long memoryConsumed;
private boolean success;
static final String table = " JDBCBENCHTABLE ";
static final String sqlQuery = " select * from "+table;
static final String int1 = " INT1 ";
static final String int2 = " INT2 ";
static final String string3 = " STRING3 ";
static final String string4 = " STRING4 ";
static final String string5 = " STRING5 ";
static final String double6 = " DOUBLE6 ";
static final String int7 = " INT7 ";
static final String int8 = " INT8 ";
static final String int9 = " INT9 ";
static final String string10 = " STRING10 ";
static final String string11 = " STRING11 ";
static final String string12 = " STRING12 ";
static final String string13 = " STRING13 ";
private TableDataSet timings;
private int batchSize;
private int batchCount;
private boolean cacheStatements;
private boolean fast;
private boolean noResetStatus;
private boolean garbageCollect;
private boolean verbose;
private boolean useDataStore;
private int sleepInterval;
private Store store;
private Database database;
private Connection connection;
private QueryDataSet genDataSet;
}
// Base implementation that defaults to JDataStore.
//
class JDBCDriver {
static String tempDir = JDBCBenchMark.tempDir;
void connect() {
try {
database = new Database();
database.setConnection(new ConnectionDescriptor(
getUrl(), getUser(), getPassword(),
false, getDriver() ));
connection = database.getJdbcConnection();
}
catch(DataSetException ex) {
ex.printStackTrace();
System.exit(1 );
}
}
void createBenchTable(StorageDataSet dataSet) throws Exception {
deleteTable(dataSet);
createTable(dataSet);
createIndex(dataSet);
}
void deleteTable(StorageDataSet dataSet) throws Exception {
MetaData metadata = MetaData.getMetaData(database);
try { metadata.dropTable(dataSet.getTableName());}
catch(Exception ex) {
ex.printStackTrace();
database.rollback();
// just ignore.
// throw ex;
}
}
void createTable(StorageDataSet dataSet) throws Exception {
MetaData metadata = MetaData.getMetaData(database);
metadata.createTable(dataSet.getTableName(), dataSet);
}
void createIndex(StorageDataSet dataSet) throws Exception {
String query = "create unique index " + dataSet.getTableName() + " 1 on " + dataSet.getTableName() + " ( " + JDBCBenchMark.int1 + " ) ";
database.executeStatement(query);
}
String getUrl() { return " jdbc:odbc:DataSet Tutorial "; }
String getUser() { return " sysdba "; }
String getPassword() { return " masterkey "; }
String getDriver() { return " sun.jdbc.odbc.JdbcOdbcDriver "; }
Database database;
Connection connection;
}
class MSSQLDriver extends JDBCDriver {
void createTable(StorageDataSet dataSet) throws Exception {
MetaData metadata = MetaData.getMetaData(database);
try {
Statement stmt;
System.out.println(" Creating table ");
stmt = connection.createStatement();
stmt.execute(" CREATE TABLE " + dataSet.getTableName() + " ( " +
JDBCBenchMark.int1 + " INT NOT NULL, " +
JDBCBenchMark.int2 + " INT NOT NULL, " +
JDBCBenchMark.string3 + " CHAR( 30 ) NOT NULL, " +
JDBCBenchMark.string4 + " CHAR( 30 ) NOT NULL, " +
JDBCBenchMark.string5 + " CHAR( 30 ) NOT NULL, " +
JDBCBenchMark.double6 + " FLOAT NOT NULL, " +
JDBCBenchMark.int7 + " INT NOT NULL, " +
JDBCBenchMark.int8 + " INT NOT NULL, " +
JDBCBenchMark.int9 + " INT NOT NULL, " +
JDBCBenchMark.string10 + " CHAR( 30 ) NOT NULL, " +
JDBCBenchMark.string11 + " CHAR( 30 ) NOT NULL, " +
JDBCBenchMark.string12 + " CHAR( 30 ) NOT NULL, " +
JDBCBenchMark.string13 + " CHAR( 30 ) NOT NULL) ");
connection.commit();
stmt.close();
}
catch(Exception ex) {
ex.printStackTrace();
throw ex;
}
}
String getUrl() { return " jdbc:ff-microsoft://localhost: 1433 /SRS "; }
String getUser() { return " sa "; }
String getPassword() { return " letmein "; }
String getDriver() { return " connect.microsoft.MicrosoftDriver "; }
}
class DB2Driver extends JDBCDriver {
void createTable(StorageDataSet dataSet) throws Exception {
MetaData metadata = MetaData.getMetaData(database);
try {
Statement stmt;
System.out.println(" Creating table ");
stmt = connection.createStatement();
stmt.execute(" CREATE TABLE " + dataSet.getTableName() + " ( " +
JDBCBenchMark.int1 + " INT NOT NULL, " +
JDBCBenchMark.int2 + " INT NOT NULL, " +
JDBCBenchMark.string3 + " CHAR( 30 ) NOT NULL, " +
JDBCBenchMark.string4 + " CHAR( 30 ) NOT NULL, " +
JDBCBenchMark.string5 + " CHAR( 30 ) NOT NULL, " +
JDBCBenchMark.double6 + " FLOAT NOT NULL, " +
JDBCBenchMark.int7 + " INT NOT NULL, " +
JDBCBenchMark.int8 + " INT NOT NULL, " +
JDBCBenchMark.int9 + " INT NOT NULL, " +
JDBCBenchMark.string10 + " CHAR( 30 ) NOT NULL, " +
JDBCBenchMark.string11 + " CHAR( 30 ) NOT NULL, " +
JDBCBenchMark.string12 + " CHAR( 30 ) NOT NULL, " +
JDBCBenchMark.string13 + " CHAR( 30 ) NOT NULL) ");
connection.commit();
stmt.close();
}
catch(Exception ex) {
ex.printStackTrace();
throw ex;
}
}
String getUrl() { return " jdbc:db2:sample "; }
String getUser() { return " db2admin "; }
String getPassword() { return " jamesf "; }
String getDriver() { return " COM.ibm.db2.jdbc.app.DB2Driver "; }
}
class SybaseSQLDriver extends JDBCDriver {
void createTable(StorageDataSet dataSet) throws Exception {
MetaData metadata = MetaData.getMetaData(database);
try {
Statement stmt;
System.out.println(" Creating table ");
stmt = connection.createStatement();
stmt.execute(" CREATE TABLE " + dataSet.getTableName() + " ( " +
JDBCBenchMark.int1 + " INT NOT NULL, " +
JDBCBenchMark.int2 + " INT NOT NULL, " +
JDBCBenchMark.string3 + " CHAR( 30 ) NOT NULL, " +
JDBCBenchMark.string4 + " CHAR( 30 ) NOT NULL, " +
JDBCBenchMark.string5 + " CHAR( 30 ) NOT NULL, " +
JDBCBenchMark.double6 + " FLOAT NOT NULL, " +
JDBCBenchMark.int7 + " INT NOT NULL, " +
JDBCBenchMark.int8 + " INT NOT NULL, " +
JDBCBenchMark.int9 + " INT NOT NULL, " +
JDBCBenchMark.string10 + " CHAR( 30 ) NOT NULL, " +
JDBCBenchMark.string11 + " CHAR( 30 ) NOT NULL, " +
JDBCBenchMark.string12 + " CHAR( 30 ) NOT NULL, " +
JDBCBenchMark.string13 + " CHAR( 30 ) NOT NULL) ");
connection.commit();
stmt.close();
}
catch(Exception ex) {
ex.printStackTrace();
throw ex;
}
}
String getUrl() { return " jdbc:ff-sybase://STEROID: 5000 /testdb "; }
String getUser() { return " sa "; }
String getPassword() { return " jamesf "; }
String getDriver() { return " connect.sybase.SybaseDriver "; }
}
class DataStoreDriver extends JDBCDriver {
void connect() {
DataStore dataStore = new DataStore();
try {
TxManager manager = new TxManager();
// manager.setSoftRecovery(false);
dataStore.setTxManager(manager);
dataStore.setUserName(" foo ");
dataStore.setFileName(tempDir+" /benchDataStore.jds ");
dataStore.create();
dataStore.close();
}
catch(DataSetException ex) {
}
super.connect();
}
void createTable(StorageDataSet dataSet) throws Exception {
try {
Statement stmt;
System.out.println(" Creating table ");
stmt = connection.createStatement();
stmt.execute(" CREATE TABLE " + dataSet.getTableName() + " ( " +
JDBCBenchMark.int1 + " INT , " +
JDBCBenchMark.int2 + " INT , " +
JDBCBenchMark.string3 + " CHAR( 30 ) , " +
JDBCBenchMark.string4 + " CHAR( 30 ) , " +
JDBCBenchMark.string5 + " CHAR( 30 ) , " +
JDBCBenchMark.double6 + " DOUBLE , " +
JDBCBenchMark.int7 + " INT , " +
JDBCBenchMark.int8 + " INT , " +
JDBCBenchMark.int9 + " INT , " +
JDBCBenchMark.string10 + " CHAR( 30 ) , " +
JDBCBenchMark.string11 + " CHAR( 30 ) , " +
JDBCBenchMark.string12 + " CHAR( 30 ) , " +
JDBCBenchMark.string13 + " CHAR( 30 ) ) ");
connection.commit();
stmt.close();
}
catch(Exception ex) {
ex.printStackTrace();
throw ex;
}
}
void createBenchTable(StorageDataSet dataSet) throws Exception {
deleteTable(dataSet);
createTable(dataSet);
createIndex(dataSet);
}
void deleteTable(StorageDataSet dataSet) throws Exception {
MetaData metadata = MetaData.getMetaData(database);
try { metadata.dropTable(dataSet.getTableName());}
catch(Exception ex) {
ex.printStackTrace();
database.rollback();
// just ignore.
// throw ex;
}
}
String getUrl() { return " jdbc:borland:dslocal: "+tempDir+" /benchDataStore.jds "; }
String getUser() { return " Frank "; }
String getPassword() { return " "; }
String getDriver() { return " com.borland.datastore.jdbc.DataStoreDriver"; }
}