18
18
19
19
import java .nio .charset .StandardCharsets ;
20
20
import java .time .Duration ;
21
+ import java .time .Instant ;
22
+ import java .time .temporal .ChronoUnit ;
21
23
import java .util .ArrayList ;
24
+ import java .util .HashMap ;
25
+ import java .util .HashSet ;
22
26
import java .util .List ;
27
+ import java .util .Map ;
28
+ import java .util .Set ;
23
29
import java .util .concurrent .ExecutorService ;
24
30
import java .util .concurrent .Future ;
25
31
import java .util .concurrent .ThreadLocalRandom ;
26
32
import java .util .concurrent .TimeUnit ;
27
33
import java .util .concurrent .TimeoutException ;
28
- import java .util .concurrent .atomic .AtomicInteger ;
29
34
30
35
abstract class AbstractRunner implements BenchmarkRunner {
31
- static final int TOTAL_RECORDS = 1000000 ;
32
- static final String SELECT_QUERY = "SELECT ID FROM FOO WHERE ID = @id" ;
33
- static final String UPDATE_QUERY = "UPDATE FOO SET BAR=1 WHERE ID = @id" ;
36
+ static final int TOTAL_RECORDS = 100000 ;
37
+ static final String TABLE_NAME = "Employees" ;
38
+ static final String SELECT_QUERY = String .format ("SELECT ID FROM %s WHERE ID = @id" , TABLE_NAME );
39
+ static final String UPDATE_QUERY =
40
+ String .format ("UPDATE %s SET Name=Google WHERE ID = @id" , TABLE_NAME );
34
41
static final String ID_COLUMN_NAME = "id" ;
35
- static final String SERVER_URL = "https://staging-wrenchworks.sandbox.googleapis.com" ;
42
+ static final Map < Environment , String > SERVER_URL_MAPPING = new HashMap <>() ;
36
43
37
- private final AtomicInteger operationCounter = new AtomicInteger ();
44
+ static {
45
+ SERVER_URL_MAPPING .put (
46
+ Environment .CLOUD_DEVEL , "https://staging-wrenchworks.sandbox.googleapis.com" );
47
+ SERVER_URL_MAPPING .put (Environment .PROD , "https://spanner.googleapis.com" );
48
+ }
49
+
50
+ Map <Integer , TimerConfiguration > timerConfigurations = new HashMap <>();
51
+ private final Set <Integer > completedClients = new HashSet <>();
52
+ private final Set <Integer > finishedClients = new HashSet <>();
53
+
54
+ protected void initiateTimer (int clientId , String message , Instant endTime ) {
55
+ TimerConfiguration timerConfiguration =
56
+ timerConfigurations .getOrDefault (clientId , new TimerConfiguration ());
57
+ timerConfiguration .setMessage (message );
58
+ timerConfiguration .setEndTime (endTime );
59
+ timerConfigurations .put (clientId , timerConfiguration );
60
+ }
38
61
39
- protected void incOperations ( ) {
40
- operationCounter . incrementAndGet ( );
62
+ protected void setBenchmarkingCompleted ( int clientId ) {
63
+ this . completedClients . add ( clientId );
41
64
}
42
65
43
66
protected List <Duration > collectResults (
44
67
ExecutorService service ,
45
68
List <Future <List <Duration >>> results ,
46
- int numClients ,
47
- int numOperations )
69
+ BenchmarkingConfiguration configuration )
48
70
throws Exception {
49
- int totalOperations = numClients * numOperations ;
71
+ while (!(finishedClients .size () == configuration .getNumOfClients ()))
72
+ for (int i = 0 ; i < configuration .getNumOfClients (); i ++) {
73
+ TimerConfiguration timerConfiguration =
74
+ timerConfigurations .getOrDefault (i , new TimerConfiguration ());
75
+ long totalSeconds =
76
+ ChronoUnit .SECONDS .between (Instant .now (), timerConfiguration .getEndTime ());
77
+ if (completedClients .contains (i )) {
78
+ if (!finishedClients .contains (i )) {
79
+ System .out .printf ("Client %s: Completed" , i );
80
+ finishedClients .add (i );
81
+ }
82
+ } else {
83
+ System .out .printf (
84
+ "Client %s: %s %s Minutes %s Seconds\r " ,
85
+ i + 1 , timerConfiguration .getMessage (), totalSeconds / 60 , totalSeconds % 60 );
86
+ }
87
+ //noinspection BusyWait
88
+ Thread .sleep (1000L );
89
+ }
50
90
service .shutdown ();
51
- while (!service .isTerminated ()) {
52
- //noinspection BusyWait
53
- Thread .sleep (1000L );
54
- System .out .printf ("\r %d/%d" , operationCounter .get (), totalOperations );
55
- }
56
- System .out .println ();
57
91
if (!service .awaitTermination (60L , TimeUnit .MINUTES )) {
58
92
throw new TimeoutException ();
59
93
}
60
- List <Duration > allResults = new ArrayList <>(numClients * numOperations );
94
+ List <Duration > allResults = new ArrayList <>();
61
95
for (Future <List <Duration >> result : results ) {
62
96
allResults .addAll (result .get ());
63
97
}
@@ -77,4 +111,25 @@ protected String generateRandomString() {
77
111
ThreadLocalRandom .current ().nextBytes (bytes );
78
112
return new String (bytes , StandardCharsets .UTF_8 );
79
113
}
114
+
115
+ static class TimerConfiguration {
116
+ private Instant endTime = Instant .now ();
117
+ private String message = "Waiting for benchmarks to start..." ;
118
+
119
+ Instant getEndTime () {
120
+ return endTime ;
121
+ }
122
+
123
+ void setEndTime (Instant endTime ) {
124
+ this .endTime = endTime ;
125
+ }
126
+
127
+ String getMessage () {
128
+ return message ;
129
+ }
130
+
131
+ void setMessage (String message ) {
132
+ this .message = message ;
133
+ }
134
+ }
80
135
}
0 commit comments