changeset 1053:6b8d78ecdeba

PgSqlMetric, DbUtils
author Devel 2
date Wed, 15 Apr 2020 11:06:03 +0200
parents 9b341057540b
children a8dda9af38cf
files stress-tester/src/main/java/com/passus/st/client/DbMetric.java stress-tester/src/main/java/com/passus/st/client/pgsql/PgSqlMetric.java stress-tester/src/main/java/com/passus/st/utils/DbUtils.java stress-tester/src/test/java/com/passus/st/utils/DbUtilsTest.java
diffstat 4 files changed, 161 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/client/DbMetric.java	Wed Apr 15 11:06:03 2020 +0200
@@ -0,0 +1,55 @@
+package com.passus.st.client;
+
+import com.passus.commons.metric.Metric;
+import org.apache.commons.lang3.mutable.MutableInt;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+public abstract class DbMetric extends GenericMetric {
+
+    private HashMap<String, MutableInt> queryTypeCount;
+
+    public DbMetric(String name) {
+        super(name);
+        attrs.put("queryTypeCount", queryTypeCount);
+    }
+
+    public abstract String extractStatementType(String query);
+
+    public Map<String, MutableInt> getQueryTypeCount() {
+        return Collections.unmodifiableMap(queryTypeCount);
+    }
+
+    public void addQuery(String query) {
+        String stmtType = extractStatementType(query);
+        if (stmtType != null) {
+            Metric.incrementCountMap(stmtType, queryTypeCount);
+        }
+    }
+
+    @Override
+    protected void doActivate() {
+        super.doActivate();
+        queryTypeCount = new HashMap<>();
+    }
+
+    @Override
+    protected void doDeactivate() {
+        super.doDeactivate();
+        reset();
+        queryTypeCount = null;
+    }
+
+    @Override
+    public void update(Metric metric) {
+        DbMetric dbMetric = (DbMetric) metric;
+        Metric.updateCountMap(queryTypeCount, dbMetric.queryTypeCount);
+    }
+
+    @Override
+    public void reset() {
+        queryTypeCount.clear();
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/client/pgsql/PgSqlMetric.java	Wed Apr 15 11:06:03 2020 +0200
@@ -0,0 +1,45 @@
+package com.passus.st.client.pgsql;
+
+import com.passus.st.client.DbMetric;
+import com.passus.st.utils.DbUtils;
+
+public class PgSqlMetric extends DbMetric {
+
+    public static final String DEFAULT_NAME = "PgSql";
+
+    private final StringBuilder sb = new StringBuilder();
+
+    public PgSqlMetric(String name) {
+        super(name);
+    }
+
+    public PgSqlMetric() {
+        super(DEFAULT_NAME);
+    }
+
+    @Override
+    public String extractStatementType(String query) {
+        sb.setLength(0);
+        int pos = DbUtils.extractStatementType(query, sb);
+        if (pos != -1) {
+            String stmtType = sb.toString().toUpperCase();
+            switch (stmtType) {
+                case "ALTER":
+                case "CREATE":
+                case "DROP":
+                case "START":
+                    sb.append(' ');
+                    pos = DbUtils.extractStatementType(query, pos, sb);
+                    if (pos != -1) {
+                        return sb.toString().toUpperCase();
+                    }
+
+                    break;
+                default:
+                    return stmtType;
+            }
+        }
+
+        return null;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/utils/DbUtils.java	Wed Apr 15 11:06:03 2020 +0200
@@ -0,0 +1,34 @@
+package com.passus.st.utils;
+
+import com.passus.commons.utils.AsciiUtils;
+
+public class DbUtils {
+
+    private DbUtils() {
+    }
+
+    public static int extractStatementType(String sql, StringBuilder out) {
+        return extractStatementType(sql, 0, out);
+    }
+
+    public static int extractStatementType(String sql, int pos, StringBuilder out) {
+        if (sql == null || sql.length() < 4) {
+            return -1;
+        }
+
+        int startLen = out.length();
+        while (pos < sql.length()) {
+            char ch = sql.charAt(pos++);
+            if (ch == ' ') {
+                break;
+            } else if (!AsciiUtils.isCharacter(ch)) {
+                out.setLength(startLen);
+                return -1;
+            }
+
+            out.append(ch);
+        }
+
+        return pos;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/test/java/com/passus/st/utils/DbUtilsTest.java	Wed Apr 15 11:06:03 2020 +0200
@@ -0,0 +1,27 @@
+package com.passus.st.utils;
+
+import org.testng.annotations.Test;
+
+import static org.testng.AssertJUnit.assertEquals;
+
+public class DbUtilsTest {
+
+    private String extractStatementType(String query) {
+        return extractStatementType(query, 0);
+    }
+
+    private String extractStatementType(String query, int pos) {
+        StringBuilder sb = new StringBuilder();
+        DbUtils.extractStatementType(query, pos, sb);
+        return sb.toString();
+    }
+
+    @Test
+    public void testExtractStatementType() {
+        assertEquals("SELECT", extractStatementType("SELECT * FROM test"));
+        assertEquals("select", extractStatementType("select * FROM test"));
+        assertEquals("select", extractStatementType("select"));
+        assertEquals("select", extractStatementType(" select", 1));
+        assertEquals("select", extractStatementType(" select ", 1));
+    }
+}
\ No newline at end of file