Skip to content

Instantly share code, notes, and snippets.

@carlosmn
Created January 31, 2012 00:37
Show Gist options
  • Save carlosmn/1707844 to your computer and use it in GitHub Desktop.
Save carlosmn/1707844 to your computer and use it in GitHub Desktop.
Small benchmark
#!/bin/bash
git ls-tree -r latest-messages4-tree | cut -d ' ' -f 3 | cut -f 1 | git cat-file --batch
#blobs=$(git ls-tree -r latest-messages4-tree | cut -d ' ' -f 3 | cut -f 1)
#
#rm -f /dev/shm/tmp
#for blob in $blobs
#do
# echo $blob >> /dev/shm/tmp
#done
#
#git cat-file --batch < /dev/shm/tmp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
static int count;
static int read_chat_messages(const char *dirname)
{
char fname[512], msg[2048];
struct dirent *dirent;
DIR *dirp;
off_t offset;
ssize_t ret;
int skips;
dirp = opendir(dirname);
if (dirp == NULL) {
fprintf(stderr, "no open %s\n", dirname);
perror("can't open dir");
return -1;
}
skips = 0;
while (1) {
int fd;
dirent = readdir(dirp);
if (dirent == NULL)
return -1;
if (skips < 2) {
skips++;
continue;
}
sprintf(fname, "%s/%s", dirname, dirent->d_name);
errno = 0;
fd = open(fname, O_RDONLY);
if (fd < 0) {
fprintf(stderr, "couldn't open %s\n", fname);
perror("bad open");
return -1;
}
offset = 0;
do {
ret = read(fd, msg + offset, sizeof(msg) - offset);
if (ret < 0) {
fprintf(stderr, "bad read of %s\n", fname);
perror("failure to read");
return -1;
}
offset += ret;
} while (ret > 0 && sizeof(msg) - offset);
count++;
if (count % 10 == 0)
printf("\rRead in %d files", count);
close(fd);
}
return 0;
}
/*
* Read in the last 1000 messages by calling checkout and reading the
* files in and printing it out.
*/
int main(int argc, char **argv)
{
int i, status, skips;
pid_t child;
DIR *dirp;
struct dirent *dirent = NULL;
char str[512];
char *git_argv[] = {"git", "checkout", "-f",
"refs/tags/latest-messages2", "--",
"channels/", NULL};
child = fork();
if (!child) {
execv("/usr/bin/git", git_argv);
return EXIT_FAILURE;
}
if (child < 0) {
perror("failed to fork git");
return EXIT_FAILURE;
}
wait(&status);
if (status != EXIT_SUCCESS) {
fprintf(stderr, "oh noes, git failed: %s\n", strerror(status));
return EXIT_FAILURE;
}
memset(str, 0x0, sizeof(str));
dirp = opendir("channels");
if (dirp == NULL) {
perror("failed to open channels/");
return EXIT_FAILURE;
}
skips = 0;
/* Read through the dirs in channels/ */
while (1) {
errno = 0;
dirent = readdir(dirp);
if (dirent == NULL)
break;
sprintf(str, "channels/%s", dirent->d_name);
printf("going for %s\n", str);
if (skips < 2) {
skips++;
continue;
}
if (read_chat_messages(str)) {
fprintf(stderr, "error reading\n");
return EXIT_FAILURE;
}
}
closedir(dirp);
puts("\ndone");
return EXIT_SUCCESS;
}
#!/bin/bash
# Create 1000 small files, put them in a tree and put that into git,
# then run some benchmarks on it.
mkdir -p channels/A
mkdir -p channels/B
for i in {1..500}
do
echo "$i: some message" > channels/A/$i.message
done
for i in {1..500}
do
echo "$i: some message" > channels/B/$i.message
done
git init -q
git add channels/
git commit -m "Committed 1000 files. Let's see you handle that"
#!/bin/bash
# Create 1000 small files in each of 30 directories
for i in {1..30}
do
mkdir -p channels/$i
for j in {1..1000}
do
cat message.sample > channels/$i/$j
done
done
git add channels/
git commit -m "Lots and lots of files"
#!/bin/bash
# Create 1000 small files in each of 30 directories
git rm -rf channels
for i in {1..10}
do
mkdir -p channels/$i
for j in {1..20}
do
cat message.sample > channels/$i/$j
done
done
git add channels/
git commit -m "Lots and lots of files"
#!/bin/bash
git rm -rf channels
for i in {1..10}
do
mkdir -p channels/$i
for j in {1..100}
do
cat message.sample > channels/$i/$j
done
done
git add channels/
git commit -m "Lots and lots of files"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <git2.h>
#define min(a,b) ((a) < (b) ? (a) : (b))
int main(int argc, char **argv)
{
git_repository *repo;
git_reference *ref;
git_tree *tree, *inner;
const git_tree_entry *entry;
const git_oid *oid;
int ret, i, count;
char msg[2048];
ret = git_repository_open(&repo, ".git");
if (ret < GIT_SUCCESS)
goto exit;
ret = git_reference_lookup(&ref, repo, "refs/tags/latest-messages3-tree");
if (ret < GIT_SUCCESS)
goto exit;
oid = git_reference_oid(ref);
ret = git_tree_lookup(&tree, repo, oid);
if (ret < GIT_SUCCESS)
goto exit;
count = 0;
/* The first loop goes through the channels */
for (i = 0; i < git_tree_entrycount(tree); i++) {
int j;
entry = git_tree_entry_byindex(tree, i);
ret = git_tree_lookup(&inner, repo, git_tree_entry_id(entry));
if (ret < GIT_SUCCESS)
goto exit;
/* And the inner loop through the messages */
for (j = 0; j < git_tree_entrycount(inner); j++) {
git_blob *blob;
entry = git_tree_entry_byindex(inner, j);
ret = git_blob_lookup(&blob, repo, git_tree_entry_id(entry));
if (ret < GIT_SUCCESS)
goto exit;
/* We have to copy it somewhere, so let's simulate this */
memcpy(msg, git_blob_rawcontent(blob), min(git_blob_rawsize(blob), sizeof(msg)));
count++;
if (count % 10 == 0)
printf("\rRead in %d files", count);
git_blob_free(blob);
}
}
exit:
puts("\ndone");
git_tree_free(tree);
git_reference_free(ref);
git_repository_free(repo);
if (ret < GIT_SUCCESS) {
fprintf(stderr, "failed to open repo %s\n", git_lasterror());
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
CFLAGS=-I/home/carlos/staging/libgit2/include -ggdb
all: checkout lowlevel
checkout: checkout.o
gcc -ggdb -o checkout checkout.o
lowlevel: lowlevel.o
gcc -ggdb -o lowlevel lowlevel.o -I/home/carlos/staging/libgit2/include -L/home/carlos/staging/libgit2/lib -lgit2
---
date: 4843681176817
user: random
channel: #smuxi
---
This is a sample longish message to test git as a sync mechanism
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
minim veniam, quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum
Sed ut perspiciatis unde omnis iste natus error sit voluptatem
accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae
ab illo inventore veritatis et quasi architecto beatae vitae dicta
sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit
aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos
qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui
dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed
quia non numquam eius modi tempora incidunt ut labore et dolore magnam
aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum
exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex
ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in
ea voluptate velit esse quam nihil molestiae consequatur, vel illum
qui dolorem eum fugiat quo voluptas nulla pariatur?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment