2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (c) 2017 AT&T Intellectual Property
6 * ===================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * ============LICENSE_END=============================================
20 * ====================================================================
22 package org.onap.music.benchmarks;
24 import java.util.HashMap;
26 import javax.ws.rs.core.MediaType;
27 import org.onap.music.datastore.jsonobjects.JsonInsert;
28 import org.onap.music.datastore.jsonobjects.JsonKeySpace;
29 import org.onap.music.datastore.jsonobjects.JsonTable;
30 import org.onap.music.main.MusicUtil;
31 import com.sun.jersey.api.client.Client;
32 import com.sun.jersey.api.client.ClientResponse;
33 import com.sun.jersey.api.client.WebResource;
34 import com.sun.jersey.api.client.config.ClientConfig;
35 import com.sun.jersey.api.client.config.DefaultClientConfig;
36 import com.sun.jersey.api.json.JSONConfiguration;
38 public class MicroBenchMarks {
39 final String keyspaceName = "shankarks";
40 final String musicurl = "http://" + MusicUtil.getMusicRestIp() + ":8080/MUSIC/rest/formal";
41 final String userForGets = "shankarUserForGets";
43 public MicroBenchMarks() {
47 private void createVotingKeyspace() {
48 System.out.println(keyspaceName);
49 Map<String, Object> replicationInfo = new HashMap<String, Object>();
50 replicationInfo.put("class", "SimpleStrategy");
51 replicationInfo.put("replication_factor", 3);
52 String durabilityOfWrites = "false";
53 Map<String, String> consistencyInfo = new HashMap<String, String>();
54 consistencyInfo.put("type", "eventual");
55 JsonKeySpace jsonKp = new JsonKeySpace();
56 jsonKp.setConsistencyInfo(consistencyInfo);
57 jsonKp.setDurabilityOfWrites(durabilityOfWrites);
58 jsonKp.setReplicationInfo(replicationInfo);
60 ClientConfig clientConfig = new DefaultClientConfig();
62 clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
64 Client client = Client.create(clientConfig);
66 WebResource webResource = client.resource(musicurl + "/keyspaces/" + keyspaceName);
68 ClientResponse response = webResource.accept("application/json").type("application/json")
69 .post(ClientResponse.class, jsonKp);
71 if (response.getStatus() < 200 || response.getStatus() > 299)
72 throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
76 private void createVotingTable() {
77 Map<String, String> fields = new HashMap<String, String>();
78 fields.put("name", "text");
79 fields.put("count", "varint");
80 fields.put("PRIMARY KEY", "(name)");
83 Map<String, String> consistencyInfo = new HashMap<String, String>();
84 consistencyInfo.put("type", "eventual");
86 JsonTable jtab = new JsonTable();
87 jtab.setFields(fields);
88 jtab.setConsistencyInfo(consistencyInfo);
90 ClientConfig clientConfig = new DefaultClientConfig();
92 clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
94 Client client = Client.create(clientConfig);
96 WebResource webResource = client
97 .resource(musicurl + "/keyspaces/" + keyspaceName + "/tables/votecount");
99 ClientResponse response = webResource.accept("application/json").type("application/json")
100 .post(ClientResponse.class, jtab);
102 if (response.getStatus() < 200 || response.getStatus() > 299)
103 throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
107 private void createEntryForCandidate(String candidateName) {
108 Map<String, Object> values = new HashMap<String, Object>();
109 values.put("name", candidateName);
110 values.put("count", 0);
112 Map<String, String> consistencyInfo = new HashMap<String, String>();
113 consistencyInfo.put("type", "eventual");
115 JsonInsert jIns = new JsonInsert();
116 jIns.setValues(values);
117 jIns.setConsistencyInfo(consistencyInfo);
118 ClientConfig clientConfig = new DefaultClientConfig();
120 clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
122 Client client = Client.create(clientConfig);
124 String url = musicurl + "/keyspaces/" + keyspaceName + "/tables/votecount/rows";
125 WebResource webResource = client.resource(url);
127 ClientResponse response = webResource.accept("application/json").type("application/json")
128 .post(ClientResponse.class, jIns);
130 if (response.getStatus() < 200 || response.getStatus() > 299)
131 throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
134 private String createLock(String lockName) {
135 Client client = Client.create();
136 String msg = musicurl + "/locks/create/" + lockName;
137 WebResource webResource = client.resource(msg);
138 System.out.println(msg);
140 WebResource.Builder wb = webResource.accept(MediaType.TEXT_PLAIN);
142 ClientResponse response = wb.post(ClientResponse.class);
144 if (response.getStatus() != 200) {
145 throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
148 String output = response.getEntity(String.class);
153 private boolean acquireLock(String lockId) {
154 Client client = Client.create();
155 String msg = musicurl + "/locks/acquire/" + lockId;
156 System.out.println(msg);
157 WebResource webResource = client.resource(msg);
160 WebResource.Builder wb = webResource.accept(MediaType.TEXT_PLAIN);
162 ClientResponse response = wb.get(ClientResponse.class);
164 if (response.getStatus() != 200) {
165 throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
168 String output = response.getEntity(String.class);
169 Boolean status = Boolean.parseBoolean(output);
170 System.out.println("Server response .... \n");
171 System.out.println(output);
175 private void unlock(String lockId) {
176 Client client = Client.create();
177 WebResource webResource = client.resource(musicurl + "/locks/release/" + lockId);
179 ClientResponse response = webResource.delete(ClientResponse.class);
182 if (response.getStatus() != 204) {
183 throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
187 public String musicCriticalPutAndUpdate(String candidateName) {
189 * create lock for the candidate. The music API dictates that the lock name must be of the
190 * form keyspacename.tableName.primaryKeyName
192 createEntryForCandidate(candidateName);
193 System.out.println("trying to acquire lock!");
195 String lockName = keyspaceName + ".votecount." + candidateName;
196 String lockId = createLock(lockName);
197 while (acquireLock(lockId) != true);
199 System.out.println("acquired lock!");
200 // update candidate entry if you have the lock
201 Map<String, Object> values = new HashMap<String, Object>();
202 values.put("count", 5);
204 Map<String, String> consistencyInfo = new HashMap<String, String>();
205 consistencyInfo.put("type", "atomic");
206 consistencyInfo.put("lockId", lockId);
208 JsonInsert jIns = new JsonInsert();
209 jIns.setValues(values);
210 jIns.setConsistencyInfo(consistencyInfo);
211 ClientConfig clientConfig = new DefaultClientConfig();
213 clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
215 Client client = Client.create(clientConfig);
216 String url = musicurl + "/keyspaces/" + keyspaceName + "/tables/votecount/rows?name="
218 WebResource webResource = client.resource(url);
220 ClientResponse response = webResource.accept("application/json").type("application/json")
221 .put(ClientResponse.class, jIns);
223 if (response.getStatus() < 200 || response.getStatus() > 299)
224 throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
226 // release lock now that the operation is done
228 return "musicCriticalPutAndUpdate:" + url;
232 public String musicPutAndUpdate(String candidateName) {
233 createEntryForCandidate(candidateName);
235 Map<String, Object> values = new HashMap<String, Object>();
236 values.put("count", 5);
238 Map<String, String> consistencyInfo = new HashMap<String, String>();
239 consistencyInfo.put("type", "eventual");
241 JsonInsert jIns = new JsonInsert();
242 jIns.setValues(values);
243 jIns.setConsistencyInfo(consistencyInfo);
244 ClientConfig clientConfig = new DefaultClientConfig();
246 clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
248 Client client = Client.create(clientConfig);
249 String url = musicurl + "/keyspaces/" + keyspaceName + "/tables/votecount/rows?name="
251 WebResource webResource = client.resource(url);
253 ClientResponse response = webResource.accept("application/json").type("application/json")
254 .put(ClientResponse.class, jIns);
256 if (response.getStatus() < 200 || response.getStatus() > 299)
257 throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
258 return "musicPutAndUpdate:" + url;
261 public String musicGet() {
262 ClientConfig clientConfig = new DefaultClientConfig();
264 clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
266 Client client = Client.create(clientConfig);
268 String url = musicurl + "/keyspaces/" + keyspaceName + "/tables/votecount/rows?name="
270 WebResource webResource = client.resource(url);
272 ClientResponse response = webResource.accept("application/json").get(ClientResponse.class);
274 if (response.getStatus() < 200 || response.getStatus() > 299)
275 throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
277 Map<String, Object> output = response.getEntity(Map.class);
278 return "musicGet:" + url;
281 public String cassaQuorumPutAndUpdate(String candidateName) {
282 // http://135.197.226.98:8080/MUSIC/rest/formal/purecassa/keyspaces/shankarks/tables/employees/rows?emp_name=shankaruser1
283 Map<String, Object> values = new HashMap<String, Object>();
284 values.put("count", 5);
286 Map<String, String> consistencyInfo = new HashMap<String, String>();
287 consistencyInfo.put("type", "atomic");
289 JsonInsert jIns = new JsonInsert();
290 jIns.setValues(values);
291 jIns.setConsistencyInfo(consistencyInfo);
292 ClientConfig clientConfig = new DefaultClientConfig();
294 clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
296 Client client = Client.create(clientConfig);
297 String url = musicurl + "/purecassa/keyspaces/" + keyspaceName
298 + "/tables/votecount/rows?name=" + candidateName;
299 WebResource webResource = client.resource(url);
301 ClientResponse response = webResource.accept("application/json").type("application/json")
302 .put(ClientResponse.class, jIns);
304 if (response.getStatus() < 200 || response.getStatus() > 299)
305 throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
306 return "cassaQuorumPutAndUpdate:" + url;
310 public String cassaPutAndUpdate(String candidateName) {
311 // http://135.197.226.98:8080/MUSIC/rest/formal/purecassa/keyspaces/shankarks/tables/employees/rows?emp_name=shankaruser1
312 long start = System.currentTimeMillis();
313 createEntryForCandidate(candidateName);
315 Map<String, Object> values = new HashMap<String, Object>();
316 values.put("count", 5);
318 Map<String, String> consistencyInfo = new HashMap<String, String>();
319 consistencyInfo.put("type", "eventual");
321 JsonInsert jIns = new JsonInsert();
322 jIns.setValues(values);
323 jIns.setConsistencyInfo(consistencyInfo);
324 ClientConfig clientConfig = new DefaultClientConfig();
326 clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
328 Client client = Client.create(clientConfig);
329 String url = musicurl + "/purecassa/keyspaces/" + keyspaceName
330 + "/tables/votecount/rows?name=" + candidateName;
331 WebResource webResource = client.resource(url);
332 long end = System.currentTimeMillis();
333 String time = (end - start) + "";
335 start = System.currentTimeMillis();
336 ClientResponse response = webResource.accept("application/json").type("application/json")
337 .put(ClientResponse.class, jIns);
338 end = System.currentTimeMillis();
339 String time2 = (end - start) + "";
340 if (response.getStatus() < 200 || response.getStatus() > 299)
341 throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
342 return "cassaPutAndUpdate:" + url;
345 public String cassaGet() {
346 ClientConfig clientConfig = new DefaultClientConfig();
348 clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
350 Client client = Client.create(clientConfig);
352 String url = musicurl + "/keyspaces/" + keyspaceName + "/tables/votecount/rows?name="
354 WebResource webResource = client.resource(url);
356 ClientResponse response = webResource.accept("application/json").get(ClientResponse.class);
358 if (response.getStatus() < 200 || response.getStatus() > 299)
359 throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
361 Map<String, Object> output = response.getEntity(Map.class);
362 return "cassaGet:" + url;
365 private void zkCreate(String candidateName) {
366 // http://135.197.226.98:8080/MUSIC/rest/formal/purezk/shankarzknode
367 ClientConfig clientConfig = new DefaultClientConfig();
369 clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
371 Client client = Client.create(clientConfig);
373 WebResource webResource = client.resource(musicurl + "/purezk/" + candidateName);
375 ClientResponse response = webResource.accept("application/json").type("application/json")
376 .post(ClientResponse.class);
378 if (response.getStatus() < 200 || response.getStatus() > 299)
379 throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
382 public String zkPutAndUpdate(String candidateName) {
383 // http://135.197.226.99:8080/MUSIC/rest/formal/purezk/shankarzknode
386 zkCreate(candidateName);
388 Map<String, Object> values = new HashMap<String, Object>();
389 values.put("count", 5);
391 Map<String, String> consistencyInfo = new HashMap<String, String>();
392 consistencyInfo.put("type", "atomic");
394 JsonInsert jIns = new JsonInsert();
395 jIns.setValues(values);
396 jIns.setConsistencyInfo(consistencyInfo);
397 ClientConfig clientConfig = new DefaultClientConfig();
399 clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
401 Client client = Client.create(clientConfig);
402 String url = musicurl + "/purezk/" + candidateName;
403 System.out.println("in zk put:" + url);
404 WebResource webResource = client.resource(url);
406 ClientResponse response = webResource.accept("application/json").type("application/json")
407 .put(ClientResponse.class, jIns);
409 if (response.getStatus() < 200 || response.getStatus() > 299)
410 throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
412 return "zkPutAndUpdate:" + url;
415 public String zkGet() {
416 Client client = Client.create();
417 String url = musicurl + "/purezk/" + userForGets;
418 System.out.println("in zk get:" + url);
419 WebResource webResource = client.resource(url);
421 ClientResponse response = webResource.accept("text/plain").get(ClientResponse.class);
423 if (response.getStatus() != 200) {
424 throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
427 String output = response.getEntity(String.class);
428 return "zkGet:" + url;
432 public void bootStrap() {
433 // createVotingKeyspace();
434 // createVotingTable();
435 // createEntryForCandidate(userForGets);
436 // zkPutAndUpdate(userForGets);