Skip to content

Instantly share code, notes, and snippets.

@philwo
Last active August 7, 2020 13:56
Show Gist options
  • Save philwo/9619f39c7d92ca75805bad810c4604b4 to your computer and use it in GitHub Desktop.
Save philwo/9619f39c7d92ca75805bad810c4604b4 to your computer and use it in GitHub Desktop.
symlink benchmark tool
#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
//const char *target = "shortlink";
const char *target = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789foobartoolchainproviderconfiguration";
//const char *target = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567";
int main(int argc, char *argv[]) {
for (int shard=0; shard<64; shard++) {
char dirname[16];
snprintf(dirname, 16, "%d", shard);
if (mkdir(dirname, 0755) < 0) {
perror("mkdir");
return 1;
}
}
for (int i=0; i<160000; i++) {
char linkname[64];
snprintf(linkname, 64, "%d/%d", i % 64, i);
#if 0
int fd = open(linkname, O_CREAT | O_WRONLY | O_EXCL, 0777);
if (fd < 0) {
perror("open");
return 1;
}
if (write(fd, target, strlen(target)) < 0) {
perror("write");
return 1;
}
if (close(fd) < 0) {
perror("close");
return 1;
}
#else
if (symlink(target, linkname) < 0) {
perror("symlink");
return 1;
}
#endif
}
for (int shard=0; shard<64; shard++) {
char dirname[16];
snprintf(dirname, 16, "%d", shard);
DIR *dir = opendir(dirname);
if (dir == NULL) {
perror("opendir");
return 1;
}
struct dirent *next_file;
char filepath[256];
while ((next_file = readdir(dir)) != NULL) {
if (strcmp(next_file->d_name, ".") == 0 || strcmp(next_file->d_name, "..") == 0) {
continue;
}
if (unlinkat(dirfd(dir), next_file->d_name, 0) < 0) {
perror("unlinkat");
return 1;
}
}
if (closedir(dir) < 0) {
perror("closedir");
return 1;
}
if (rmdir(dirname) < 0) {
perror("rmdir");
return 1;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment