1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 package org.apache.zookeeper.audit;
19 
20 import java.io.IOException;
21 import java.util.concurrent.TimeoutException;
22 import org.apache.zookeeper.CreateMode;
23 import org.apache.zookeeper.KeeperException;
24 import org.apache.zookeeper.ZooDefs.Ids;
25 import org.apache.zookeeper.ZooKeeper;
26 import org.apache.zookeeper.common.Time;
27 import org.apache.zookeeper.data.Stat;
28 import org.apache.zookeeper.test.ClientBase.CountdownWatcher;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 
32 public class ZKAuditLoggerPerformance {
33     private static final Logger LOG = LoggerFactory
34             .getLogger(ZKAuditLoggerPerformance.class);
35     private ZooKeeper zkClient;
36     private String parentPath;
37     private int numberOfRecords;
38 
ZKAuditLoggerPerformance(ZooKeeper zkClient, String parentPath, int numberOfRecords)39     public ZKAuditLoggerPerformance(ZooKeeper zkClient, String parentPath,
40                                     int numberOfRecords) {
41         this.zkClient = zkClient;
42         this.parentPath = parentPath;
43         this.numberOfRecords = numberOfRecords;
44     }
45 
create()46     public void create() throws Exception {
47         for (int i = 0; i < numberOfRecords; i++) {
48             zkClient.create(getPath(i), "0123456789".getBytes(),
49                     Ids.OPEN_ACL_UNSAFE,
50                     CreateMode.PERSISTENT);
51 
52         }
53     }
54 
setData()55     public void setData() throws Exception {
56         for (int i = 0; i < numberOfRecords; i++) {
57             zkClient.setData(getPath(i), "9876543210".getBytes(), -1);
58         }
59     }
60 
delete()61     public void delete() throws Exception {
62         for (int i = 0; i < numberOfRecords; i++) {
63             zkClient.delete(getPath(i), -1);
64         }
65     }
66 
doOperations()67     public AuditLogPerfReading doOperations() throws Exception {
68         AuditLogPerfReading perfReading = new AuditLogPerfReading();
69         // create
70         long startTime = Time.currentElapsedTime();
71         create();
72         perfReading.setCreate(Time.currentElapsedTime() - startTime);
73 
74         // setData
75         startTime = Time.currentElapsedTime();
76         setData();
77         perfReading.setSetData(Time.currentElapsedTime() - startTime);
78 
79         // delete
80         startTime = Time.currentElapsedTime();
81         delete();
82         perfReading.setDelete(Time.currentElapsedTime() - startTime);
83         return perfReading;
84     }
85 
getPath(int i)86     private String getPath(int i) {
87         return parentPath + "zNode" + i;
88     }
89 
main(String[] args)90     public static void main(String[] args) {
91         if (args.length != 3) {
92             System.err.println(
93                     "USAGE: ZKAuditLoggerPerformance connectionString parentPath numberOfRecords");
94             System.exit(1);
95         }
96         String cxnString = args[0];
97         CountdownWatcher watcher = new CountdownWatcher();
98         ZooKeeper zkClient = null;
99         try {
100             zkClient = new ZooKeeper(cxnString, 60000, watcher);
101             watcher.waitForConnected(30000);
102         } catch (InterruptedException | TimeoutException | IOException e) {
103             String msg = "ZooKeeper client can not connect to " + cxnString;
104             logErrorAndExit(e, msg);
105         }
106         String parentPath = args[1];
107         try {
108             Stat exists = zkClient.exists(parentPath, false);
109             if (exists == null) {
110                 System.err.println(
111                         "Parent path '" + parentPath + "' must exist.");
112                 System.exit(1);
113             }
114         } catch (KeeperException | InterruptedException e1) {
115             String msg = "Error while checking the existence of parent path";
116             logErrorAndExit(e1, msg);
117         }
118         int recordCount = 0;
119         try {
120             recordCount = Integer.parseInt(args[2]);
121         } catch (NumberFormatException e) {
122             String msg = "Failed to parse '" + args[2] + "' to integer";
123             LOG.error(msg, e);
124             System.err.println(msg);
125             System.exit(1);
126         }
127         ZKAuditLoggerPerformance auditLoggingPerf = new ZKAuditLoggerPerformance(
128                 zkClient,
129                 parentPath, recordCount);
130         AuditLogPerfReading doOperations = null;
131         try {
132             doOperations = auditLoggingPerf.doOperations();
133         } catch (Exception e) {
134             String msg = "Error while doing operations.";
135             LOG.error(msg, e);
136             System.err.println(msg);
137             System.exit(1);
138         }
139         System.out
140                 .println("Time taken for " + recordCount + " operations are:");
141         System.out.println(doOperations.report());
142         System.exit(0);
143     }
144 
logErrorAndExit(Exception e, String msg)145     private static void logErrorAndExit(Exception e, String msg) {
146         LOG.error(msg, e);
147         System.err.println(msg + ", error=" + e.getMessage());
148         System.exit(1);
149     }
150 }
151