debian: apply security fixes from 2.24.1
[git/debian.git] / debian / patches / 0011-test-path-utils-offer-to-run-a-protectNTFS-protectHFS.diff
blobff6972dbe17e6eeccef7edbf8e247f2b18f3a4ac
1 From f152ec4944b7fa21bf563996a97d4e59a5be2bf2 Mon Sep 17 00:00:00 2001
2 From: Garima Singh <garima.singh@microsoft.com>
3 Date: Wed, 4 Sep 2019 13:36:39 -0400
4 Subject: test-path-utils: offer to run a protectNTFS/protectHFS benchmark
6 In preparation to flipping the default on `core.protectNTFS`, let's have
7 some way to measure the speed impact of this config setting reliably
8 (and for comparison, the `core.protectHFS` config setting).
10 For now, this is a manual performance benchmark:
12 ./t/helper/test-path-utils protect_ntfs_hfs [arguments...]
14 where the arguments are an optional number of file names to test with,
15 optionally followed by minimum and maximum length of the random file
16 names. The default values are one million, 3 and 20, respectively.
18 Just like `sqrti()` in `bisect.c`, we introduce a very simple function
19 to approximation the square root of a given value, in order to avoid
20 having to introduce the first user of `<math.h>` in Git's source code.
22 Note: this is _not_ implemented as a Unix shell script in t/perf/
23 because we really care about _very_ precise timings here, and Unix shell
24 scripts are simply unsuited for precise and consistent benchmarking.
26 Signed-off-by: Garima Singh <garima.singh@microsoft.com>
27 Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
28 (cherry picked from commit a62f9d1ace8c6556cbc1bb7df69eff0a0bb9e774)
29 Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
30 ---
31 t/helper/test-path-utils.c | 96 ++++++++++++++++++++++++++++++++++++++
32 1 file changed, 96 insertions(+)
34 diff --git a/t/helper/test-path-utils.c b/t/helper/test-path-utils.c
35 index 5d543ad21f..b97b15fbef 100644
36 --- a/t/helper/test-path-utils.c
37 +++ b/t/helper/test-path-utils.c
38 @@ -185,6 +185,99 @@ static int cmp_by_st_size(const void *a, const void *b)
39 return x > y ? -1 : (x < y ? +1 : 0);
42 +/*
43 + * A very simple, reproducible pseudo-random generator. Copied from
44 + * `test-genrandom.c`.
45 + */
46 +static uint64_t my_random_value = 1234;
48 +static uint64_t my_random(void)
50 + my_random_value = my_random_value * 1103515245 + 12345;
51 + return my_random_value;
54 +/*
55 + * A fast approximation of the square root, without requiring math.h.
56 + *
57 + * It uses Newton's method to approximate the solution of 0 = x^2 - value.
58 + */
59 +static double my_sqrt(double value)
61 + const double epsilon = 1e-6;
62 + double x = value;
64 + if (value == 0)
65 + return 0;
67 + for (;;) {
68 + double delta = (value / x - x) / 2;
69 + if (delta < epsilon && delta > -epsilon)
70 + return x + delta;
71 + x += delta;
72 + }
75 +static int protect_ntfs_hfs_benchmark(int argc, const char **argv)
77 + size_t i, j, nr, min_len = 3, max_len = 20;
78 + char **names;
79 + int repetitions = 15, file_mode = 0100644;
80 + uint64_t begin, end;
81 + double m[3][2], v[3][2];
82 + uint64_t cumul;
83 + double cumul2;
85 + if (argc > 1 && !strcmp(argv[1], "--with-symlink-mode")) {
86 + file_mode = 0120000;
87 + argc--;
88 + argv++;
89 + }
91 + nr = argc > 1 ? strtoul(argv[1], NULL, 0) : 1000000;
92 + ALLOC_ARRAY(names, nr);
94 + if (argc > 2) {
95 + min_len = strtoul(argv[2], NULL, 0);
96 + if (argc > 3)
97 + max_len = strtoul(argv[3], NULL, 0);
98 + if (min_len > max_len)
99 + die("min_len > max_len");
102 + for (i = 0; i < nr; i++) {
103 + size_t len = min_len + (my_random() % (max_len + 1 - min_len));
105 + names[i] = xmallocz(len);
106 + while (len > 0)
107 + names[i][--len] = (char)(' ' + (my_random() % ('\x7f' - ' ')));
110 + for (protect_ntfs = 0; protect_ntfs < 2; protect_ntfs++)
111 + for (protect_hfs = 0; protect_hfs < 2; protect_hfs++) {
112 + cumul = 0;
113 + cumul2 = 0;
114 + for (i = 0; i < repetitions; i++) {
115 + begin = getnanotime();
116 + for (j = 0; j < nr; j++)
117 + verify_path(names[j], file_mode);
118 + end = getnanotime();
119 + printf("protect_ntfs = %d, protect_hfs = %d: %lfms\n", protect_ntfs, protect_hfs, (end-begin) / (double)1e6);
120 + cumul += end - begin;
121 + cumul2 += (end - begin) * (end - begin);
123 + m[protect_ntfs][protect_hfs] = cumul / (double)repetitions;
124 + v[protect_ntfs][protect_hfs] = my_sqrt(cumul2 / (double)repetitions - m[protect_ntfs][protect_hfs] * m[protect_ntfs][protect_hfs]);
125 + printf("mean: %lfms, stddev: %lfms\n", m[protect_ntfs][protect_hfs] / (double)1e6, v[protect_ntfs][protect_hfs] / (double)1e6);
128 + for (protect_ntfs = 0; protect_ntfs < 2; protect_ntfs++)
129 + for (protect_hfs = 0; protect_hfs < 2; protect_hfs++)
130 + printf("ntfs=%d/hfs=%d: %lf%% slower\n", protect_ntfs, protect_hfs, (m[protect_ntfs][protect_hfs] - m[0][0]) * 100 / m[0][0]);
132 + return 0;
135 int cmd__path_utils(int argc, const char **argv)
137 if (argc == 3 && !strcmp(argv[1], "normalize_path_copy")) {
138 @@ -355,6 +448,9 @@ int cmd__path_utils(int argc, const char **argv)
139 return !!res;
142 + if (argc > 1 && !strcmp(argv[1], "protect_ntfs_hfs"))
143 + return !!protect_ntfs_hfs_benchmark(argc - 1, argv + 1);
145 fprintf(stderr, "%s: unknown function name: %s\n", argv[0],
146 argv[1] ? argv[1] : "(there was none)");
147 return 1;
149 2.24.0.393.g34dc348eaf