Skip to content

Instantly share code, notes, and snippets.

@ongardie
Created February 25, 2014 21:50
Show Gist options
  • Save ongardie/9218611 to your computer and use it in GitHub Desktop.
Save ongardie/9218611 to your computer and use it in GitHub Desktop.
Microbenchmark that executes 1000 writes of the requested size (up to 8KB). This is a slight tweak on bench.c
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
const char* buf = "xghtczfphisyausfylavxgfefmxuszfnlespnfwupsniqdjxcucfgrpyrlenigmuqibhsdbimpwnyiubetvihqvxtlatyeuzdzhatzbstjlhjwtxcpngyedamwmecdsxtsxwrvkmychmgpaayjjyuzrbshyrvwekvvytdothhjhgqkuqcdnixokucnxgxipvrwnpkmltvhqjqclmcxdzvxhljtgnqyjqzxggspasccmgegogyibhfmapkswvnbaffsmurmmauqjwzpwuupsvkunzoqdohhuqrsndfcjfptwbxubocvbxkethgfhmpbjqhpmiiwjhbhzkotylzdbiektybpiyuefkbyxpwggpjbdebqviacasurgsqyonzybwsmftihjsdturodfdqdjkilxtapqxxdodkovbmmkhnhboitpxhbjljvkmagegsmetrxelakjdzewymjrvldzdkqfqlqjjryoafcjwsfdoihdxrxrerlpyrpxjiwksuvmsoiptpszhkjvbkyxbprsjsxzegcjgreiakvzrutnixnuqhqbcwgelarjanbljowcoueedlxyrjvnxctqgnekqisrtuxqxgjorcihicfgftslpwtmyzmumyxmanjiktktuwevbdkkceqadduhqpudkcetojnliflzjqxuxwgwmzkpzegufnhdmjyrjfjjwawoczwdfjztkjvgrkiccrsgswamrzdcmqtxqwjuoxgsxkbutuxngzhxcypkigelbnwtqjujgfcwzmgzgbgtvwcszogvoyrrevqtjopwzoizzsinzqlkukyczmfwmxdtnacudmozaishkydyqucsmmjuuhzphcaktsortlknquakpjvcjkwrtdfnpjiwknrccuqlghtbacicgfcjzidujclojbsokkgnatahvayvwnfphxocykgdctsqfthlcipwhrdfuqigoqzpmvodjgrrmiumdguvxbldjsizxgnudbrrxhnakhjgcbjzaqgdxpxethsidmlypkvqcspmrvlelaufyydfjlbxfnaopdrsxvpaopbfteulzmkrbppinkexilqlxagjhtbrqrhuhojhipblxcwervktuowqislpfrgwcvqyvzaysfohasfyycqacdnjmqjrhkulhttkvwddqigurhlfdcojxqcigzllrzdmktdggbqfczqypcvqktyxpibwtyzxslpieifhsmnbynyugaxmsglnbsassqyjirvcixlaccixtfymhdnjelnrsiqwzxezvqijdbasysscncdxxudbpqnlabyfnnuaujjjfzoozwydmurictczkkylzfkfcqoqdtjmmahpubhgyjhmxudppaqurgovkoakwlmwxtwruiumtfjlekwknrfywjlyfmulnbvsxvkmpemynxbxrwxxkcjdlqltbezrkcsspolzxxwvbogkhhrnfvjqpjannzevmskjmnzcyngxerudhedgshkvqemqnurfohshednqhchldoygyluhvimvdpfwjwvyrywdtuahcjnjrqoqijytjhcvmlqnttzsfbicjftxotrikrjfagaqfyyaoeheasiabxukxsazxcoongslknxlcompizdggjqgeisfmtehkultecrxbqqtxymievhvowqjxczqgstcnzmtfdmleplrhjvvufxqvwqiyaujvvktjdjhqxjvauoinrsnquachfdzndeflejqdztfkprkfiwnbsyspdzlznwznfmqibbuhyfeyupiymzuyzgtkrgybblcksubbqxztulzotnsnbklfskgwyspohfazskczrsozueluvzuqbfzprfibfjxhlbsagraflxwrxgtnaichuujfxozlfjsctmwaodzfjuajcizgcxfwllyyvgwgtgzvtixbcnqabgdjrxegswlkffdihgjcbjlytnifueyetbfqxrhxiplmryrirwdosxzfcmrsruzdmfjgxfsklytubkmquwcmgzxfvhbnbukkkttbapojryaxskgezcnggunlisrwkakoaevgkdbowjrekbzothysgpaqboeyzeahknwygozphczksmoildxxqftkdomjweybaxgaonluifilzquuwsycxycicvcdlvopbkrassnclkpqmkhpmmowlyfpvyzvtbajjnkceufkhaflsevjkhuukkfhbocrvzspeaxicafrxasnkmbzpioftjciizppviyzkhizxmyyjcbwntfgipimprtqgfodnlosnsewsfncowjcapgjtmhccevnsxwgshlsuauulfdywoohceokgbodwadpeerbnqzoemnlunbdsllsdkmhhoeizmzyzncftzkljdpfdpaldauzrkvymhfslpvthjdkuargphijfgajsiwdxhpehomwmwysdoaabjzyfichbsvfzxkrfuotfkkksfobmnmoufhvsuakdvtmtlibguqjvkjuhfkwidbviylfgornjjfzbvxjpyqkuhdffgpqfwvllavpxyyjqnsenqpprgmylvsvhvcbnimwemobsufgddamsleidwcpfmasdycnkwyywrofsfnormegqzbxptgtvvcnuftlvsypluksyapjpgqnqrjqvrzwnytlqioylfbtmpsewytovlfggldilwsfeyxikftjvnrlitwksvyoneeyqhgizdbbdohbwplbrlolppvdosgfwcvgcxortgnasjvdpitjkxxofrtecbgbhidnramgkbfpmkkbhugstivprkjsxjdquafwtcigzftiqxhevjpewftmbwkknjaoqatwujhkdcwxqabdhevrydcxfmhezhvqgubqxiqofxyninpbmejsfvwuomabqqlkrlphpwwdsdwkvsemlxbvkplzxfglddazgqrpqugivghcqyskdlkdbwvkrrrfqxealjtzfsvfktgurpkxvwrqikzsetajupzwkccvazbctjfgphyrsdrxbuircwrpeznhzafzmkbrjnjuohryfiprpchzhxdfijmnntfqvsmwbtscwtnlvbprgmrwuxcefxmmuykgckjnvzcvtcjlqmjawxpamwsortyhmgfkjkvbiodzvzkazuyulvjclpcythtserborfdfglmlyvqztnavmcoqbhdnxcxfhgrhzzmgxqinkckomjpavzmqfubujwcofhhobxidxwdxfvxzwwfcaxswhrahhznfmwcmshdiitvytluvcovrpkxpbmdwkeifbjnmyztrqogliwtzscpwzyxupszonwabhljyouplhjlyosnamkyyhtgearypcsqzyidggvhljplokkbmvnwrjrsxuryjknxdjmwuvclhjpmquuiypfkfghqcuuicjzlpqhzendbckdbcvtntkszcfbkeekwkbjiaxrwzttsunxtagftgqvonfchzfrnucfepripgooojmhbaeqsdwiqqlxiccjndxcsaugxleinddmrtlzsqmcksdrmwvmjgreyhxzaeipkhqsjxikldjbcefgadmcftthssppumgssoznntaihzogriugmdvykexwmkglhukoskyfzbrkymwkjuswbxhpfoxksacdlvnmrekbryiuvzdyidbofclsmmoqquoyipferrrbtglxsjebzaxedvificfufanbuegpqdvpzuavqcujnqcewkhihddqsixuotfvqxfvitvrbfnegmcqtckdvoiyvymtwnzxybnmxyaoawxqfjsgufcipulyjlwhmcgtwdxwwewqqjxdlfpctwunxqzsadajvnjfjhvctsfwwfhbitzjobmuvcpewxvbrnylrqviwerxnooznqcuspepcrqxlhlfrlknaisbcrirxyitnieidjjkxjethjqfgdkzehydadtozklfivgzcjamqzcvuyaenbaovygoloekmadabihdkmdrvqtulxnajayklvworkdlpjzfjvkqkkvnhqezcpbjohgqvypugvxpjbqzttuzuhxhwgdxzuqjnfsmewwospobbdjhmxniekiwojiojosdblnvdcxcargtltmpcghpeutjmbuazhpxljcbjehaqcabqrccmuejpeysssdcnjbjxjamzxwjjfrexcelujfidgsxfkrwtmiovemoaywmqylyfehbbqtapotpbtxspuumckktiqksrcjgpdrmewhncmavmzqkzfqoinqrwoskxhvkdtpjjryxxzmrnazgcnanyqbbceupygqffjwejsqeyywrnvftmmusxqptjkbucarpxepdsvfgachvlynmuxbcpaeudxjvbpmsaweacxcndlkkeilbcvamkjjmnwumbacopawirbwgamsbcucswyolwlijbrzxlamfvlxktalbcqsqewnagctiogtzeyzmmaroocglnpnvmyunjfwlbfvfujbdjsllojhfyixtyqlnjwjasxrczdplzcqlubxisvumtjlltxtazpmqgljnpfvorianydsvypfahjjddghwvpxjkhuyadzeqmgbxlzywtmzosvxhhzzfypnacxujlvcjaaetdnjvpxkdzhghfsxsryrrmayszdbzmgnbiuziaznjafuzbzwpajdbnjeoftbwbqhgqsswbzxxbfrqsvrvdkentaovgtgbkuhinbogdzjndlqvuxgisvmlpoedvuavcxficwbunggljmcgdghyirdpnnveejeldzgnzjxzbjokjvfzavgdijihohlvhqrprppdjnzxjygsihgafanegdaexxolqqbpnflghbkbnaxetljkvbywwifntlunzgdthhovuoosidgsqgwvrbgarmrxtondnbegyrkifbkbjfgvvgdvhowbqbsithedyltmoowyfrmybyykoixyyccxzazxtdhdohisxaxekbgqqnikqnqiaifgdsncrrdlxrpyhedkjqdewdeueeyimryhneqmnuvfefbyuuwwyyecusactwschysbzyhudmvigndmdqmejjlpbsqvnykzndmttvpoebcwkyuitarimyqqcswzdsdqasjgpppclvlzxcyuhaxqdrdtiwfhxirpxmpziecnzacnxeznmbctdfhqrenqdmsvxfmvjcaeswtcbdvbeitlahtlbmvhenrnisqygkydcqcefqwuhozymclkcdkkovnzbnhwtaundmtntbitgcynzmanhjdcoinatkqqvcgpnmrudumasiaztwdmdnbjomhirglbirnarrsdmavlilgsrxkxwcllctigtygcyxdrouxzuthgivzhhzwzrxalfryzcrgaxatkcvfqrmbvbqbsaaaxjgjzvusccupcshyxmzzxehsmxiyaeqhymfrwtzzuwsketrqsiebpzforfevtkanmymricmkoexctkxfdjxjgctawoqujttelgnhhmdxlqlisdgtzszfrrhkddxwjakthrkixfnumudtdrgmzyhzcmekciqmzwljelqssgwuwndnlpvdacamkbwxjwckslnkrwsivheumdyfzvflssfbrnaizujqfcstfzcwplyrhrdywseizhkqkeytdwvqyebatxunjptlthbjjvevpbfaxqawogozrwtrpxudlmnvkgkmakdrurkujgyisyhbztrzqslerizfggqnpnifdivjztnyahrlxiygaflspqxwaaulnwnvzikzdtevgatjzgapcyqndgjijczxbehvdrnridaowdxchezjduedfrghtsnaeukhlqwxfcknvfepnfewxdzijobzwfdwioonjhxdfflmcbpumcksntxkfsfqtxhcfwdznflxukecrkylwvsfusdubpbjgjowgrmmqiahkuurwiwlzutlnatgyfngnvmtyhuaryuujpkfiajlovwopxxqifxvfufqhfruuazyytfhdzfbjcsysqpnhqretxhsmoyzefopjcokymrtikjybmxdsgjbovhoverxartdyprvbevnvqrmbgbvyiloaulnmrcfnkmknoxamzdkgrfvatdpzmxtvzytzgjwltnrdcplxuooacmlhaggedrzcisgfioqendttyyrgjpmqwvgytkkbkviexylxysgdthjozxviieonfdukrkewnucjzzezirkvewqjvfcnjauxdmzjnjxbbptavhwutalncsnklqcvdwinmtgpnexewmfkckypujmsfauswjvdxxxqztwyaijozeoaiejfielzvbzzbpnoxxgpcakivlaubctoecharcosiixqzdynzfohfmqnlczaneolqwvnbutxjtpwhyguvwqyojhdpmugyjzwrxklyhlqutzoywrxshxdeqohbiwsahqbitwdibxoxbflgtndxwbojqlgafsfjrgdddjxxbmsibhzqctqjjjffcekjicwxanwotafpcyyundheznfxhlmekencnkyrkrzfxmfyxbkrynriukigjnnddrefbikoyxamdnsxkvcekuxvtaroeqmxbcrfdqvvigtgvcqpvkhmzahwdcwdrxqfcxhklnpmezrzvphjanpinvsngxvvtomisealvwkcwfzrfjtgqbthujadglffezwetatshffnoojacrihtteilupfxcbujsvtbjgztjvjdwcooyjqlueuvjpbkrihzljofptolcymyksredxrkqryxclbkmucutpmmkcizdtjchvjneqbncatmxfpoznwejfcedgcxkcohwtkuirurssxwjeskglnzzzkmfhykfaokgttlyrtmbpqaushvmolmxopmxyvuxuaxkudutconspzpfauqsxvcyjdsosgwumbaimipnbwmjyccxyfghfrrahqwlqnndlfqirfoiekmwqgyeguioivcwikzjlbqfoncvhkotxclegkinnyaimihiiuuoijvjevgqdopsofasksthelaflvkxrlxkafdbvirnxntvnkktxgyyejhjdexxrtiedqzzrsuyfluwnlgdyaojzcgybasiicxtxfgdiknbtsdzwofuzocqgkhkknigqwnasepnzbsljzgnzmkghogjyhbhrglcpknylwmjjudzkziunampywsbteezieoitttsxvltrlbhojoupagmotevhpirhdrnygbzztgaponxjmdrikceeybuqfiptgsyrbdytaxavghfdhvgdnuciygnhyxtrahchzgirobgciobxrhxnkgzbzzxtyzyxsqwqfuictjluesxhcscmvgqxpctcjdivmqmtpidthfshwdjzqozpwcdgoomzwnkbijydqmdrvureyijjcvxgpcgiapddihjvwwzyyhkjeqksfcjtupzmniocupsrrssezizvykvohikovwcsrplchsvszapwozkomsofocwxskynczbbojjwvwlyjnftliiygmpfjbvjcmrovogbgsyaxlkzgcdghmgmnefrptlpljgriptttgxmldposmflshiqeknfnhessjrfiznftdbxrnljeqmbkwjzcnajerlrhfjuhfrxgbmpipovyamgbuliwrkmveljjihvyymthdvlolpbrvleyvxswrzscsaqgmgtoeennxylyjxnwovhgglrxmmycisfsuzfpfuuedyuxfkdfmkpnxnnalatfxzsafoasqzsdpmafzjetmyopcivbihxofgxntaehqbipdluhnhhqmpppdtmjkroetdstxzhlskzjzaoyuxfjrskjlmrutfpxadaypwuqtypfvevxjnqyfosmhjwhhptyzgmgpplcsyxcwceoizgzoftswaevovzmfnaagtsnaaedwtkpgoifqbispdwaowyparhuibcvdjaxpfezzotkjerijrcbieplcrhmrfqobnayezivcrpqalrddcppxouzxvmuinrqvxrfldqztxxkglucnejploxnapanewjuldhebyaffrxjppatucpplgfvxdwhzpcjbwbmtldcywbiyevwblpxcgwjuhuxntpawozblezzvmzpcsiqylaadssavahzorwxxfwokoitbdlnjlefzrficeaktusamhcfahaaimbvoeagepvjvraukvslegrxgxdxvofjvgiywshauxpnxktuaaagumxwwmnlovrd";
int main(int argc, const char** argv) {
int len = 1;
if (argc > 1)
len = atoi(argv[1]);
printf("Writing %d bytes at a time\n", len);
int fd = open("bench.dat", O_WRONLY|O_CREAT|O_TRUNC, 0666);
if (fd < 0) {
fprintf(stderr, "open failed: %s\n", strerror(errno));
exit(1);
}
int r = posix_fallocate(fd, 0, 1000 * 1024);
if (r != 0) {
fprintf(stderr, "fallocate failed: %s\n", strerror(r));
exit(1);
}
r = fsync(fd);
if (r != 0) {
fprintf(stderr, "fsync failed: %s\n", strerror(errno));
exit(1);
}
int i;
for (i = 0; i < 1000; ++i) {
r = write(fd, buf, len);
if (r != len) {
fprintf(stderr, "write failed: %s\n", strerror(errno));
exit(1);
}
r = fdatasync(fd);
if (r != 0) {
fprintf(stderr, "fdatasync failed: %s\n", strerror(errno));
exit(1);
}
}
return 0;
}
@ongardie
Copy link
Author

Bug: forgot to adjust the fallocate call. Should be 1000 * len.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment