-
Notifications
You must be signed in to change notification settings - Fork 3
/
DFSWritingWithMultiThread.rs
174 lines (150 loc) · 4.25 KB
/
DFSWritingWithMultiThread.rs
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
extern crate ddb;
extern crate libc;
use crate::ddb::*;
use libc::{c_int, c_longlong, c_uchar};
use std::str;
use std::time::Instant;
use std::panic;
use std::thread;
static HOSTS: [&str; 10] = [
"127.0.0.1",
"127.0.0.1",
"127.0.0.1",
"127.0.0.1",
"127.0.0.1",
"127.0.0.1",
"127.0.0.1",
"127.0.0.1",
"127.0.0.1",
"127.0.0.1",
];
static PORTS: [c_int; 10] = [8848, 8848, 8848, 8848, 8848,8848, 8848, 8848, 8848, 8848];
static USER: &str = "admin";
static PASS: &str = "123456";
fn create_demo_table(
rows: c_int,
startp: c_uchar,
pcount: c_uchar,
starttime: c_int,
time_inc: c_int,
) -> Table {
let colnames: [&str; 11] = [
"fwname",
"filename",
"source_address",
"source_port",
"destination_address",
"destination_port",
"nat_source_address",
"nat_source_port",
"starttime",
"stoptime",
"elapsed_time",
];
let coltypes: [c_int; 11] = [
DT_SYMBOL,
DT_STRING,
DT_IP,
DT_INT,
DT_IP,
DT_INT,
DT_IP,
DT_INT,
DT_DATETIME,
DT_DATETIME,
DT_INT,
];
let colnum = 11;
let table = create_table(&colnames[..], &coltypes[..], rows, rows);
let mut colv: Vec<ddb::Vector> = Vec::new();
for i in 0..(colnum) {
colv.push(table.get_column(i));
}
let mut sip: [c_uchar; 16] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
let ip: [c_uchar; 16] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
sip[3] = 192;
sip[2] = startp;
sip[1] = pcount;
let spip = create_constant(DT_IP);
for j in 1..255 {
sip[0] = j as c_uchar;
spip.set_binary(&sip[..]);
let x = spip.get_hash(50) as c_uchar;
if x >= startp && x < startp + pcount {
break;
}
}
for i in 0..rows {
colv[0].set_string_by_index(i, "10.189.45.2:9000");
colv[1].set_string_by_index(i, &startp.to_string());
colv[2].set_binary_by_index(i, &sip[..]);
colv[3].set_int_by_index(i, i as c_int);
colv[4].set_binary_by_index(i, &ip[..]);
colv[5].set_int_by_index(i, 2 * i as c_int);
colv[6].set_by_index(i, parse_constant(DT_IP, "192.168.1.1"));
colv[7].set_int_by_index(i, 3 * i as c_int);
colv[8].set_long_by_index(i, (starttime + time_inc) as c_longlong);
colv[9].set_long_by_index(i, (starttime + 100) as c_longlong);
colv[10].set_int_by_index(i, i as c_int);
}
return table;
}
fn finsert(
rows: c_int,
startp: c_uchar,
pcount: c_uchar,
starttime: c_int,
time_inc: c_int,
p: c_int,
inserttimes: c_int,
) {
let stime = Instant::now();
let conn = DBConnection::new();
let success = conn.connect(&HOSTS[p as usize], PORTS[p as usize], USER, PASS);
if !success {
panic!("connect failed");
}
let t = create_demo_table(rows, startp, pcount, starttime, time_inc);
let args: [Constant; 1] = [t.to_constant()];
for _i in 0..inserttimes {
conn.run_func(
"tableInsert{loadTable('dfs://natlog', `natlogrecords)}",
&args[..],
);
}
println!(
"Inserted {} rows {} times used {} ms",
rows,
inserttimes,
stime.elapsed().as_millis()
);
}
fn main() {
let thread_count = 10;
let mut thread_handlers = Vec::new();
let tablerows = 100000;
let inserttimes = 100;
let stime = Instant::now();
for i in 0..thread_count {
println!("thread {} started to create.",i);
thread_handlers.push(thread::spawn(move || {
finsert(
tablerows,
(i * 5 ) as c_uchar,
(5) as c_uchar,
(get_epoch_time() / 1000) as c_int,
(i * 5) as c_int,
i as c_int,
inserttimes as c_int,
);
}))
}
for handler in thread_handlers {
handler.join().map_err(|err| println!("{:?}", err)).ok();
}
let rows :u128 =tablerows as u128 * inserttimes * thread_count;
println!(
"Inserted {} rows, took a total of {} ms,{} records per second ",
rows,stime.elapsed().as_millis(),rows/stime.elapsed().as_millis() * 1000
);
}