aboutsummaryrefslogtreecommitdiffstats
path: root/mdbc-server/src/test/java/org/openecomp/sdnc/sli/resource/dblib/StressTest.java
blob: d6b5717dedfbf71a4b3bfe7a836aff81b2fcd260 (plain)
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
/*
 * ============LICENSE_START====================================================
 * org.onap.music.mdbc
 * =============================================================================
 * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
 * =============================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END======================================================
 */
package org.openecomp.sdnc.sli.resource.dblib;

import static org.junit.Assert.*;

import java.net.URL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;

import javax.sql.rowset.CachedRowSet;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.anarsoft.vmlens.concurrent.junit.ConcurrentTestRunner;
import com.anarsoft.vmlens.concurrent.junit.ThreadCount;

//@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@RunWith(ConcurrentTestRunner.class)
public class StressTest {

//	static {
//		System.setProperty(org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "INFO");
//		System.setProperty(org.slf4j.impl.SimpleLogger.LOG_FILE_KEY, String.format("ComparativeAnalysisTest-%d.log", System.currentTimeMillis()));
//	}
	private static final Logger LOG = LoggerFactory.getLogger(StressTest.class);
	private static Properties props;
	private static DBResourceManager jdbcDataSource = null;
	@SuppressWarnings("unused")
	private static final int MAX_TREADS = 1;
	@SuppressWarnings("unused")
	private static final int MAX_ITERATIONS = 10;
	
	private final AtomicInteger  count= new AtomicInteger();

	Set<Thread> runningThreads = new HashSet<Thread>();

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		props = new Properties();
		URL url = StressTest.class.getResource("/dblib.properties");
		assertNotNull(url);
		LOG.info("Property file is: " + url.toString());
		props.load(url.openStream());

		try {
			jdbcDataSource = DBResourceManager.create(props);
			Connection conn = jdbcDataSource.getConnection();

			// ---------------
			// CREATE TABLE
			String sql =
				"CREATE TABLE IF NOT EXISTS `AIC_SITE` (" + 
					"`name` varchar(100) DEFAULT NULL, "+
					"`aic_site_id` varchar(100) NOT NULL, "+
					"`vcenter_url` varchar(200) DEFAULT NULL, "+
					"`vcenter_username` varchar(40) DEFAULT NULL, "+
					"`vcenter_passwd` varchar(255) DEFAULT NULL, "+
					"`city` varchar(100) DEFAULT NULL, "+
					"`state` varchar(2) DEFAULT NULL, "+
					"`operational_status` varchar(20) DEFAULT NULL, "+
					"`oam_gateway_addr` varchar(20) DEFAULT '', "+
					"PRIMARY KEY (`aic_site_id`) "+
				") ; ";
			Statement stmt = conn.createStatement();
			stmt.execute(sql);
			// ---------------

			conn.close();
		} catch (Throwable exc) {
			LOG.error("", exc);
		}
		assertNotNull(jdbcDataSource);
		if (((DBResourceManager)jdbcDataSource).isActive()){
			LOG.warn( "DBLIB: JDBC DataSource has been initialized.");
		} else {
			LOG.warn( "DBLIB: JDBC DataSource did not initialize successfully.");
		}
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
		jdbcDataSource.cleanUp();
	}

	@Before
	public void setUp() throws Exception {

	}

	@After
	public void tearDown() throws Exception {

	}

//	@Test
	public void test01() {
		LOG.info("TEST 1: Verify primary db selection");
		checkPrimaryDatabase();
	}
	
	
	@Test
	@ThreadCount(10)
	public void test0X() {
		int id = count.incrementAndGet();
		
		String siteid = String.format("Councurrent-tester-%02d", id);
		for(int i=0; i<40; i++){
			String site = String.format("%s_%04d", siteid, i);
			insertTestData(site);
			queryTestData(site);
			removeTestData(site);
			try {
				Thread.sleep(0);
			} catch (Exception e) {
				LOG.warn("", e);
			}			
		}
	}

	private void removeTestData(String site) {
		ArrayList<String> delete = new ArrayList<String>();
		delete.add(site);
		try {
			long startTime = System.currentTimeMillis();
			boolean success = jdbcDataSource.writeData("delete from AIC_SITE where aic_site_id=?", delete, null);
			logRequest(site, "DELETE", startTime, System.currentTimeMillis() - startTime);
			assertTrue(success);
		} catch (SQLException e) {
			LOG.warn("", e);
			
		}
	}

	private boolean queryTestData(String site) {
		ArrayList<String> identifier = new ArrayList<String>();
		identifier.add(site);
		try {
			int rowcount = 0;
			long startTime = System.currentTimeMillis();
			CachedRowSet data = jdbcDataSource.getData("select * from AIC_SITE where aic_site_id=?", identifier, null);
			logRequest(site, "QUERY", startTime, System.currentTimeMillis() - startTime);
			while(data.next()) {
				rowcount ++;
			}
			return rowcount!=0;
//			assertTrue(success);
		} catch (SQLException e) {
			LOG.warn("", e);
			return false;
		}
	}


	private void insertTestData(String site) {
		ArrayList<String> data = new ArrayList<String>();
		data.add(site);
		data.add(site);
		data.add("Sample03");
		data.add("Sample04");
		data.add("Sample05");

		boolean success;
		try {
			long startTime = System.currentTimeMillis();
			success = jdbcDataSource.writeData("insert into AIC_SITE (name, aic_site_id, vcenter_url, vcenter_username, vcenter_passwd) values (?,?,?,?,?)", data, null);
			logRequest(site, "INSERT", startTime, System.currentTimeMillis() - startTime);
			assertTrue(success);
		} catch (SQLException e) {
			LOG.warn("", e);
		}
	}

	private void checkPrimaryDatabase() {
		Connection conn = null;
		PreparedStatement statement = null;
		ResultSet rs  = null;
		
		try {
			conn = jdbcDataSource.getConnection();
			statement = conn.prepareStatement("SELECT 1 FROM DUAL");
			rs = statement.executeQuery();
			int value = -1;
			while(rs.next()) {
				value = rs.getInt(1);
			}
			LOG.info("Value returned is: " + value);
			conn.close();
		} catch (SQLException e) {
			LOG.warn("transaction failed", e);
		} finally {
			try {
				if(rs != null) 	{ rs.close();	} 
				if(conn != null){ conn.close();	}
				if(conn != null){ conn.close();	}
			} catch (SQLException e) {
				LOG.warn("transaction failed", e);
			}
		}
		CachedDataSource ds = null;
		try {
			ds = jdbcDataSource.findMaster();
		} catch (Throwable e) {
			LOG.warn("", e);
		} 
		LOG.info("Primary DS is " + ds.getDbConnectionName());
	}
	private static void logRequest(String site, String command, long timestamp, long duration) {
		LOG.info(String.format("%s|%s|%d|%d", site, command, timestamp, duration));
	}	
}