View Javadoc
1   package ch.qos.logback.classic.pattern;
2   
3   import java.io.IOException;
4   import java.nio.file.Files;
5   import java.nio.file.Paths;
6   import java.util.Arrays;
7   import java.util.List;
8   import java.util.stream.Collectors;
9   
10  import org.junit.jupiter.api.AfterEach;
11  import org.junit.jupiter.api.BeforeAll;
12  import org.junit.jupiter.api.BeforeEach;
13  import org.junit.jupiter.api.Disabled;
14  import org.junit.jupiter.api.Test;
15  import org.slf4j.Logger;
16  import org.slf4j.LoggerFactory;
17  
18  import ch.qos.logback.classic.ClassicTestConstants;
19  import ch.qos.logback.classic.LoggerContext;
20  import ch.qos.logback.classic.testUtil.Gaussian;
21  import ch.qos.logback.core.status.OnConsoleStatusListener;
22  
23  @Disabled
24  public class LoggerNameConverterPerfTest {
25  
26      static final String NAMES_FILE = ClassicTestConstants.INPUT_PREFIX + "fqcn.txt";
27  
28      static List<String> NAMES_LIST;
29  
30      static int SIZE;
31      static double MEAN;
32      static double DEVIATION;
33      static Gaussian G;
34  
35      LoggerContext loggerContext = new LoggerContext();
36      LoggerConverter loggerConverter = new LoggerConverter();
37  
38      LoggerNameOnlyLoggingEvent event = new LoggerNameOnlyLoggingEvent();
39  
40      Logger logger = LoggerFactory.getLogger(this.getClass());
41  
42      @BeforeAll
43      static public void loadClassNames() throws IOException {
44  
45          NAMES_LIST = Files.lines(Paths.get(NAMES_FILE)).collect(Collectors.toList());
46  
47          SIZE = NAMES_LIST.size();
48          MEAN = SIZE / 2;
49          DEVIATION = MEAN / 8;
50          G = new Gaussian(MEAN, DEVIATION);
51          System.out.println("names list size=" + SIZE);
52      }
53  
54      @BeforeEach
55      public void setUp() {
56          OnConsoleStatusListener ocsl = new OnConsoleStatusListener();
57          ocsl.setContext(loggerContext);
58          ocsl.start();
59          loggerContext.getStatusManager().add(ocsl);
60          loggerConverter.setOptionList(Arrays.asList("30"));
61          loggerConverter.setContext(loggerContext);
62          loggerConverter.start();
63      }
64  
65      @AfterEach
66      public void tearDown() {
67  
68      }
69  
70      @Test
71      public void measureAbbreviationPerf() {
72          for (int i = 0; i < 10 * 1000; i++) {
73              performAbbreviation();
74          }
75          for (int i = 0; i < 10 * 1000; i++) {
76              performAbbreviation();
77          }
78          final int runLength = 1000 * 1000;
79          System.out.println("Start measurements");
80          long start = System.nanoTime();
81          for (int i = 0; i < runLength; i++) {
82              performAbbreviation();
83          }
84          long end = System.nanoTime();
85          long diff = end - start;
86          double average = diff * 1.0D / runLength;
87          logger.atInfo().addArgument(average).log("Average = {} nanos");
88          int cacheMisses = loggerConverter.getCacheMisses();
89  
90          logger.atInfo().addArgument(cacheMisses).log("cacheMisses = {} ");
91          logger.atInfo().addArgument(runLength).log("total calls= = {} ");
92  
93          double cacheMissRate = loggerConverter.getCacheMissRate() * 100;
94          logger.atInfo().addArgument(cacheMissRate).log("cacheMiss rate %= {} ");
95  
96      }
97  
98      public void performAbbreviation() {
99          String fqn = getFQN();
100         event.setLoggerName(fqn);
101         loggerConverter.convert(event);
102     }
103 
104     private String getFQN() {
105         while (true) {
106             int index = (int) G.getGaussian();
107             if (index >= 0 && index < SIZE) {
108                 return NAMES_LIST.get(index);
109             } else {
110                 continue;
111             }
112         }
113     }
114 
115 }