1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        tests/benchmarks/log.cpp
3 // Purpose:     Log-related benchmarks
4 // Author:      Vadim Zeitlin
5 // Created:     2012-01-21
6 // Copyright:   (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
7 // Licence:     wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9 
10 #include "bench.h"
11 
12 #include "wx/log.h"
13 
14 // This class is used to check that the arguments of log functions are not
15 // evaluated.
16 struct NotCreated
17 {
NotCreatedNotCreated18     NotCreated() { wxAbort(); }
19 
AsStrNotCreated20     const char* AsStr() const { return "unreachable"; }
21 };
22 
23 // Temporarily change the log level to the given one.
24 class LogLevelSetter
25 {
26 public:
LogLevelSetter(wxLogLevel levelNew)27     LogLevelSetter(wxLogLevel levelNew)
28         : m_levelOld(wxLog::GetLogLevel())
29     {
30         wxLog::SetLogLevel(levelNew);
31     }
32 
~LogLevelSetter()33     ~LogLevelSetter()
34     {
35         wxLog::SetLogLevel(m_levelOld);
36     }
37 
38 private:
39     const wxLogLevel m_levelOld;
40 
41     wxDECLARE_NO_COPY_CLASS(LogLevelSetter);
42 };
43 
BENCHMARK_FUNC(LogDebugDisabled)44 BENCHMARK_FUNC(LogDebugDisabled)
45 {
46     LogLevelSetter level(wxLOG_Info);
47 
48     wxLogDebug("Ignored debug message: %s", NotCreated().AsStr());
49 
50     return true;
51 }
52 
BENCHMARK_FUNC(LogTraceDisabled)53 BENCHMARK_FUNC(LogTraceDisabled)
54 {
55     LogLevelSetter level(wxLOG_Info);
56 
57     wxLogTrace("", NotCreated().AsStr());
58 
59     return true;
60 }
61 
BENCHMARK_FUNC(LogTraceActive)62 BENCHMARK_FUNC(LogTraceActive)
63 {
64     static bool s_added = false;
65     if ( !s_added )
66     {
67         s_added = true;
68         wxLog::AddTraceMask("logbench");
69     }
70 
71     // Remove the actual logging overhead by simply throwing away the log
72     // messages.
73     class NulLog : public wxLog
74     {
75     public:
76         NulLog()
77             : m_logOld(wxLog::SetActiveTarget(this))
78         {
79         }
80 
81         virtual ~NulLog()
82         {
83             wxLog::SetActiveTarget(m_logOld);
84         }
85 
86     protected:
87         virtual void DoLogRecord(wxLogLevel,
88                                  const wxString&,
89                                  const wxLogRecordInfo&)
90         {
91         }
92 
93         wxLog* m_logOld;
94     };
95 
96     NulLog nulLog;
97 
98     wxLogTrace("logbench", "Trace message");
99 
100     return true;
101 }
102 
BENCHMARK_FUNC(LogTraceInactive)103 BENCHMARK_FUNC(LogTraceInactive)
104 {
105     wxLogTrace("bloordyblop", "Trace message");
106 
107     return true;
108 }
109