1 // A simple test for performance of the ACEXML_escape_string() function
3 #include "ACEXML/common/XML_Util.h"
5 #include "ace/OS_main.h"
6 #include "ace/Time_Value.h"
7 #include "ace/OS_NS_sys_time.h"
8 #include "ace/Log_Msg.h"
10 const int MAX_ITERATIONS
= 100 * 1000;
11 const int NUM_TEST_STRS
= 6;
13 static bool is_escaped(const ACEXML_String
& s
)
15 if (s
[0] != ACE_TEXT('&'))
17 if (s
[s
.length() - 1] != ACE_TEXT(';'))
22 static int run_tests(ACEXML_String test_strings
[NUM_TEST_STRS
], int iterations
)
24 // Test 1 - Escape the strings using a new temporary string each iteration.
25 ACE_Time_Value start
= ACE_OS::gettimeofday();
27 for (i
= 0; i
< iterations
; ++i
)
29 ACEXML_String tmp
= ACEXML_escape_string(test_strings
[i
% NUM_TEST_STRS
]);
30 if (! is_escaped(tmp
))
32 ACE_ERROR((LM_ERROR
, "Error: Failed to escape string\n"));
36 ACE_DEBUG((LM_DEBUG
, "Test1 took %dms\n", (ACE_OS::gettimeofday() - start
).msec()));
38 // Test 2 - Escape the strings using a shared temporary string. This shouldn't
39 // be any faster than Test 1 as long as the compiler has return value optimization.
41 start
= ACE_OS::gettimeofday();
42 for (i
= 0; i
< iterations
; ++i
)
44 tmp
= ACEXML_escape_string(test_strings
[i
% NUM_TEST_STRS
]);
45 if (! is_escaped(tmp
))
47 ACE_ERROR((LM_ERROR
, "Error: Failed to escape string\n"));
51 ACE_DEBUG((LM_DEBUG
, "Test2 took %dms\n", (ACE_OS::gettimeofday() - start
).msec()));
53 // Test 3 - Escape the strings using a shared temporary string. This time, we use
54 // the alternate form of ACEXML_escape_string() so that our temporary buffer is reused.
56 start
= ACE_OS::gettimeofday();
57 for (i
= 0; i
< iterations
; ++i
)
59 ACEXML_escape_string(test_strings
[i
% NUM_TEST_STRS
], tmp
);
60 if (! is_escaped(tmp
))
62 ACE_ERROR((LM_ERROR
, "Error: Failed to escape string\n"));
66 ACE_DEBUG((LM_DEBUG
, "Test3 took %dms\n", (ACE_OS::gettimeofday() - start
).msec()));
68 // Test 4 - Same as Test 3, except that the tmp buffer shouldn't have to resize.
69 start
= ACE_OS::gettimeofday();
70 for (i
= 0; i
< iterations
; ++i
)
72 ACEXML_escape_string(test_strings
[i
% NUM_TEST_STRS
], tmp
);
73 if (! is_escaped(tmp
))
75 ACE_ERROR((LM_ERROR
, "Error: Failed to escape string\n"));
79 ACE_DEBUG((LM_DEBUG
, "Test4 took %dms\n", (ACE_OS::gettimeofday() - start
).msec()));
83 int ACE_TMAIN (int, ACE_TCHAR
*[])
85 ACEXML_String test_strings
[NUM_TEST_STRS
] = {
86 ACE_TEXT("\"xxxxx\"xxxxxxxx xx\"xxxxxx xxxxxx\"xxxxxxxxxx xxxxxxxx\"xxxxxx\""),
87 ACE_TEXT("'xxxxx\'xxxxxxxx' xxxxxxxx xx'xxxxxxxx'xxxxxx xxxxxxx'xxxxxxx'"),
88 ACE_TEXT("&xxxx&xxxxxxxxx &xxxxxxxx xxxxx&xxxxxxxxxxx xxxx&xxxxxxxxxx&"),
89 ACE_TEXT(">xx>xxxxxxxxxxx >xxxxxxxx xxxxx>xxxxxxxxxxx xxxxx>xxxxxxxxx>"),
90 ACE_TEXT("<xxxxx<xxxxxxxx xxxxxxxx <xxxxxxxxxxxxxxx<x xxxxxxxxxxxxxx<"),
91 ACE_TEXT("&xxxx\"xxxxxxx&xx xxx'xxxxx xx<xxxxxxx>xxxxxxx xx\"xxxxxxxxxxxx>"),
94 if (run_tests(test_strings
, MAX_ITERATIONS
) != 0)
97 ACE_DEBUG((LM_DEBUG
, "Rerun tests with larger strings\n"));
98 for (int i
= 0; i
< NUM_TEST_STRS
; ++i
)
100 for (int j
= 0; j
< 5; ++j
)
102 test_strings
[i
] += test_strings
[i
];
106 if (run_tests(test_strings
, MAX_ITERATIONS
/ 10) != 0)