blob: 7a12eb6eb8220a96ae3c9b66e64fc51fecf28ae5 [file] [log] [blame]
Anders Lewisac4f4b42017-08-08 18:29:51 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <langinfo.h>
18#include <locale.h>
19#include <stdlib.h>
20
21#include <benchmark/benchmark.h>
22#include "util.h"
23
24static void BM_stdlib_malloc_free(benchmark::State& state) {
25 const size_t nbytes = state.range(0);
26
27 void* c;
28 while (state.KeepRunning()) {
29 c = malloc(nbytes);
30 free(c);
31 }
32
33 state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes));
34}
35BIONIC_BENCHMARK(BM_stdlib_malloc_free);
36
37static void BM_stdlib_mbstowcs(benchmark::State& state) {
38 const size_t buf_alignment = state.range(0);
39 const size_t widebuf_alignment = state.range(1);
40
41 std::vector<char> buf;
42 std::vector<wchar_t> widebuf;
43
44 setlocale(LC_CTYPE, "C.UTF-8")
45 || setlocale(LC_CTYPE, "en_US.UTF-8")
46 || setlocale(LC_CTYPE, "en_GB.UTF-8")
47 || setlocale(LC_CTYPE, "en.UTF-8")
48 || setlocale(LC_CTYPE, "de_DE-8")
49 || setlocale(LC_CTYPE, "fr_FR-8");
50 if (strcmp(nl_langinfo(CODESET), "UTF-8")) abort();
51
52 char* buf_aligned = GetAlignedPtr(&buf, buf_alignment, 500000);
53 wchar_t* widebuf_aligned = GetAlignedPtr(&widebuf, widebuf_alignment, 500000);
54 size_t i, j, k, l;
55 l = 0;
56 for (i=0xc3; i<0xe0; i++)
57 for (j=0x80; j<0xc0; j++)
58 buf[l++] = i, buf[l++] = j;
59 for (i=0xe1; i<0xed; i++)
60 for (j=0x80; j<0xc0; j++)
61 for (k=0x80; k<0xc0; k++)
62 buf[l++] = i, buf[l++] = j, buf[l++] = k;
63 for (i=0xf1; i<0xf4; i++)
64 for (j=0x80; j<0xc0; j++)
65 for (k=0x80; k<0xc0; k++)
66 buf[l++] = i, buf[l++] = j, buf[l++] = 0x80, buf[l++] = k;
67 buf[l++] = 0;
68
69 volatile size_t c __attribute__((unused)) = 0;
70 while (state.KeepRunning()) {
71 c = mbstowcs(widebuf_aligned, buf_aligned, 500000);
72 }
73
74 state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(500000));
75}
76BIONIC_BENCHMARK(BM_stdlib_mbstowcs);
77
78static void BM_stdlib_mbrtowc(benchmark::State& state) {
79 const size_t buf_alignment = state.range(0);
80
81 std::vector<char> buf;
82
83 setlocale(LC_CTYPE, "C.UTF-8")
84 || setlocale(LC_CTYPE, "en_US.UTF-8")
85 || setlocale(LC_CTYPE, "en_GB.UTF-8")
86 || setlocale(LC_CTYPE, "en.UTF-8")
87 || setlocale(LC_CTYPE, "de_DE-8")
88 || setlocale(LC_CTYPE, "fr_FR-8");
89 if (strcmp(nl_langinfo(CODESET), "UTF-8")) abort();
90
91 char* buf_aligned = GetAlignedPtr(&buf, buf_alignment, 500000);
92 size_t i, j, k, l;
93 l = 0;
94 for (i=0xc3; i<0xe0; i++)
95 for (j=0x80; j<0xc0; j++)
96 buf[l++] = i, buf[l++] = j;
97 for (i=0xe1; i<0xed; i++)
98 for (j=0x80; j<0xc0; j++)
99 for (k=0x80; k<0xc0; k++)
100 buf[l++] = i, buf[l++] = j, buf[l++] = k;
101 for (i=0xf1; i<0xf4; i++)
102 for (j=0x80; j<0xc0; j++)
103 for (k=0x80; k<0xc0; k++)
104 buf[l++] = i, buf[l++] = j, buf[l++] = 0x80, buf[l++] = k;
105 buf[l++] = 0;
106
107 wchar_t wc = 0;
108 while (state.KeepRunning()) {
109 for (j = 0; buf_aligned[j]; j+=mbrtowc(&wc, buf_aligned + j, 4, NULL)) {
110 }
111 }
112
113 state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(500000));
114}
115BIONIC_BENCHMARK(BM_stdlib_mbrtowc);