embulk-filter-speedometer 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,63 @@
1
+ package org.embulk.filter;
2
+
3
+ public class SpeedometerUtil {
4
+
5
+ public static String toNumberText(long originalNum) {
6
+ long baseNum = 1000;
7
+ long remain = 0;
8
+ float num = originalNum;
9
+
10
+ String[] units = new String[]{ "b", "kb", "mb", "gb", "tb", "pb", "eb", "zb", "yb" };
11
+ for (String unit : units) {
12
+ if (num < baseNum) {
13
+ if (num < baseNum/10) {
14
+ return String.format("%1.1f%s", num + ((float)remain) / baseNum, unit);
15
+ } else {
16
+ return String.format("%2d%s", (long)num, unit);
17
+ }
18
+ } else {
19
+ num /= baseNum;
20
+ }
21
+ }
22
+
23
+ return String.valueOf(originalNum);
24
+ }
25
+
26
+ public static String toTimeText(long timeMillisecs) {
27
+ long num = timeMillisecs;
28
+
29
+ long mseconds = num % 1000;
30
+ num /= 1000;
31
+
32
+ if (num < 10) {
33
+ return String.format("%1.2f", num + mseconds / 1000.0);
34
+ }
35
+
36
+ if (num < 60) {
37
+ return String.format("%1.1f", num + mseconds / 1000.0);
38
+ }
39
+
40
+ long seconds = num % 60;
41
+ num /= 60;
42
+
43
+ if (num < 60) {
44
+ return String.format("%1d:%02d", num, seconds);
45
+ }
46
+
47
+ long mins = num % 60;
48
+ num /= 60;
49
+
50
+ if (num < 24) {
51
+ return String.format("%1d:%02d:%02d", num, mins, seconds);
52
+ }
53
+
54
+ long hours = num % 24;
55
+ num /= 24;
56
+
57
+ if (num < 10) {
58
+ return String.format("%1.1f days", num + hours / 24.0);
59
+ }
60
+
61
+ return String.format("%1d days", num);
62
+ }
63
+ }
@@ -0,0 +1,114 @@
1
+ package org.embulk.filter;
2
+
3
+ import mockit.Mocked;
4
+ import mockit.NonStrictExpectations;
5
+ import mockit.Verifications;
6
+
7
+ import org.embulk.config.ConfigSource;
8
+ import org.embulk.config.TaskSource;
9
+ import org.embulk.filter.SpeedometerFilterPlugin.PluginTask;
10
+ import org.embulk.spi.ColumnVisitor;
11
+ import org.embulk.spi.Exec;
12
+ import org.embulk.spi.FilterPlugin;
13
+ import org.embulk.spi.Page;
14
+ import org.embulk.spi.PageBuilder;
15
+ import org.embulk.spi.PageOutput;
16
+ import org.embulk.spi.PageReader;
17
+ import org.embulk.spi.Schema;
18
+ import org.junit.Test;
19
+
20
+ public class TestSpeedometerFilterPlugin
21
+ {
22
+ @Mocked
23
+ Exec exec;
24
+
25
+ @Mocked
26
+ ConfigSource config;
27
+
28
+ @Mocked
29
+ PluginTask task;
30
+
31
+ @Mocked
32
+ TaskSource taskSource;
33
+
34
+ @Mocked
35
+ Schema schema;
36
+
37
+ @Mocked
38
+ PageOutput inPageOutput;
39
+
40
+ @Mocked
41
+ FilterPlugin.Control control;
42
+
43
+ @Test
44
+ public void testTransaction() {
45
+ new NonStrictExpectations() {{
46
+ config.loadConfig(PluginTask.class); result = task;
47
+ }};
48
+
49
+ SpeedometerFilterPlugin plugin = new SpeedometerFilterPlugin();
50
+ plugin.transaction(config, schema, control);
51
+
52
+ new Verifications() {{
53
+ config.loadConfig(PluginTask.class); times = 1;
54
+ control.run((TaskSource)any, schema); times = 1;
55
+ }};
56
+ }
57
+
58
+ @Test
59
+ public void testOpen(final @Mocked PageReader reader, final @Mocked PageBuilder builder, final @Mocked Page page) throws Exception {
60
+ new NonStrictExpectations() {{
61
+ taskSource.loadTask(PluginTask.class); result = task;
62
+ task.getDelimiter(); result = "";
63
+ reader.nextRecord(); result = true; result = false;
64
+ }};
65
+
66
+ SpeedometerFilterPlugin plugin = new SpeedometerFilterPlugin();
67
+ PageOutput output = plugin.open(taskSource, schema, schema, inPageOutput);
68
+ output.add(page);
69
+
70
+ new Verifications() {{
71
+ taskSource.loadTask(PluginTask.class); times = 1;
72
+ builder.finish(); times = 1;
73
+ builder.addRecord(); times = 1;
74
+ reader.nextRecord(); times = 2;
75
+ reader.setPage(page); times = 1;
76
+ schema.visitColumns(withInstanceOf(ColumnVisitor.class)); times = 2;
77
+ }};
78
+ }
79
+
80
+ @Test
81
+ public void testFinish(final @Mocked PageReader reader, final @Mocked PageBuilder builder, final @Mocked Page page) throws Exception {
82
+ new NonStrictExpectations() {{
83
+ taskSource.loadTask(PluginTask.class); result = task;
84
+ task.getDelimiter(); result = "";
85
+ }};
86
+
87
+ SpeedometerFilterPlugin plugin = new SpeedometerFilterPlugin();
88
+ PageOutput output = plugin.open(taskSource, schema, schema, inPageOutput);
89
+ output.finish();
90
+
91
+ new Verifications() {{
92
+ taskSource.loadTask(PluginTask.class); times = 1;
93
+ inPageOutput.finish(); times = 1;
94
+ }};
95
+ }
96
+
97
+ @Test
98
+ public void testClose(final @Mocked PageReader reader, final @Mocked PageBuilder builder, final @Mocked Page page) throws Exception {
99
+ new NonStrictExpectations() {{
100
+ taskSource.loadTask(PluginTask.class); result = task;
101
+ task.getDelimiter(); result = "";
102
+ }};
103
+
104
+ SpeedometerFilterPlugin plugin = new SpeedometerFilterPlugin();
105
+ PageOutput output = plugin.open(taskSource, schema, schema, inPageOutput);
106
+ output.close();
107
+
108
+ new Verifications() {{
109
+ taskSource.loadTask(PluginTask.class); times = 1;
110
+ reader.close(); times = 1;
111
+ inPageOutput.close(); times = 1;
112
+ }};
113
+ }
114
+ }
@@ -0,0 +1,120 @@
1
+ package org.embulk.filter;
2
+
3
+ import static org.junit.Assert.assertEquals;
4
+ import static org.junit.Assert.assertFalse;
5
+ import static org.junit.Assert.assertNotNull;
6
+ import static org.junit.Assert.assertTrue;
7
+ import mockit.Mocked;
8
+ import mockit.NonStrictExpectations;
9
+ import mockit.Verifications;
10
+
11
+ import org.embulk.spi.Exec;
12
+ import org.junit.Test;
13
+ import org.slf4j.Logger;
14
+
15
+ public class TestSpeedometerSpeedAggregator {
16
+ @Mocked SpeedometerSpeedController controller;
17
+ @Mocked Exec exec;
18
+
19
+ @Test
20
+ public void testGetInstance() {
21
+ assertNotNull("Verify there is a singleton.", SpeedometerSpeedAggregator.getInstance());
22
+ }
23
+
24
+ @Test
25
+ public void testSpeedometerSpeedAggregator() {
26
+ SpeedometerSpeedAggregator aggregator = new SpeedometerSpeedAggregator();
27
+ assertEquals("Verify the default global start time is zero.", 0, aggregator.getGlobalStartTime());
28
+ assertEquals("Verify the default active count is zero.", 0, aggregator.getActiveControllerCount());
29
+ }
30
+
31
+ @Test
32
+ public void testStartController() {
33
+ SpeedometerSpeedAggregator aggregator = new SpeedometerSpeedAggregator();
34
+ long nowTime = System.currentTimeMillis();
35
+ aggregator.startController(controller, nowTime);
36
+ assertEquals("Verify global start time is set.", nowTime, aggregator.getGlobalStartTime());
37
+ assertEquals("Verify active controller count.", 1, aggregator.getActiveControllerCount());
38
+ assertTrue("Verify there is a registered controller.", aggregator.getControllerList().contains(controller));
39
+ }
40
+
41
+ @Test
42
+ public void testStopController() {
43
+ new NonStrictExpectations() {{
44
+ controller.getTotalBytes(); result = 11;
45
+ }};
46
+
47
+ SpeedometerSpeedAggregator aggregator = new SpeedometerSpeedAggregator();
48
+ long nowTime = System.currentTimeMillis();
49
+ aggregator.startController(controller, nowTime);
50
+ aggregator.stopController(controller);
51
+ assertEquals("Verify active controller count.", 0, aggregator.getActiveControllerCount());
52
+ assertFalse("Verify there is a registered controller.", aggregator.getControllerList().contains(controller));
53
+ assertEquals("Verify controller's total bytes are added to aggregator.", 11, aggregator.getGlobalTotalBytes());
54
+ }
55
+
56
+ @Test
57
+ public void testStopControllerShowOverallMessage(@Mocked final Logger logger) {
58
+ new NonStrictExpectations() {{
59
+ Exec.getLogger(SpeedometerFilterPlugin.class); result = logger;
60
+ }};
61
+
62
+ SpeedometerSpeedAggregator aggregator = new SpeedometerSpeedAggregator();
63
+ long nowTime = System.currentTimeMillis();
64
+ aggregator.startController(controller, nowTime);
65
+ aggregator.startController(controller, nowTime);
66
+ aggregator.stopController(controller);
67
+ aggregator.stopController(controller);
68
+
69
+ new Verifications() {{
70
+ logger.info(withAny("Overall message.")); times = 2;
71
+ }};
72
+ }
73
+
74
+ @Test
75
+ public void testGetSpeedLimitForController() {
76
+ new NonStrictExpectations() {{
77
+ controller.getSpeedLimit(); result = 10;
78
+ }};
79
+
80
+ SpeedometerSpeedAggregator aggregator = new SpeedometerSpeedAggregator();
81
+ long nowTime = System.currentTimeMillis();
82
+ aggregator.startController(controller, nowTime);
83
+ assertEquals("Verify speed limit for one controller.", 10, aggregator.getSpeedLimitForController(controller));
84
+ aggregator.startController(controller, nowTime);
85
+ assertEquals("Verify speed limit for two controllers.", 10 / 2, aggregator.getSpeedLimitForController(controller));
86
+ }
87
+
88
+ @Test
89
+ public void testCheckProgress() throws Exception {
90
+ SpeedometerSpeedAggregator aggregator = new SpeedometerSpeedAggregator();
91
+ long initPeriodTime = System.currentTimeMillis();
92
+ final long startPeriodTime = initPeriodTime + 2;
93
+ aggregator.startController(controller, initPeriodTime);
94
+ aggregator.checkProgress(initPeriodTime, 1);
95
+ aggregator.checkProgress(startPeriodTime, 1);
96
+ aggregator.checkProgress(startPeriodTime, 1); // This call should not affect anything.
97
+
98
+ new Verifications() {{
99
+ controller.renewPeriod(); times = 1;
100
+ controller.getTotalBytes(); times = 1;
101
+ controller.getPeriodBytesPerSec(startPeriodTime); times = 1;
102
+ }};
103
+ }
104
+
105
+ @Test
106
+ public void testCheckProgressIsDisabled() throws Exception {
107
+ SpeedometerSpeedAggregator aggregator = new SpeedometerSpeedAggregator();
108
+ long initPeriodTime = System.currentTimeMillis();
109
+ final long startPeriodTime = initPeriodTime + 2;
110
+ aggregator.startController(controller, initPeriodTime);
111
+ aggregator.checkProgress(initPeriodTime, 0);
112
+ aggregator.checkProgress(startPeriodTime, 0);
113
+
114
+ new Verifications() {{
115
+ controller.renewPeriod(); times = 0;
116
+ controller.getTotalBytes(); times = 0;
117
+ controller.getPeriodBytesPerSec(startPeriodTime); times = 0;
118
+ }};
119
+ }
120
+ }
@@ -0,0 +1,114 @@
1
+ package org.embulk.filter;
2
+
3
+ import static org.junit.Assert.assertEquals;
4
+ import static org.junit.Assert.assertTrue;
5
+ import mockit.Mocked;
6
+ import mockit.NonStrictExpectations;
7
+ import mockit.Verifications;
8
+
9
+ import org.embulk.filter.SpeedometerFilterPlugin.PluginTask;
10
+ import org.junit.Test;
11
+
12
+ public class TestSpeedometerSpeedController {
13
+ @Mocked
14
+ SpeedometerSpeedAggregator aggregator;
15
+
16
+ @Mocked
17
+ PluginTask task;
18
+
19
+ SpeedometerSpeedController controller;
20
+
21
+ @Test
22
+ public void testSpeedometerSpeedController() {
23
+ new NonStrictExpectations() {{
24
+ task.getSpeedLimit(); result = 1L;
25
+ task.getMaxSleepMillisec(); result = 2;
26
+ task.getLogIntervalSeconds(); result = 3;
27
+ }};
28
+
29
+ controller = new SpeedometerSpeedController(task, aggregator);
30
+ assertEquals("Verify aggregator is set.", aggregator, controller.getAggregator());
31
+ assertEquals("Verify sleepdLimit is set.", 1, controller.getSpeedLimit());
32
+ assertEquals("Verify maxSleepMillisec is set.", 2, controller.getMaxSleepMillisec());
33
+ assertEquals("Verify logIntervalSeconds is set.", 3 * 1000, controller.getLogIntervalMillisec());
34
+
35
+ new Verifications() {{
36
+ task.getSpeedLimit(); times = 1;
37
+ task.getMaxSleepMillisec(); times = 1;
38
+ task.getLogIntervalSeconds(); times = 1;
39
+ }};
40
+ }
41
+
42
+ @Test
43
+ public void testStop() {
44
+ controller = new SpeedometerSpeedController(task, aggregator);
45
+ controller.stop();
46
+
47
+ new Verifications() {{
48
+ aggregator.stopController(controller); times = 1;
49
+ }};
50
+ }
51
+
52
+ @Test
53
+ public void testGetTotalBytes() {
54
+ new NonStrictExpectations() {{
55
+ task.getSpeedLimit(); result = 1L;
56
+ task.getMaxSleepMillisec(); result = 2;
57
+ task.getLogIntervalSeconds(); result = 3;
58
+ aggregator.getSpeedLimitForController((SpeedometerSpeedController)any); result = 10000;
59
+ }};
60
+ long nowTime = System.currentTimeMillis();
61
+ int newDataSize = 3;
62
+
63
+ controller = new SpeedometerSpeedController(task, aggregator);
64
+ controller.checkSpeedLimit(nowTime, newDataSize);
65
+ assertEquals("Verify total bytes", newDataSize, controller.getTotalBytes());
66
+ controller.checkSpeedLimit(nowTime + 1, newDataSize);
67
+ assertEquals("Verify total bytes", newDataSize * 2, controller.getTotalBytes());
68
+ }
69
+
70
+ @Test
71
+ public void testGetPeriodBytesPerSec() {
72
+ new NonStrictExpectations() {{
73
+ task.getSpeedLimit(); result = 1L;
74
+ task.getMaxSleepMillisec(); result = 2;
75
+ task.getLogIntervalSeconds(); result = 3;
76
+ aggregator.getSpeedLimitForController((SpeedometerSpeedController)any); result = 10000;
77
+ }};
78
+ long nowTime = System.currentTimeMillis();
79
+ int newDataSize = 3;
80
+
81
+ controller = new SpeedometerSpeedController(task, aggregator);
82
+ controller.checkSpeedLimit(nowTime, newDataSize);
83
+ assertTrue("Verify total bytes", controller.getPeriodBytesPerSec(System.currentTimeMillis()) > 0);
84
+ }
85
+
86
+ @Test
87
+ public void testCheckSpeedLimit() {
88
+ new NonStrictExpectations() {{
89
+ task.getSpeedLimit(); result = 1L;
90
+ task.getMaxSleepMillisec(); result = 2;
91
+ task.getLogIntervalSeconds(); result = 3;
92
+ aggregator.getSpeedLimitForController((SpeedometerSpeedController)any); result = 10000;
93
+ }};
94
+ long nowTime = System.currentTimeMillis();
95
+ int newDataSize = 3;
96
+
97
+ controller = new SpeedometerSpeedController(task, aggregator);
98
+ controller.checkSpeedLimit(nowTime, newDataSize);
99
+
100
+ new Verifications() {{
101
+ aggregator.startController(controller, anyLong); times = 1;
102
+ aggregator.checkProgress(anyLong, 3 * 1000); times = 1;
103
+ aggregator.getSpeedLimitForController(controller); times = 1;
104
+ }};
105
+ }
106
+
107
+ @Test
108
+ public void testRenewPeriod() {
109
+ controller = new SpeedometerSpeedController(task, aggregator);
110
+ controller.renewPeriod();
111
+ assertTrue("Verify renewPeriod flag is set.", controller.isRenewPeriodSet());
112
+ }
113
+
114
+ }
@@ -0,0 +1,41 @@
1
+ package org.embulk.filter;
2
+
3
+ import static org.junit.Assert.assertEquals;
4
+
5
+ import org.junit.Test;
6
+
7
+ public class TestSpeedometerUtil {
8
+
9
+ @Test
10
+ public void testToNumberText() {
11
+ assertEquals("Verify 0 bytes", "0.0b", SpeedometerUtil.toNumberText(0));
12
+ assertEquals("Verify 0 bytes", "100b", SpeedometerUtil.toNumberText(100));
13
+ assertEquals("Verify 0 bytes", "1.0kb", SpeedometerUtil.toNumberText(1000L));
14
+ assertEquals("Verify 0 bytes", "100kb", SpeedometerUtil.toNumberText(1000L * 100));
15
+ assertEquals("Verify 0 bytes", "1.0mb", SpeedometerUtil.toNumberText(1000L * 1000));
16
+ assertEquals("Verify 0 bytes", "100mb", SpeedometerUtil.toNumberText(1000L * 1000 * 100));
17
+ assertEquals("Verify 0 bytes", "1.0gb", SpeedometerUtil.toNumberText(1000L * 1000 * 1000));
18
+ assertEquals("Verify 0 bytes", "100gb", SpeedometerUtil.toNumberText(1000L * 1000 * 1000 * 100));
19
+ assertEquals("Verify 0 bytes", "1.0tb", SpeedometerUtil.toNumberText(1000L * 1000 * 1000 * 1000));
20
+ assertEquals("Verify 0 bytes", "100tb", SpeedometerUtil.toNumberText(1000L * 1000 * 1000 * 1000 * 100));
21
+ assertEquals("Verify 0 bytes", "1.0pb", SpeedometerUtil.toNumberText(1000L * 1000 * 1000 * 1000 * 1000));
22
+ assertEquals("Verify 0 bytes", "100pb", SpeedometerUtil.toNumberText(1000L * 1000 * 1000 * 1000 * 1000 * 100));
23
+ assertEquals("Verify 0 bytes", "1.0eb", SpeedometerUtil.toNumberText(1000L * 1000 * 1000 * 1000 * 1000 * 1000));
24
+ }
25
+
26
+ @Test
27
+ public void testToTimeText() {
28
+ assertEquals("Verify 0 msec", "0.00", SpeedometerUtil.toTimeText(0));
29
+ assertEquals("Verify 1 sec", "1.00", SpeedometerUtil.toTimeText(1000));
30
+ assertEquals("Verify 9 secs", "9.00", SpeedometerUtil.toTimeText(9000));
31
+ assertEquals("Verify 10 secs", "10.0", SpeedometerUtil.toTimeText(10000));
32
+ assertEquals("Verify 1 min", "1:00", SpeedometerUtil.toTimeText(60000));
33
+ assertEquals("Verify 10 mins 10 secs", "10:01", SpeedometerUtil.toTimeText(60000 * 10 + 1000));
34
+ assertEquals("Verify 1 hour 10 mins 10 secs", "1:10:01", SpeedometerUtil.toTimeText(60000 * 10 + 1000 + 3600 * 1000));
35
+ assertEquals("Verify 10 hours 10 mins 10 secs", "10:10:01", SpeedometerUtil.toTimeText(60000 * 10 + 1000 + 10 * 3600 * 1000));
36
+ assertEquals("Verify 23 hours 10 mins 10 secs", "23:10:01", SpeedometerUtil.toTimeText(60000 * 10 + 1000 + 23 * 3600 * 1000));
37
+ assertEquals("Verify 1.0 days", "1.0 days", SpeedometerUtil.toTimeText(60L * 60 * 24 * 1000));
38
+ assertEquals("Verify 100 days", "100 days", SpeedometerUtil.toTimeText(60L * 60 * 24 * 1000 * 100));
39
+ }
40
+
41
+ }