Initial commit - from Precise source
[freerdp-ubuntu-pcb-backport.git] / libfreerdp-utils / profiler.c
1 /**
2  * FreeRDP: A Remote Desktop Protocol Client
3  * Profiler Utils
4  *
5  * Copyright 2011 Stephen Erisman
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 #include <freerdp/utils/profiler.h>
21
22 PROFILER* profiler_create(char* name)
23 {
24         PROFILER* profiler;
25
26         profiler = (PROFILER*) xmalloc(sizeof(PROFILER));
27         
28         profiler->name = name;
29         profiler->stopwatch = stopwatch_create();
30
31         return profiler;
32 }
33
34 void profiler_free(PROFILER* profiler)
35 {       
36         stopwatch_free(profiler->stopwatch);
37         
38         xfree(profiler);
39 }
40
41 void profiler_enter(PROFILER* profiler)
42 {
43         stopwatch_start(profiler->stopwatch);
44 }
45
46 void profiler_exit(PROFILER* profiler)
47 {
48         stopwatch_stop(profiler->stopwatch);
49 }
50
51 void profiler_print_header()
52 {
53         printf("\n");
54         printf("                                             |-----------------------|\n" );
55         printf("                PROFILER                     |    elapsed seconds    |\n" );
56         printf("|--------------------------------------------|-----------------------|\n" );
57         printf("| code section                  | iterations |     total |      avg. |\n" );
58         printf("|-------------------------------|------------|-----------|-----------|\n" );
59 }
60
61 void profiler_print(PROFILER* profiler)
62 {
63         double elapsed_sec = stopwatch_get_elapsed_time_in_seconds(profiler->stopwatch);
64         double avg_sec = elapsed_sec / (double) profiler->stopwatch->count;
65         
66         printf("| %-30.30s| %'10lu | %'9f | %'9f |\n", profiler->name, profiler->stopwatch->count, elapsed_sec, avg_sec);
67 }
68
69 void profiler_print_footer()
70 {
71         printf("|--------------------------------------------------------------------|\n" );
72 }