c267d25400ec8ac73be56bb3a2debdce03e38208
[music.git] / src / test / java / org / onap / music / benchmarks / MicroBenchMarks.java
1 /*
2  * ============LICENSE_START==========================================
3  * org.onap.music
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
10  * 
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  * 
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.
18  * 
19  * ============LICENSE_END=============================================
20  * ====================================================================
21  */
22 package org.onap.music.benchmarks;
23
24 import java.util.HashMap;
25 import java.util.Map;
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;
37
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";
42
43     public MicroBenchMarks() {
44         bootStrap();
45     }
46
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);
59
60         ClientConfig clientConfig = new DefaultClientConfig();
61
62         clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
63
64         Client client = Client.create(clientConfig);
65
66         WebResource webResource = client.resource(musicurl + "/keyspaces/" + keyspaceName);
67
68         ClientResponse response = webResource.accept("application/json").type("application/json")
69                         .post(ClientResponse.class, jsonKp);
70
71         if (response.getStatus() < 200 || response.getStatus() > 299)
72             throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
73
74     }
75
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)");
81
82
83         Map<String, String> consistencyInfo = new HashMap<String, String>();
84         consistencyInfo.put("type", "eventual");
85
86         JsonTable jtab = new JsonTable();
87         jtab.setFields(fields);
88         jtab.setConsistencyInfo(consistencyInfo);
89
90         ClientConfig clientConfig = new DefaultClientConfig();
91
92         clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
93
94         Client client = Client.create(clientConfig);
95
96         WebResource webResource = client
97                         .resource(musicurl + "/keyspaces/" + keyspaceName + "/tables/votecount");
98
99         ClientResponse response = webResource.accept("application/json").type("application/json")
100                         .post(ClientResponse.class, jtab);
101
102         if (response.getStatus() < 200 || response.getStatus() > 299)
103             throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
104
105     }
106
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);
111
112         Map<String, String> consistencyInfo = new HashMap<String, String>();
113         consistencyInfo.put("type", "eventual");
114
115         JsonInsert jIns = new JsonInsert();
116         jIns.setValues(values);
117         jIns.setConsistencyInfo(consistencyInfo);
118         ClientConfig clientConfig = new DefaultClientConfig();
119
120         clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
121
122         Client client = Client.create(clientConfig);
123
124         String url = musicurl + "/keyspaces/" + keyspaceName + "/tables/votecount/rows";
125         WebResource webResource = client.resource(url);
126
127         ClientResponse response = webResource.accept("application/json").type("application/json")
128                         .post(ClientResponse.class, jIns);
129
130         if (response.getStatus() < 200 || response.getStatus() > 299)
131             throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
132     }
133
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);
139
140         WebResource.Builder wb = webResource.accept(MediaType.TEXT_PLAIN);
141
142         ClientResponse response = wb.post(ClientResponse.class);
143
144         if (response.getStatus() != 200) {
145             throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
146         }
147
148         String output = response.getEntity(String.class);
149
150         return output;
151     }
152
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);
158
159
160         WebResource.Builder wb = webResource.accept(MediaType.TEXT_PLAIN);
161
162         ClientResponse response = wb.get(ClientResponse.class);
163
164         if (response.getStatus() != 200) {
165             throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
166         }
167
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);
172         return status;
173     }
174
175     private void unlock(String lockId) {
176         Client client = Client.create();
177         WebResource webResource = client.resource(musicurl + "/locks/release/" + lockId);
178
179         ClientResponse response = webResource.delete(ClientResponse.class);
180
181
182         if (response.getStatus() != 204) {
183             throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
184         }
185     }
186
187     public String musicCriticalPutAndUpdate(String candidateName) {
188         /*
189          * create lock for the candidate. The music API dictates that the lock name must be of the
190          * form keyspacename.tableName.primaryKeyName
191          */
192         createEntryForCandidate(candidateName);
193         System.out.println("trying to acquire lock!");
194
195         String lockName = keyspaceName + ".votecount." + candidateName;
196         String lockId = createLock(lockName);
197         while (acquireLock(lockId) != true);
198
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);
203
204         Map<String, String> consistencyInfo = new HashMap<String, String>();
205         consistencyInfo.put("type", "atomic");
206         consistencyInfo.put("lockId", lockId);
207
208         JsonInsert jIns = new JsonInsert();
209         jIns.setValues(values);
210         jIns.setConsistencyInfo(consistencyInfo);
211         ClientConfig clientConfig = new DefaultClientConfig();
212
213         clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
214
215         Client client = Client.create(clientConfig);
216         String url = musicurl + "/keyspaces/" + keyspaceName + "/tables/votecount/rows?name="
217                         + candidateName;
218         WebResource webResource = client.resource(url);
219
220         ClientResponse response = webResource.accept("application/json").type("application/json")
221                         .put(ClientResponse.class, jIns);
222
223         if (response.getStatus() < 200 || response.getStatus() > 299)
224             throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
225
226         // release lock now that the operation is done
227         unlock(lockId);
228         return "musicCriticalPutAndUpdate:" + url;
229
230     }
231
232     public String musicPutAndUpdate(String candidateName) {
233         createEntryForCandidate(candidateName);
234
235         Map<String, Object> values = new HashMap<String, Object>();
236         values.put("count", 5);
237
238         Map<String, String> consistencyInfo = new HashMap<String, String>();
239         consistencyInfo.put("type", "eventual");
240
241         JsonInsert jIns = new JsonInsert();
242         jIns.setValues(values);
243         jIns.setConsistencyInfo(consistencyInfo);
244         ClientConfig clientConfig = new DefaultClientConfig();
245
246         clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
247
248         Client client = Client.create(clientConfig);
249         String url = musicurl + "/keyspaces/" + keyspaceName + "/tables/votecount/rows?name="
250                         + candidateName;
251         WebResource webResource = client.resource(url);
252
253         ClientResponse response = webResource.accept("application/json").type("application/json")
254                         .put(ClientResponse.class, jIns);
255
256         if (response.getStatus() < 200 || response.getStatus() > 299)
257             throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
258         return "musicPutAndUpdate:" + url;
259     }
260
261     public String musicGet() {
262         ClientConfig clientConfig = new DefaultClientConfig();
263
264         clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
265
266         Client client = Client.create(clientConfig);
267
268         String url = musicurl + "/keyspaces/" + keyspaceName + "/tables/votecount/rows?name="
269                         + userForGets;
270         WebResource webResource = client.resource(url);
271
272         ClientResponse response = webResource.accept("application/json").get(ClientResponse.class);
273
274         if (response.getStatus() < 200 || response.getStatus() > 299)
275             throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
276
277         Map<String, Object> output = response.getEntity(Map.class);
278         return "musicGet:" + url;
279     }
280
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);
285
286         Map<String, String> consistencyInfo = new HashMap<String, String>();
287         consistencyInfo.put("type", "atomic");
288
289         JsonInsert jIns = new JsonInsert();
290         jIns.setValues(values);
291         jIns.setConsistencyInfo(consistencyInfo);
292         ClientConfig clientConfig = new DefaultClientConfig();
293
294         clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
295
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);
300
301         ClientResponse response = webResource.accept("application/json").type("application/json")
302                         .put(ClientResponse.class, jIns);
303
304         if (response.getStatus() < 200 || response.getStatus() > 299)
305             throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
306         return "cassaQuorumPutAndUpdate:" + url;
307
308     }
309
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);
314
315         Map<String, Object> values = new HashMap<String, Object>();
316         values.put("count", 5);
317
318         Map<String, String> consistencyInfo = new HashMap<String, String>();
319         consistencyInfo.put("type", "eventual");
320
321         JsonInsert jIns = new JsonInsert();
322         jIns.setValues(values);
323         jIns.setConsistencyInfo(consistencyInfo);
324         ClientConfig clientConfig = new DefaultClientConfig();
325
326         clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
327
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) + "";
334
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;
343     }
344
345     public String cassaGet() {
346         ClientConfig clientConfig = new DefaultClientConfig();
347
348         clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
349
350         Client client = Client.create(clientConfig);
351
352         String url = musicurl + "/keyspaces/" + keyspaceName + "/tables/votecount/rows?name="
353                         + userForGets;
354         WebResource webResource = client.resource(url);
355
356         ClientResponse response = webResource.accept("application/json").get(ClientResponse.class);
357
358         if (response.getStatus() < 200 || response.getStatus() > 299)
359             throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
360
361         Map<String, Object> output = response.getEntity(Map.class);
362         return "cassaGet:" + url;
363     }
364
365     private void zkCreate(String candidateName) {
366         // http://135.197.226.98:8080/MUSIC/rest/formal/purezk/shankarzknode
367         ClientConfig clientConfig = new DefaultClientConfig();
368
369         clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
370
371         Client client = Client.create(clientConfig);
372
373         WebResource webResource = client.resource(musicurl + "/purezk/" + candidateName);
374
375         ClientResponse response = webResource.accept("application/json").type("application/json")
376                         .post(ClientResponse.class);
377
378         if (response.getStatus() < 200 || response.getStatus() > 299)
379             throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
380     }
381
382     public String zkPutAndUpdate(String candidateName) {
383         // http://135.197.226.99:8080/MUSIC/rest/formal/purezk/shankarzknode
384
385         // CREATE IT FIRST
386         zkCreate(candidateName);
387
388         Map<String, Object> values = new HashMap<String, Object>();
389         values.put("count", 5);
390
391         Map<String, String> consistencyInfo = new HashMap<String, String>();
392         consistencyInfo.put("type", "atomic");
393
394         JsonInsert jIns = new JsonInsert();
395         jIns.setValues(values);
396         jIns.setConsistencyInfo(consistencyInfo);
397         ClientConfig clientConfig = new DefaultClientConfig();
398
399         clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
400
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);
405
406         ClientResponse response = webResource.accept("application/json").type("application/json")
407                         .put(ClientResponse.class, jIns);
408
409         if (response.getStatus() < 200 || response.getStatus() > 299)
410             throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
411
412         return "zkPutAndUpdate:" + url;
413     }
414
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);
420
421         ClientResponse response = webResource.accept("text/plain").get(ClientResponse.class);
422
423         if (response.getStatus() != 200) {
424             throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
425         }
426
427         String output = response.getEntity(String.class);
428         return "zkGet:" + url;
429     }
430
431
432     public void bootStrap() {
433         // createVotingKeyspace();
434         // createVotingTable();
435         // createEntryForCandidate(userForGets);
436         // zkPutAndUpdate(userForGets);
437     }
438
439
440 }