Change eina_counter_dump to return a string so it could work easily on windows.

SVN revision: 38055
This commit is contained in:
Cedric BAIL 2008-12-09 13:55:10 +00:00
parent af30207725
commit 24c1995b68
4 changed files with 68 additions and 14 deletions

View File

@ -49,7 +49,7 @@ EAPI void eina_counter_delete(Eina_Counter *counter);
EAPI void eina_counter_start(Eina_Counter *counter); EAPI void eina_counter_start(Eina_Counter *counter);
EAPI void eina_counter_stop(Eina_Counter *counter, int specimen); EAPI void eina_counter_stop(Eina_Counter *counter, int specimen);
EAPI void eina_counter_dump(Eina_Counter *counter, FILE *out); EAPI char *eina_counter_dump(Eina_Counter *counter);
/** /**
* @} * @}

View File

@ -335,6 +335,7 @@ eina_benchmark_run(Eina_Benchmark *bench)
EINA_INLIST_FOREACH(bench->runs, run) EINA_INLIST_FOREACH(bench->runs, run)
{ {
Eina_Counter *counter; Eina_Counter *counter;
char *result;
int i; int i;
snprintf(buffer, PATH_MAX, "bench_%s_%s.%s.data", bench->name, bench->run, run->name); snprintf(buffer, PATH_MAX, "bench_%s_%s.%s.data", bench->name, bench->run, run->name);
@ -356,7 +357,12 @@ eina_benchmark_run(Eina_Benchmark *bench)
eina_counter_stop(counter, i); eina_counter_stop(counter, i);
} }
eina_counter_dump(counter, current_data); result = eina_counter_dump(counter);
if (result)
{
fprintf(current_data, "%s", result);
free(result);
}
eina_counter_delete(counter); eina_counter_delete(counter);

View File

@ -95,6 +95,40 @@ _eina_counter_time_get(Eina_Nano_Time *tp)
} }
#endif /* _WIN2 */ #endif /* _WIN2 */
static char *
_eina_counter_asiprintf(char *base, int *position, const char *format, ...)
{
char *tmp, *result;
int size = 32;
int n;
va_list ap;
tmp = realloc(base, sizeof (char) * (*position + size));
if (!tmp) return base;
result = tmp;
while (1)
{
va_start(ap, format);
n = vsnprintf(result + *position, size, format, ap);
va_end(ap);
if (n > -1 && n < size)
{
/* If we always have glibc > 2.2, we could just return *position += n. */
*position += strlen(result + *position);
return result;
}
if (n > -1) size = n + 1;
else size <<= 1;
tmp = realloc(result, sizeof (char) * (*position + size));
if (!tmp) return result;
result = tmp;
}
}
/** /**
* @endcond * @endcond
*/ */
@ -379,8 +413,8 @@ eina_counter_stop(Eina_Counter *counter, int specimen)
/** /**
* @brief Dump the result of all clocks of a counter to a stream. * @brief Dump the result of all clocks of a counter to a stream.
* *
* @return A string with a summary of the test.
* @param counter The counter. * @param counter The counter.
* @param out The stream to dump the clocks.
* *
* This function dump all the valid clocks of @p counter to the stream * This function dump all the valid clocks of @p counter to the stream
* @p out. If @p counter or @p out are @c NULL, the functions exits * @p out. If @p counter or @p out are @c NULL, the functions exits
@ -393,14 +427,17 @@ eina_counter_stop(Eina_Counter *counter, int specimen)
* *
* The unit of time is the nanosecond. * The unit of time is the nanosecond.
*/ */
EAPI void EAPI char *
eina_counter_dump(Eina_Counter *counter, FILE *out) eina_counter_dump(Eina_Counter *counter)
{ {
Eina_Clock *clk; Eina_Clock *clk;
char *result = NULL;
int position = 0;
if (!counter || !out) return; if (!counter) return NULL;
fprintf(out, "# specimen\texperiment time\tstarting time\tending time\n"); result = _eina_counter_asiprintf(result, &position, "# specimen\texperiment time\tstarting time\tending time\n");
if (!result) return NULL;
EINA_INLIST_REVERSE_FOREACH(counter->clocks, clk) EINA_INLIST_REVERSE_FOREACH(counter->clocks, clk)
{ {
@ -420,12 +457,15 @@ eina_counter_dump(Eina_Counter *counter, FILE *out)
diff = (long int)(((long long int)(clk->end.QuadPart - clk->start.QuadPart) * 1000000000LL) / (long long int)_eina_counter_frequency.QuadPart); diff = (long int)(((long long int)(clk->end.QuadPart - clk->start.QuadPart) * 1000000000LL) / (long long int)_eina_counter_frequency.QuadPart);
#endif /* _WIN2 */ #endif /* _WIN2 */
fprintf(out, "%i\t%li\t%li\t%li\n", result = _eina_counter_asiprintf(result, &position,
clk->specimen, "%i\t%li\t%li\t%li\n",
diff, clk->specimen,
start, diff,
end); start,
end);
} }
return result;
} }
/** /**

View File

@ -41,6 +41,7 @@ END_TEST
START_TEST(eina_counter_simple) START_TEST(eina_counter_simple)
{ {
Eina_Counter *cnt; Eina_Counter *cnt;
char *dump;
int i; int i;
eina_counter_init(); eina_counter_init();
@ -62,7 +63,10 @@ START_TEST(eina_counter_simple)
eina_counter_stop(cnt, i); eina_counter_stop(cnt, i);
eina_counter_dump(cnt, stderr); dump = eina_counter_dump(cnt);
fail_if(!dump);
free(dump);
eina_counter_delete(cnt); eina_counter_delete(cnt);
@ -73,6 +77,7 @@ END_TEST
START_TEST(eina_counter_break) START_TEST(eina_counter_break)
{ {
Eina_Counter *cnt; Eina_Counter *cnt;
char *dump;
eina_counter_init(); eina_counter_init();
@ -83,7 +88,10 @@ START_TEST(eina_counter_break)
eina_counter_delete(cnt); eina_counter_delete(cnt);
eina_counter_dump(NULL, stderr); dump = eina_counter_dump(NULL);
fail_if(dump);
free(dump);
eina_counter_shutdown(); eina_counter_shutdown();
} }