1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.classic.net;
15
16 import java.io.IOException;
17 import java.io.ObjectOutputStream;
18 import java.net.Socket;
19
20 import org.junit.jupiter.api.AfterEach;
21 import org.junit.jupiter.api.Assertions;
22 import org.junit.jupiter.api.BeforeEach;
23 import org.junit.jupiter.api.Disabled;
24
25 import ch.qos.logback.classic.net.testObjectBuilders.Builder;
26 import ch.qos.logback.classic.net.testObjectBuilders.MinimalSer;
27 import ch.qos.logback.classic.net.testObjectBuilders.MinimalSerBuilder;
28 import ch.qos.logback.classic.net.testObjectBuilders.TrivialLoggingEventVOBuilder;
29 import ch.qos.logback.classic.spi.LoggingEventVO;
30 import org.junit.jupiter.api.Test;
31
32 @Disabled
33 public class SerializationPerfTest {
34
35 ObjectOutputStream oos;
36
37 int loopNumber = 10000;
38 int resetFrequency = 100;
39 int pauseFrequency = 10;
40 long pauseLengthInMillis = 20;
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60 boolean runWithExternalMockServer = true;
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 @BeforeEach
96 public void setUp() throws Exception {
97 if (runWithExternalMockServer) {
98 oos = new ObjectOutputStream(new Socket("localhost", ExternalMockSocketServer.PORT).getOutputStream());
99 } else {
100 oos = new ObjectOutputStream(new NOPOutputStream());
101 }
102 }
103
104 @AfterEach
105 public void tearDown() throws Exception {
106 oos.close();
107 oos = null;
108 }
109
110 public void runPerfTest(Builder<?> builder, String label) throws Exception {
111
112
113
114
115
116 int resetCounter = 0;
117 int pauseCounter = 0;
118 for (int i = 0; i < loopNumber; i++) {
119 try {
120 oos.writeObject(builder.build(i));
121 oos.flush();
122 if (++resetCounter >= resetFrequency) {
123 oos.reset();
124 resetCounter = 0;
125 }
126 if (++pauseCounter >= pauseFrequency) {
127 Thread.sleep(pauseLengthInMillis);
128 pauseCounter = 0;
129 }
130
131 } catch (IOException ex) {
132 Assertions.fail(ex.getMessage());
133 }
134 }
135
136
137 Long t1;
138 Long t2;
139 Long total = 0L;
140 resetCounter = 0;
141 pauseCounter = 0;
142
143 for (int i = 0; i < loopNumber; i++) {
144 try {
145 t1 = System.nanoTime();
146 oos.writeObject(builder.build(i));
147 oos.flush();
148 t2 = System.nanoTime();
149 total += (t2 - t1);
150 if (++resetCounter >= resetFrequency) {
151 oos.reset();
152 resetCounter = 0;
153 }
154 if (++pauseCounter >= pauseFrequency) {
155 Thread.sleep(pauseLengthInMillis);
156 pauseCounter = 0;
157 }
158 } catch (IOException ex) {
159 Assertions.fail(ex.getMessage());
160 }
161 }
162 total /= 1000;
163 System.out.println(
164 label + " : average time = " + total / loopNumber + " microsecs after " + loopNumber + " writes.");
165
166
167
168
169 }
170
171
172
173
174
175
176 @Test
177 public void testWithMinimalSerialization() throws Exception {
178 Builder<MinimalSer> builder = new MinimalSerBuilder();
179 runPerfTest(builder, "Minimal object serialization");
180 }
181
182
183
184
185
186
187 @Test
188 public void testWithSerialization() throws Exception {
189 Builder<LoggingEventVO> builder = new TrivialLoggingEventVOBuilder();
190 runPerfTest(builder, "LoggingEventVO object serialization");
191 }
192
193 }