-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRandomTableDataProvider.java
More file actions
141 lines (125 loc) · 5.01 KB
/
RandomTableDataProvider.java
File metadata and controls
141 lines (125 loc) · 5.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* Copyright 2023 Greptime Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.greptime.bench;
import com.codahale.metrics.Timer;
import io.greptime.common.SPI;
import io.greptime.common.util.MetricsUtil;
import io.greptime.common.util.SystemPropertyUtil;
import io.greptime.models.DataType;
import io.greptime.models.TableSchema;
import java.util.Iterator;
import java.util.concurrent.ThreadLocalRandom;
@SPI(
name = "random_table_data_provider",
priority = 10 /* newer implementation can use higher priority to override the old one */)
public class RandomTableDataProvider implements TableDataProvider {
private final TableSchema tableSchema;
private final long rowCount;
{
tableSchema = TableSchema.newBuilder("my_bench_table")
.addTimestamp("log_ts", DataType.TimestampMillisecond)
.addField("business_name", DataType.String)
.addField("app_name", DataType.String)
.addField("host_name", DataType.String)
.addField("log_message", DataType.String) // 2K
.addField("log_level", DataType.String)
.addField("log_name", DataType.String)
.addField("uri", DataType.String)
.addField("trace_id", DataType.String)
.addField("span_id", DataType.String)
.addField("errno", DataType.String)
.addField("trace_flags", DataType.String)
.addField("trace_state", DataType.String)
.addField("pod_name", DataType.String)
.build();
// Total number of rows to generate, configurable via system property
rowCount = SystemPropertyUtil.getLong("table_data_provider.row_count", 5_000_000L);
}
@Override
public void init() {}
@Override
public TableSchema tableSchema() {
return this.tableSchema;
}
@Override
public Iterator<Object[]> rows() {
ThreadLocalRandom random = ThreadLocalRandom.current();
return new Iterator<Object[]>() {
private long index = 0;
@Override
public boolean hasNext() {
return index < rowCount;
}
@Override
public Object[] next() {
index++;
Timer.Context timerContext =
MetricsUtil.timer("random_table_data_provider.next_row").time();
long logTs = System.currentTimeMillis();
String businessName = "business_" + random.nextInt(10);
String appName = "app_" + random.nextInt(100);
String hostName = "host_" + random.nextInt(1000);
String logMessage = LogTextHelper.generate2kText(random, logTs);
String logLevel = LogTextHelper.randomLogLevel(random);
String logName = "log_name_" + random.nextInt(100);
String uri = "http://example.com/path_" + random.nextInt(1000);
String traceId = "trace_" + random.nextLong(1000000);
String spanId = "span_" + random.nextLong(1000000);
String errno = "errno_" + random.nextInt(1000);
String traceFlags;
int flags = random.nextInt(1000);
if (flags % 2 == 0) {
traceFlags = "trace_flags_" + flags;
} else {
traceFlags = null;
}
int state = random.nextInt(1000);
String traceState;
if (state % 3 == 0) {
traceState = "trace_state_" + state;
} else {
traceState = null;
}
String podName = "pod_" + random.nextInt(1000);
timerContext.stop();
MetricsUtil.histogram("random_table_data_provider.log_message_length")
.update(logMessage.length());
return new Object[] {
logTs,
businessName,
appName,
hostName,
logMessage,
logLevel,
logName,
uri,
traceId,
spanId,
errno,
traceFlags,
traceState,
podName
};
}
};
}
@Override
public void close() throws Exception {}
@Override
public long rowCount() {
return rowCount;
}
}