• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

realm / realm-core / github_pull_request_312964

19 Feb 2025 07:31PM UTC coverage: 90.814% (-0.3%) from 91.119%
github_pull_request_312964

Pull #8071

Evergreen

web-flow
Bump serialize-javascript and mocha

Bumps [serialize-javascript](https://github.com/yahoo/serialize-javascript) to 6.0.2 and updates ancestor dependency [mocha](https://github.com/mochajs/mocha). These dependencies need to be updated together.


Updates `serialize-javascript` from 6.0.0 to 6.0.2
- [Release notes](https://github.com/yahoo/serialize-javascript/releases)
- [Commits](https://github.com/yahoo/serialize-javascript/compare/v6.0.0...v6.0.2)

Updates `mocha` from 10.2.0 to 10.8.2
- [Release notes](https://github.com/mochajs/mocha/releases)
- [Changelog](https://github.com/mochajs/mocha/blob/main/CHANGELOG.md)
- [Commits](https://github.com/mochajs/mocha/compare/v10.2.0...v10.8.2)

---
updated-dependencies:
- dependency-name: serialize-javascript
  dependency-type: indirect
- dependency-name: mocha
  dependency-type: direct:development
...

Signed-off-by: dependabot[bot] <support@github.com>
Pull Request #8071: Bump serialize-javascript and mocha

96552 of 179126 branches covered (53.9%)

212672 of 234185 relevant lines covered (90.81%)

3115802.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

70.37
/test/util/timer.cpp
1
/*************************************************************************
2
 *
3
 * Copyright 2016 Realm Inc.
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 **************************************************************************/
18

19
#include <stdexcept>
20
#include <iomanip>
21
#include <cmath>
22
#include <sstream>
23

24
#include <realm/util/features.h>
25
#include <realm/util/assert.hpp>
26

27
#if defined _WIN32
28
#include <windows.h>
29
#elif REALM_PLATFORM_APPLE
30
#include <sys/resource.h>
31
#include <mach/mach_time.h>
32
#include <sys/time.h>
33
#else
34
#include <ctime>
35
#endif
36

37
#include "timer.hpp"
38

39
using namespace realm::test_util;
40

41

42
#ifdef _WIN32
43

44

45
uint_fast64_t Timer::get_timer_ticks() const
46
{
47
    switch (m_type) {
48
        case type_RealTime:
49
            return GetTickCount64();
50
        case type_UserTime:
51
            FILETIME creation, exit, kernel, user;
52
            BOOL b = GetProcessTimes(GetCurrentProcess(), &creation, &exit, &kernel, &user);
53
            REALM_ASSERT_RELEASE(b);
54
            return (static_cast<uint_fast64_t>(user.dwHighDateTime) << 32) + user.dwLowDateTime;
55
    }
56

57
    return 0;
58
}
59

60
double Timer::calc_elapsed_seconds(uint_fast64_t ticks) const
61
{
62
    return ticks * 1E-3;
63
}
64

65

66
#elif REALM_PLATFORM_APPLE
67

68

69
uint_fast64_t Timer::get_timer_ticks() const
70
{
71
    switch (m_type) {
72
        case type_RealTime:
73
            return mach_absolute_time();
74
        case type_UserTime: {
75
            rusage ru;
76
            getrusage(RUSAGE_SELF, &ru);
77
            timeval tv;
78
            timeradd(&ru.ru_utime, &ru.ru_stime, &tv);
79
            return tv.tv_sec * 1000000 + tv.tv_usec;
80
        }
81
    }
82
}
83

84
namespace {
85

86
struct TimeBase {
87
    double m_seconds_per_tick;
88
    TimeBase()
89
    {
90
        mach_timebase_info_data_t info;
91
        kern_return_t err = mach_timebase_info(&info);
92
        if (err)
93
            throw std::runtime_error("Failed to get absolute time base");
94
        m_seconds_per_tick = (1E-9 * info.numer) / info.denom;
95
    }
96
};
97

98
} // anonymous namespace
99

100
double Timer::calc_elapsed_seconds(uint_fast64_t ticks) const
101
{
102
    static TimeBase base;
103
    switch (m_type) {
104
        case type_RealTime:
105
            return ticks * base.m_seconds_per_tick;
106
        case type_UserTime:
107
            return static_cast<double>(ticks) / 1000000;
108
    }
109
}
110

111

112
#else
113

114

115
namespace {
116

117
#ifdef CLOCK_MONOTONIC_RAW
118
const clockid_t real_time_clock_id = CLOCK_MONOTONIC_RAW; // (since Linux 2.6.28; Linux-specific)
119
#else
120
const clockid_t real_time_clock_id = CLOCK_MONOTONIC;
121
#endif
122

123
const clockid_t user_time_clock_id = CLOCK_PROCESS_CPUTIME_ID;
124

125

126
struct InitialTimes {
127
    timespec m_real, m_user;
128
    InitialTimes()
129
    {
4✔
130
        clock_gettime(real_time_clock_id, &m_real);
4✔
131
        clock_gettime(user_time_clock_id, &m_user);
4✔
132
    }
4✔
133
};
134

135
} // anonymous namespace
136

137
uint_fast64_t Timer::get_timer_ticks() const
138
{
9,212✔
139
    static const InitialTimes init_times;
9,212✔
140
    clockid_t clock_id = clockid_t();
9,212✔
141
    const timespec* init_time = nullptr;
9,212✔
142
    switch (m_type) {
9,212✔
143
        case type_RealTime:
9,212✔
144
            clock_id = real_time_clock_id;
9,212✔
145
            init_time = &init_times.m_real;
9,212✔
146
            break;
9,212✔
147
        case type_UserTime:
✔
148
            clock_id = user_time_clock_id;
×
149
            init_time = &init_times.m_user;
×
150
            break;
×
151
    }
9,212✔
152
    timespec time;
9,212✔
153
    clock_gettime(clock_id, &time);
9,212✔
154
    if (time.tv_nsec < init_time->tv_nsec) {
9,212✔
155
        time.tv_sec -= 1;
1,778✔
156
        time.tv_nsec += 1000000000;
1,778✔
157
    }
1,778✔
158
    return uint_fast64_t(time.tv_sec - init_time->tv_sec) * 1000000000 + (time.tv_nsec - init_time->tv_nsec);
9,212✔
159
}
9,212✔
160

161
double Timer::calc_elapsed_seconds(uint_fast64_t ticks) const
162
{
4,606✔
163
    return ticks * 1E-9;
4,606✔
164
}
4,606✔
165

166
#endif
167

168
std::string Timer::format(double seconds)
169
{
24✔
170
    std::ostringstream out;
24✔
171
    format(seconds, out);
24✔
172
    return out.str();
24✔
173
}
24✔
174

175
namespace {
176
// FIXME: This should be std::llround once we switch to >= C++11.
177
int64_t round_to_int64(double x)
178
{
58✔
179
    // FIXME: Assumes x >= 0.
180
    // FIXME: The adding of 0.5 is error-prone, see: http://blog.frama-c.com/index.php?post/2013/05/02/nearbyintf1
181
    return static_cast<int64_t>(x + 0.5);
58✔
182
}
58✔
183
} // namespace
184

185

186
void Timer::format(double seconds_float, std::ostream& out)
187
{
24✔
188
    int64_t rounded_minutes = round_to_int64(seconds_float / 60);
24✔
189
    if (rounded_minutes > 60) {
24✔
190
        // 1h0m -> inf
191
        int64_t hours = rounded_minutes / 60;
×
192
        int64_t minutes = rounded_minutes % 60;
×
193
        out << hours << "h" << minutes << "m";
×
194
    }
×
195
    else {
24✔
196
        int64_t rounded_seconds = round_to_int64(seconds_float);
24✔
197
        if (rounded_seconds > 60) {
24✔
198
            // 1m0s -> 59m59s
199
            int64_t minutes = rounded_seconds / 60;
14✔
200
            int64_t seconds = rounded_seconds % 60;
14✔
201
            out << minutes << "m" << seconds << "s";
14✔
202
        }
14✔
203
        else {
10✔
204
            int64_t rounded_centies = round_to_int64(seconds_float * 100);
10✔
205
            if (rounded_centies > 100) {
10✔
206
                // 1s -> 59.99s
207
                int64_t seconds = rounded_centies / 100;
10✔
208
                int64_t centies = rounded_centies % 100;
10✔
209
                out << seconds;
10✔
210
                if (centies > 0) {
10✔
211
                    out << '.' << std::setw(2) << std::setfill('0') << centies;
10✔
212
                }
10✔
213
                out << 's';
10✔
214
            }
10✔
215
            else {
×
216
                int64_t rounded_centi_ms = round_to_int64(seconds_float * 100000);
×
217
                if (rounded_centi_ms > 100) {
×
218
                    // 0.1ms -> 999.99ms
219
                    int64_t ms = rounded_centi_ms / 100;
×
220
                    int64_t centi_ms = rounded_centi_ms % 100;
×
221
                    out << ms;
×
222
                    if (centi_ms > 0) {
×
223
                        out << '.' << std::setw(2) << std::setfill('0') << centi_ms;
×
224
                    }
×
225
                    out << "ms";
×
226
                }
×
227
                else {
×
228
                    // 0 -> 999µs
229
                    int64_t us = round_to_int64(seconds_float * 1000000);
×
230
                    out << us << "us";
×
231
                }
×
232
            }
×
233
        }
10✔
234
    }
24✔
235
}
24✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc