Serenity Operating System
at hosted 208 lines 6.1 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <AK/ByteBuffer.h> 28#include <AK/String.h> 29#include <AK/Types.h> 30#include <AK/Vector.h> 31#include <LibCore/ElapsedTimer.h> 32#include <fcntl.h> 33#include <getopt.h> 34#include <stdio.h> 35#include <stdlib.h> 36#include <string.h> 37#include <sys/stat.h> 38#include <unistd.h> 39 40struct Result { 41 u64 write_bps; 42 u64 read_bps; 43}; 44 45Result average_result(const Vector<Result>& results) 46{ 47 Result average; 48 49 for (auto& res : results) { 50 average.write_bps += res.write_bps; 51 average.read_bps += res.read_bps; 52 } 53 54 average.write_bps /= results.size(); 55 average.read_bps /= results.size(); 56 57 return average; 58} 59 60void exit_with_usage(int rc) 61{ 62 fprintf(stderr, "Usage: disk_benchmark [-h] [-d directory] [-t time_per_benchmark] [-f file_size1,file_size2,...] [-b block_size1,block_size2,...]\n"); 63 exit(rc); 64} 65 66Result benchmark(const String& filename, int file_size, int block_size, ByteBuffer& buffer, bool allow_cache); 67 68int main(int argc, char** argv) 69{ 70 char* directory = strdup("."); 71 int time_per_benchmark = 10; 72 Vector<int> file_sizes; 73 Vector<int> block_sizes; 74 bool allow_cache = false; 75 76 int opt; 77 while ((opt = getopt(argc, argv, "chd:t:f:b:")) != -1) { 78 switch (opt) { 79 case 'h': 80 exit_with_usage(0); 81 break; 82 case 'c': 83 allow_cache = true; 84 break; 85 case 'd': 86 directory = strdup(optarg); 87 break; 88 case 't': 89 time_per_benchmark = atoi(optarg); 90 break; 91 case 'f': 92 for (auto size : String(optarg).split(',')) 93 file_sizes.append(atoi(size.characters())); 94 break; 95 case 'b': 96 for (auto size : String(optarg).split(',')) 97 block_sizes.append(atoi(size.characters())); 98 break; 99 } 100 } 101 102 if (file_sizes.size() == 0) { 103 file_sizes = { 131072, 262144, 524288, 1048576, 5242880 }; 104 } 105 if (block_sizes.size() == 0) { 106 block_sizes = { 8192, 32768, 65536 }; 107 } 108 109 umask(0644); 110 111 auto filename = String::format("%s/disk_benchmark.tmp", directory); 112 113 for (auto file_size : file_sizes) { 114 for (auto block_size : block_sizes) { 115 if (block_size > file_size) 116 continue; 117 118 auto buffer = ByteBuffer::create_uninitialized(block_size); 119 120 Vector<Result> results; 121 122 printf("Running: file_size=%d block_size=%d\n", file_size, block_size); 123 Core::ElapsedTimer timer; 124 timer.start(); 125 while (timer.elapsed() < time_per_benchmark * 1000) { 126 printf("."); 127 fflush(stdout); 128 results.append(benchmark(filename, file_size, block_size, buffer, allow_cache)); 129 usleep(100); 130 } 131 auto average = average_result(results); 132 printf("\nFinished: runs=%zu time=%dms write_bps=%llu read_bps=%llu\n", results.size(), timer.elapsed(), average.write_bps, average.read_bps); 133 134 sleep(1); 135 } 136 } 137 138 if (isatty(0)) { 139 printf("Press any key to exit...\n"); 140 fgetc(stdin); 141 } 142} 143 144Result benchmark(const String& filename, int file_size, int block_size, ByteBuffer& buffer, bool allow_cache) 145{ 146 int flags = O_CREAT | O_TRUNC | O_RDWR; 147 if (!allow_cache) 148 flags |= O_DIRECT; 149 150 int fd = open(filename.characters(), flags, 0644); 151 if (fd == -1) { 152 perror("open"); 153 exit(1); 154 } 155 156 auto cleanup_and_exit = [fd, filename]() { 157 close(fd); 158 unlink(filename.characters()); 159 exit(1); 160 }; 161 162 Result res; 163 164 Core::ElapsedTimer timer; 165 166 timer.start(); 167 int nwrote = 0; 168 for (int j = 0; j < file_size; j += block_size) { 169 int n = write(fd, buffer.data(), block_size); 170 if (n < 0) { 171 perror("write"); 172 cleanup_and_exit(); 173 } 174 nwrote += n; 175 } 176 177 res.write_bps = (u64)(timer.elapsed() ? (file_size / timer.elapsed()) : file_size) * 1000; 178 179 if (lseek(fd, 0, SEEK_SET) < 0) { 180 perror("lseek"); 181 cleanup_and_exit(); 182 } 183 184 timer.start(); 185 int nread = 0; 186 while (nread < file_size) { 187 int n = read(fd, buffer.data(), block_size); 188 if (n < 0) { 189 perror("read"); 190 cleanup_and_exit(); 191 } 192 nread += n; 193 } 194 195 res.read_bps = (u64)(timer.elapsed() ? (file_size / timer.elapsed()) : file_size) * 1000; 196 197 if (close(fd) != 0) { 198 perror("close"); 199 cleanup_and_exit(); 200 } 201 202 if (unlink(filename.characters()) != 0) { 203 perror("unlink"); 204 cleanup_and_exit(); 205 } 206 207 return res; 208}