Browse Source

添加TimerService

张泳健 5 years ago
parent
commit
ba06d3a609

+ 2 - 2
src/main/java/com/vcarecity/cvs/module/SQLModule.java View File

@@ -34,8 +34,8 @@ public class SQLModule extends AbstractModule {
34 34
 
35 35
         Class<? extends ResultHandlerService> resultClass;
36 36
         if (isMysql) {
37
-            // resultClass = MySQLResultStatHandlerServiceImpl.class;
38
-            resultClass = MySQLResultHandlerServiceImpl.class;
37
+             resultClass = MySQLResultStatHandlerServiceImpl.class;
38
+            //resultClass = MySQLResultHandlerServiceImpl.class;
39 39
         } else {
40 40
             resultClass = CSVResultHandlerServiceImpl.class;
41 41
         }

+ 16 - 0
src/main/java/com/vcarecity/cvs/service/TimerService.java View File

@@ -0,0 +1,16 @@
1
+package com.vcarecity.cvs.service;
2
+
3
+/**
4
+ * @author Kerry on 19/12/03
5
+ */
6
+
7
+public interface TimerService {
8
+
9
+    /**
10
+     * 获取时间
11
+     *
12
+     * @return
13
+     */
14
+    long getUsageTime();
15
+
16
+}

+ 14 - 3
src/main/java/com/vcarecity/cvs/service/impl/AbstractSQLQueryService.java View File

@@ -2,6 +2,7 @@ package com.vcarecity.cvs.service.impl;
2 2
 
3 3
 import com.vcarecity.cvs.core.AutoMapper;
4 4
 import com.vcarecity.cvs.service.SQLQueryService;
5
+import com.vcarecity.cvs.service.TimerService;
5 6
 import com.zaxxer.hikari.HikariDataSource;
6 7
 import lombok.AllArgsConstructor;
7 8
 import lombok.Data;
@@ -12,6 +13,7 @@ import java.sql.*;
12 13
 import java.util.ArrayList;
13 14
 import java.util.List;
14 15
 import java.util.Set;
16
+import java.util.concurrent.atomic.AtomicLong;
15 17
 import java.util.stream.Collectors;
16 18
 
17 19
 import static com.vcarecity.cvs.FileExporterApp.TABLE_COLUMN;
@@ -21,10 +23,11 @@ import static com.vcarecity.cvs.FileExporterApp.TABLE_COLUMN;
21 23
  */
22 24
 
23 25
 @Slf4j
24
-public abstract class AbstractSQLQueryService implements SQLQueryService {
26
+public abstract class AbstractSQLQueryService implements SQLQueryService, TimerService {
25 27
 
26 28
     private boolean isFirst = true;
27 29
 
30
+    private AtomicLong readTime = new AtomicLong(0L);
28 31
 
29 32
     protected final HikariDataSource dataSource;
30 33
 
@@ -48,12 +51,16 @@ public abstract class AbstractSQLQueryService implements SQLQueryService {
48 51
 
49 52
         List<T> list = resultMapper(table, resultSet, clazz);
50 53
 
51
-        logger.info("QUERY SUCCESS. size = {}. usageTime = {} /ms", list.size(), (System.currentTimeMillis() - startTime));
52
-
53 54
         resultSet.close();
54 55
         preparedStatement.close();
55 56
         connection.close();
56 57
 
58
+        final long curReadTime = System.currentTimeMillis() - startTime;
59
+
60
+        readTime.addAndGet(curReadTime);
61
+
62
+        logger.info("QUERY SUCCESS. size = {}. usageTime = {} /ms", list.size(), curReadTime);
63
+
57 64
         return list;
58 65
     }
59 66
 
@@ -154,4 +161,8 @@ public abstract class AbstractSQLQueryService implements SQLQueryService {
154 161
         return list;
155 162
     }
156 163
 
164
+    @Override
165
+    public long getUsageTime() {
166
+        return this.readTime.longValue();
167
+    }
157 168
 }

+ 16 - 5
src/main/java/com/vcarecity/cvs/service/impl/MySQLResultHandlerServiceImpl.java View File

@@ -5,6 +5,7 @@ import com.google.inject.assistedinject.Assisted;
5 5
 import com.vcarecity.cvs.core.ColumnUpdateMapper;
6 6
 import com.vcarecity.cvs.properties.AppProperties;
7 7
 import com.vcarecity.cvs.service.ResultHandlerService;
8
+import com.vcarecity.cvs.service.TimerService;
8 9
 import com.vcarecity.cvs.util.CreateDatasource;
9 10
 import com.zaxxer.hikari.HikariDataSource;
10 11
 import lombok.extern.slf4j.Slf4j;
@@ -13,6 +14,7 @@ import java.sql.Connection;
13 14
 import java.sql.PreparedStatement;
14 15
 import java.util.Arrays;
15 16
 import java.util.List;
17
+import java.util.concurrent.atomic.AtomicLong;
16 18
 
17 19
 /**
18 20
  * @author Kerry on 19/11/21
@@ -20,14 +22,14 @@ import java.util.List;
20 22
 
21 23
 
22 24
 @Slf4j
23
-public class MySQLResultHandlerServiceImpl implements ResultHandlerService {
25
+public class MySQLResultHandlerServiceImpl implements ResultHandlerService, TimerService {
24 26
 
25
-    private final String sql;
26 27
     private final HikariDataSource dataSource;
27 28
 
29
+    private AtomicLong writeTime = new AtomicLong(0L);
30
+
28 31
     @Inject
29 32
     public MySQLResultHandlerServiceImpl(AppProperties properties, @Assisted Object sql) {
30
-        this.sql = (String) sql;
31 33
         this.dataSource = CreateDatasource.createDataSource(properties.getToDb());
32 34
     }
33 35
 
@@ -37,7 +39,6 @@ public class MySQLResultHandlerServiceImpl implements ResultHandlerService {
37 39
 
38 40
         final String sql = getSql(table, header);
39 41
 
40
-
41 42
         long startTime = System.currentTimeMillis();
42 43
 
43 44
         logger.debug("{},{}", dataList.size(), sql);
@@ -72,7 +73,12 @@ public class MySQLResultHandlerServiceImpl implements ResultHandlerService {
72 73
         } finally {
73 74
             connection.close();
74 75
         }
75
-        logger.info("WRITE SUCCESS to mysql usage time {} /ms, size = {}", System.currentTimeMillis() - startTime, dataList.size());
76
+
77
+        final long curUseTime = System.currentTimeMillis() - startTime;
78
+
79
+        writeTime.addAndGet(curUseTime);
80
+
81
+        logger.info("WRITE SUCCESS to mysql usage time {} /ms, size = {}", curUseTime, dataList.size());
76 82
     }
77 83
 
78 84
 
@@ -88,4 +94,9 @@ public class MySQLResultHandlerServiceImpl implements ResultHandlerService {
88 94
         return "INSERT INTO " + table + "(" + column + ") VALUES(" + join + ")";
89 95
 
90 96
     }
97
+
98
+    @Override
99
+    public long getUsageTime() {
100
+        return this.writeTime.longValue();
101
+    }
91 102
 }

+ 21 - 11
src/main/java/com/vcarecity/cvs/service/impl/MySQLResultStatHandlerServiceImpl.java View File

@@ -5,6 +5,7 @@ import com.google.inject.assistedinject.Assisted;
5 5
 import com.vcarecity.cvs.core.ColumnUpdateMapper;
6 6
 import com.vcarecity.cvs.properties.AppProperties;
7 7
 import com.vcarecity.cvs.service.ResultHandlerService;
8
+import com.vcarecity.cvs.service.TimerService;
8 9
 import com.vcarecity.cvs.util.CreateDatasource;
9 10
 import com.zaxxer.hikari.HikariDataSource;
10 11
 import lombok.extern.slf4j.Slf4j;
@@ -14,6 +15,7 @@ import java.sql.Date;
14 15
 import java.sql.Statement;
15 16
 import java.sql.Timestamp;
16 17
 import java.util.List;
18
+import java.util.concurrent.atomic.AtomicLong;
17 19
 
18 20
 /**
19 21
  * @author Kerry on 19/11/21
@@ -21,26 +23,26 @@ import java.util.List;
21 23
 
22 24
 
23 25
 @Slf4j
24
-public class MySQLResultStatHandlerServiceImpl implements ResultHandlerService {
26
+public class MySQLResultStatHandlerServiceImpl implements ResultHandlerService, TimerService {
25 27
 
26
-
27
-    private final String sql;
28 28
     private final HikariDataSource dataSource;
29 29
 
30
+    private AtomicLong writeTime = new AtomicLong(0L);
31
+
30 32
     @Inject
31 33
     public MySQLResultStatHandlerServiceImpl(AppProperties properties, @Assisted Object sql) {
32
-        this.sql = (String) sql;
33 34
         this.dataSource = CreateDatasource.createDataSource(properties.getToDb());
34 35
     }
35 36
 
36
-
37 37
     @Override
38 38
     public <T> void resultHandler(String table, String[] header, List<T> dataList) throws Exception {
39 39
 
40
-
40
+        if (dataList == null || dataList.isEmpty()) {
41
+            return;
42
+        }
41 43
         long startTime = System.currentTimeMillis();
42 44
 
43
-        StringBuffer sb = new StringBuffer();
45
+        StringBuilder sb = new StringBuilder();
44 46
 
45 47
         final String[] newHeaders = ColumnUpdateMapper.updateColumnName(table, header);
46 48
         final String column = String.join(",", newHeaders);
@@ -71,19 +73,27 @@ public class MySQLResultStatHandlerServiceImpl implements ResultHandlerService {
71 73
                 sb.append("),");
72 74
             }
73 75
         }
74
-        final StringBuffer stringBuffer = sb.deleteCharAt(sb.length() - 1);
76
+        String execSql = sb.deleteCharAt(sb.length() - 1).toString();
75 77
 
76
-        // logger.debug("{}", stringBuffer.toString());
78
+        // logger.debug("{}", execSql);
77 79
 
78 80
         final Connection connection = dataSource.getConnection();
79 81
 
80 82
         final Statement statement = connection.createStatement();
81
-        statement.execute(stringBuffer.toString());
83
+        statement.execute(execSql);
82 84
         statement.close();
83 85
         connection.close();
84 86
 
85
-        logger.info("WRITE SUCCESS to mysql usage time {} /ms, size = {}", System.currentTimeMillis() - startTime, dataList.size());
87
+        final long curUseTime = System.currentTimeMillis() - startTime;
88
+
89
+        writeTime.addAndGet(curUseTime);
90
+
91
+        logger.info("WRITE SUCCESS to mysql usage time {} /ms, size = {}", curUseTime, dataList.size());
86 92
     }
87 93
 
88 94
 
95
+    @Override
96
+    public long getUsageTime() {
97
+        return this.writeTime.longValue();
98
+    }
89 99
 }

+ 4 - 3
src/main/java/com/vcarecity/cvs/starter/SQLStarter.java View File

@@ -4,6 +4,7 @@ import com.vcarecity.cvs.core.ColumnUpdateMapper;
4 4
 import com.vcarecity.cvs.factory.ResultHandlerFactory;
5 5
 import com.vcarecity.cvs.service.ResultHandlerService;
6 6
 import com.vcarecity.cvs.service.SQLQueryService;
7
+import com.vcarecity.cvs.service.TimerService;
7 8
 import com.vcarecity.cvs.service.impl.CSVResultHandlerServiceImpl;
8 9
 import lombok.extern.slf4j.Slf4j;
9 10
 
@@ -77,13 +78,13 @@ public class SQLStarter {
77 78
             ((CSVResultHandlerServiceImpl) resultHandler).close();
78 79
         }
79 80
 
80
-
81 81
         final String[] headers = TABLE_COLUMN.get(table);
82 82
         final String[] mysqlHeader = ColumnUpdateMapper.updateColumnName(table, headers);
83 83
 
84 84
         logger.info("mysql header: {}", String.join(",", mysqlHeader));
85
-
86
-        logger.info("stop query database...{} /ms.", (System.currentTimeMillis() - startTime));
85
+        logger.info("query sql usage time {} /ms", ((TimerService) queryService).getUsageTime());
86
+        logger.info("write sql usage time {} /ms", ((TimerService) resultHandler).getUsageTime());
87
+        logger.info("all usage time {} /ms.", (System.currentTimeMillis() - startTime));
87 88
     }
88 89
 
89 90
 }