-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
215 lines (168 loc) · 6.03 KB
/
Client.java
File metadata and controls
215 lines (168 loc) · 6.03 KB
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
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.io.*;
import java.util.*;
public class Client {
static void writeCardsToDB(Connection conn, int numberOfCards)
{
long cardnumber_start = 1111222233330000L;
long cardnumber = cardnumber_start;
int[] daily = {100, 200, 500, 1000, 2000, 5000, 10000, 20000, 100000, 1000000};
int[] monthly = {1000, 2000, 5000, 10000, 20000, 50000, 100000, 200000, 1000000, 10000000};
int number_rnd10 = 0;
int number_rndfn = 0;
int number_rndln = 0;
List<String> firstn = readNamesFromFile("baby-names.csv");
List<String> lastn = readNamesFromFile("surnames.csv");
for (int i = 0; i < numberOfCards; i++){
if ( i % 100 == 0 ) {
System.out.println("Number of Cards: \t" + i);
}
number_rnd10 = (int) (Math.random()*10); //random number from 0 till 9
number_rndfn = (int) (Math.random()*firstn.size()); //random number for namearray
number_rndln = (int) (Math.random()*lastn.size()); //random number for namearray
try {
Statement stmt = conn.createStatement();
stmt.execute("SELECT insert_card(CAST ("+
cardnumber++ +" AS bigint), CAST ("+
daily[number_rnd10] +" AS numeric), CAST ( "+
monthly[number_rnd10] +" AS numeric), "+ false +
", CAST("+ 5 +" AS SMALLINT), CAST( '"
+firstn.get(number_rndfn)+
" "+ lastn.get(number_rndln) + "' AS text));");
} catch (SQLException e) {
System.out.println("Error while executing Stored Procedure insert_card");
e.printStackTrace();
return;
}
}
}
static void writeCountriesToDB(Connection conn)
{
//Load Countries from file
String zeile = "";
String[] parts = new String[4];
try {
FileReader fr = new FileReader("country_names_and_code_elements_txt.txt");
BufferedReader br = new BufferedReader(fr);
Boolean ok;
Statement stmt = conn.createStatement();
while( (zeile = br.readLine()) != null )
{
parts = zeile.split(";");
//TODO
//myApp.callProcedure("Insert_country", parts[1],parts[0],"");
//myApp.callProcedure("Insert_country_specific", parts[1], parts[2],parts[3]);
stmt.execute("SELECT insert_country(CAST ('"+
parts[1] +"' AS text), CAST ('"+
parts[0] +"' AS text), CAST(' ' AS text));");
stmt.execute("SELECT insert_country_specific(CAST ('"+
parts[1] +"' AS text), CAST ("+
(ok = Integer.parseInt(parts[2]) == 1 ? true : false) +" AS BOOLEAN), CAST('"+
parts[3] +"' AS numeric));");
}
br.close();
fr.close();
} catch (IOException e) {
System.out.println("Dateilesefehler");
e.printStackTrace();
return;
} catch (SQLException e) {
System.out.println("Error while executing Stored Procedure insert_country or insert country specific");
e.printStackTrace();
return;
}
}
static List<String> readNamesFromFile(String dateiname)
{
List<String> names = new ArrayList<String>();
String zeile = "";
try {
FileReader fr = new FileReader(dateiname);
BufferedReader br = new BufferedReader(fr);
while( (zeile = br.readLine()) != null )
{
names.add(zeile);
}
br.close();
fr.close();
} catch (IOException e) {
System.out.println("Dateilesefehler");
e.printStackTrace();
System.exit(-1);
}
return names;
}
static void transfers(Connection conn, int numberOfTransactions) {
int numberOfCards = 100000; // 10 Mio dauert ca. eine Stunde
long cardnumber_start = 1111222233330000L;
long number_rndcard = 0;
long amount = 0;
String country_code = "DE";
double lat_neu = 48.4;
double long_neu = 13.9;
long rnd_cardnumber = 0L;
for (int k = 0; k < numberOfTransactions; k++) {
amount = (int) (Math.random()*10);
rnd_cardnumber = cardnumber_start+((int) (Math.random()*numberOfCards));
if ( k % 1000 == 0 ) {
System.out.println("Transfer-number: " + k);
}
String zweck = "TEST " + k;
try {
Statement stmt = conn.createStatement();
stmt.execute("SELECT new_transfer(CAST ("+
rnd_cardnumber +" AS bigint), CAST ("+
amount +" AS numeric), CAST ( "+
48.2 +" AS double precision), CAST ("+
13.0 +" AS double precision), CAST("+ "'DE'" +" AS text), CAST( '"+
zweck + "' AS text));");
} catch (SQLException e) {
System.out.println("Error while executing Stored Procedure new_transfer");
e.printStackTrace();
return;
}
}
}
public static void main(String[] argv) {
System.out.println("-------- PostgreSQL "
+ "JDBC Connection Testing ------------");
Connection conn = null;
try {
conn = DriverManager.getConnection(
"jdbc:postgresql://127.0.0.1:5432/postgres", "postgres",
"test");
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return;
}
if (conn != null) {
System.out.println("Connection OK.");
} else {
System.out.println("Failed to make connection!");
}
int numberOfTransfers = 1000000;
int numberOfCards = 100000; // 100.000 dauert ca. 15 min auf hdd und 1min 30sek auf ssd
long startTime = System.nanoTime();
writeCardsToDB(conn, numberOfCards);
long endTime = System.nanoTime();
double duration = (endTime - startTime)/1000000000.0; //in Sekunden
System.out.println("Karten hinzufügen hat: "+ duration+" Sekunden gedauert.");
System.out.println("Das sind: "+ numberOfCards/duration +" Karten pro Sekunde.");
startTime = System.nanoTime();
writeCountriesToDB(conn);
endTime = System.nanoTime();
duration = (endTime - startTime)/1000000000.0;
System.out.println("Länder hinzufügen hat: "+ duration+" Sekunden gedauert.");
startTime = System.nanoTime();
transfers(conn, numberOfTransfers);
endTime = System.nanoTime();
duration = (endTime - startTime)/1000000000.0;
System.out.println("Transfers haben "+ duration+" Sekunden gedauert.");
System.out.println("Das sind: "+ numberOfTransfers/duration +" Transfers pro Sekunde.");
System.out.printf("Datenbank vorbereitet \n\n");
}
}