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