1 package ch.qos.logback.classic.spi;
2
3 import static org.junit.Assert.assertTrue;
4 import static org.junit.Assert.fail;
5
6 import java.io.IOException;
7 import java.io.ObjectOutputStream;
8
9 import org.junit.After;
10 import org.junit.Before;
11 import org.junit.Test;
12 import org.slf4j.helpers.BogoPerf;
13
14 import ch.qos.logback.classic.net.NOPOutputStream;
15 import ch.qos.logback.classic.net.testObjectBuilders.Builder;
16 import ch.qos.logback.classic.net.testObjectBuilders.LoggingEventWithParametersBuilder;
17 import ch.qos.logback.classic.net.testObjectBuilders.TrivialLoggingEventBuilder;
18 import ch.qos.logback.core.CoreConstants;
19 import ch.qos.logback.core.testUtil.Env;
20
21 public class LoggingEventSerializationPerfTest {
22
23 static int LOOP_LEN = 10 * 1000;
24
25 NOPOutputStream noos = new NOPOutputStream();
26 ObjectOutputStream oos;
27
28 @Before
29 public void setUp() throws Exception {
30 oos = new ObjectOutputStream(noos);
31
32 }
33
34 @After
35 public void tearDown() throws Exception {
36 }
37
38 double doLoop(Builder builder, int loopLen) {
39 long start = System.nanoTime();
40 int resetCounter = 0;
41 for (int i = 0; i < loopLen; i++) {
42 try {
43 oos.writeObject(builder.build(i));
44 oos.flush();
45 if (++resetCounter >= CoreConstants.OOS_RESET_FREQUENCY) {
46 oos.reset();
47 resetCounter = 0;
48 }
49
50 } catch (IOException ex) {
51 fail(ex.getMessage());
52 }
53 }
54 long end = System.nanoTime();
55 return (end - start) / (1.0d * loopLen);
56 }
57
58 @Test
59 public void testPerformance() {
60 if (Env.isLinux()) {
61 return;
62 }
63 TrivialLoggingEventBuilder builder = new TrivialLoggingEventBuilder();
64
65 doLoop(builder, LOOP_LEN);
66 noos.reset();
67 double avg = doLoop(builder, LOOP_LEN);
68
69
70 long actualSize = (long) (noos.size()/(1024*1.1d));
71 double baosSizeLimit = 500;
72
73 assertTrue("baos size" + actualSize + " should be less than "
74 + baosSizeLimit, baosSizeLimit > actualSize);
75
76
77 long referencePerf = 5000;
78 BogoPerf.assertDuration(avg, referencePerf, CoreConstants.REFERENCE_BIPS);
79 }
80
81
82 @Test
83 public void testPerformanceWithParameters() {
84 if (Env.isLinux()) {
85 return;
86 }
87 LoggingEventWithParametersBuilder builder = new LoggingEventWithParametersBuilder();
88
89 doLoop(builder, LOOP_LEN);
90 noos.reset();
91 double avg = doLoop(builder, LOOP_LEN);
92
93 long actualSize = (long) (noos.size()/(1024*1.1d));
94
95 double baosSizeLimit = 1300;
96 assertTrue("actualSize " + actualSize + " should be less than "
97 + baosSizeLimit, baosSizeLimit > actualSize);
98
99
100 long referencePerf = 7000;
101 BogoPerf.assertDuration(avg, referencePerf, CoreConstants.REFERENCE_BIPS);
102 }
103 }